1

I am trying to sort LinkedHashMap based on its values. What I dont understand are the results. Its seems to be taking only two keys for the sorting. Any pointers as to what am I missing?

public class test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub


        LinkedHashMap<Integer, Integer> sorting = new LinkedHashMap<Integer, Integer>();
        sorting.put(1, 100);
        sorting.put(10, 100);
        sorting.put(20, 200);
        sorting.put(30, 100);
        sorting.put(40, 100);
        sorting.put(50, 200);

        for (Entry<Integer, Integer> entry : sorting.entrySet()) {
             Integer key = entry.getKey();
             Integer value = entry.getValue();
             System.out.println("LINKED UNSORTED === key: "+ key + '\t' + "Value: " + value);
              // do stuff
            }


        Comparator<Integer> comparator = new ValueCom(sorting);
        TreeMap<Integer, Integer> sortedMap =new TreeMap<Integer, Integer>(comparator);
        sortedMap.putAll(sorting);      


        for (Entry<Integer, Integer> entry : sortedMap.entrySet()) {
             Integer key = entry.getKey();
             Integer value = entry.getValue();
             System.out.println("SORTED   === key: "+ key + '\t' + "Value: " + value);
              // do stuff
            }

    }

}


class ValueCom implements Comparator<Integer> {
    LinkedHashMap<Integer, Integer> map = new LinkedHashMap<Integer, Integer>();

public ValueCom(HashMap<Integer, Integer> map) {
        this.map.putAll(map);
    }

public int compare(Integer keyA, Integer keyB){

    return map.get(keyB).compareTo(map.get(keyA));
}

} 

The current output is as explained above

LINKED UNSORTED === key: 1  Value: 100
LINKED UNSORTED === key: 10 Value: 100
LINKED UNSORTED === key: 20 Value: 200
LINKED UNSORTED === key: 30 Value: 100
LINKED UNSORTED === key: 40 Value: 100
LINKED UNSORTED === key: 50 Value: 200
SORTED   === key: 20    Value: 200
SORTED   === key: 1 Value: 100
Betafish
  • 1,212
  • 3
  • 20
  • 45

3 Answers3

0

In TreeMap the uniqueness of keys is determined by the passed Comparator. Since your Comparator compares values of the original Map, all the entries having the same value are considered by the TreeMap to have the same key, so only one entry is added to the TreeMap for each unique value.

If you want a Map sorted by values, you can use a LinkedHashMap for your sorted Map, and make sure that you put the entries in the Map in the desired order.

Eran
  • 387,369
  • 54
  • 702
  • 768
0

The problem is that you are sorting on the basis of values, and if two values are equal, then the TreeMap will make only single entry for that key, You can understand better by seeing below code. Below code will solve the problem, Let me know If anything fails:

Modify compare() method in ValueCom class as below:

    public int compare(Integer i1, Integer i2) {
        if(map.get(i1).equals(map.get(i2))){
            return 1; //To make sure, If values are equal then still there should be entry in TreeMap
        }
        return map.get(i1).compareTo(map.get(i2));
    }

Now, the equal values case is also handled. :)

pbajpai
  • 1,303
  • 1
  • 9
  • 24
0

The code below give you what you want

public class test {

public static void main(String[] args) {
    // TODO Auto-generated method stub


    LinkedHashMap<Integer, Integer> sorting = new LinkedHashMap<Integer, Integer>();
    sorting.put(10, 100);
    sorting.put(1, 100);
    sorting.put(20, 200);
    sorting.put(40, 100);
    sorting.put(30, 100);
    sorting.put(50, 200);

    for (Entry<Integer, Integer> entry : sorting.entrySet()) {
         Integer key = entry.getKey();
         Integer value = entry.getValue();
         System.out.println("LINKED UNSORTED === key: "+ key + '\t' + "Value: " + value);
          // do stuff
        }


    Comparator<Integer> comparator = new ValueCom();
    TreeMap<Integer, Integer> sortedMap =new TreeMap<Integer, Integer>(comparator);
    sortedMap.putAll(sorting);      


    for (Entry<Integer, Integer> entry : sortedMap.entrySet()) {
         Integer key = entry.getKey();
         Integer value = entry.getValue();
         System.out.println("SORTED   === key: "+ key + '\t' + "Value: " + value);
          // do stuff
        }

}

}


class ValueCom implements Comparator<Integer> {
public int compare(Integer keyA, Integer keyB){

return keyA.compareTo(keyB);
}

} 
AmjadD
  • 73
  • 1
  • 9