0

I am new in Java. I am implementing Comparator interface & i am having a compareTo function below:-

public int compare(Object o1, Object o2) {
    Map.Entry e1 = (Map.Entry) o1;
    Map.Entry e2 = (Map.Entry) o2;
    return ((Comparable) e1.getValue()).compareTo(e2.getValue());
}

I am not be able to understand what does this line means

Map.Entry e1 = (Map.Entry) o1;

What is the use of two Map.Entry ??

Why Comparable in type casting ??

Someone please give me a reference so that i can figure it out. Thanks.

Edited:- Here is my whole class,i am having a HashMap I want to sort by values using Generic class,hence writing this class

package via;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class SortMap<K, V> {
    public Map<K, V> getSortedMap(Map<K, V> mapToSort) {
        List<Map.Entry> list = new ArrayList<Map.Entry>(mapToSort.entrySet());
        Collections.sort(list, new Comparator() {

            @Override
            public int compare(Object o1, Object o2) {
                Map.Entry e1 = (Map.Entry) o1;
                Map.Entry e2 = (Map.Entry) o2;
                return ((Comparable) e2.getValue()).compareTo(e1.getValue());
            }

        });
        Map sortedMap = new LinkedHashMap();
        for(Iterator i=list.iterator();i.hasNext();)
        {
            Map.Entry entry=(Map.Entry)i.next();
            sortedMap.put(entry.getKey(), entry.getValue());
        }
        System.out.println(list);
        return sortedMap;
    }

}

1 Answers1

2

I am considering here, both K,V are String values. If you have some other values then first your classes for K,V should follow equals() and hashcode() rules. Comparable is not required if you follow the following program.

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class SortMap {
    public Map<String, String> getSortedMap(Map<String, String> mapToSort) {
        Set<Map.Entry<String,String>> mapEntry = mapToSort.entrySet();

        List<Map.Entry<String,String>> listMap= new ArrayList<Map.Entry<String,String>>();
        listMap.addAll(mapEntry);
        Collections.sort(listMap, new Comparator<Map.Entry<String,String>>() {

            @Override
            public int compare(Map.Entry<String,String> o1, Map.Entry<String,String> o2) {
                return  o1.getValue().compareTo(o2.getValue());
            }

        });
        Map<String,String> sortedMap = new LinkedHashMap<String,String>();
        for(Map.Entry<String,String> mapE:listMap)
        {
            sortedMap.put(mapE.getKey(), mapE.getValue());
        }
        System.out.println(sortedMap);
        return sortedMap;
    }

}

Map.Entry is nothing but Entry class written inside HashMap class which implements Entry interface which is written inside Map interface. so there are rules to use inner class references.

static class Entry<K,V> implements Map.Entry<K,V> {
        final K key;
        V value;
        Entry<K,V> next;
        final int hash;

        /**
         * Creates new entry.
         */
        Entry(int h, K k, V v, Entry<K,V> n) {
            value = v;
            next = n;
            key = k;
            hash = h;
        }
}
AmitG
  • 10,365
  • 5
  • 31
  • 52