0

I needed to sort my treemap based on it's value. The requirements of what I'm doing are such that I have to use a sorted map. I tried the solution here: Sort a Map<Key, Value> by values (Java) however as the comments say, this will make getting values from my map not work. So, instead I did the following:

class sorter implements Comparator<String> {
    Map<String, Integer> _referenceMap;
    public boolean sortDone = false;
    public sorter(Map<String, Integer> referenceMap) {
        _referenceMap = referenceMap;
    }
    public int compare(String a, String b) {
        return sortDone ? a.compareTo(b) : _referenceMap.get(a) >= _referenceMap.get(b) ? -1 : 1;
    }
}

So I leave sortDone to false until I'm finished sorting my map, and then I switch sortDone to true so that it compares things as normal. Problem is, I still cannot get items from my map. When I do myMap.get(/anything/) it is always null still.

I also do not understand what the comparator inconsistent with equals even means.

Community
  • 1
  • 1
David Zorychta
  • 13,039
  • 6
  • 45
  • 81

2 Answers2

1

I also do not understand what the comparator inconsistent with equals even means.

As per the contract of the Comparable interface.

The natural ordering for a class C is said to be consistent with equals if and only if e1.compareTo(e2) == 0 has the same boolean value as e1.equals(e2) for every e1 and e2 of class C. Note that null is not an instance of any class, and e.compareTo(null) should throw a NullPointerException even though e.equals(null) returns false.

It is strongly recommended (though not required) that natural orderings be consistent with equals.

I believe you need to change the line :

 _referenceMap.get(a) >= _referenceMap.get(b) ? -1 : 1;

to

 _referenceMap.get(a).compareTo(_referenceMap.get(b));

Since if the Integer returned by _referenceMap.get(a) is actually == in value to the Integer returned by _referenceMap.get(b) then you should ideally return 0, not -1.

Community
  • 1
  • 1
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • when I do "sortedData.firstKey()" it gives me the correct key, but still when I try "sortedData.get("smethingHere")" it is null every time. – David Zorychta Jul 31 '13 at 05:04
  • @Macmee How are you putting values in `Map` ? Can you show us ? – AllTooSir Jul 31 '13 at 05:05
  • I am putting them into my map using the put method, just iterating over a list of items (String, Integer), for example sortedData.put("Ardvark", 1000); sortedData.put("Zebra", 99999); and the map automatically sorts naturally by key so that it would have the order of Ardvark and then Zebra, however I want my treemap to sort by value so that I have Zebra and then Ardvark. The method I posted sorts my map fine, I can even print sortedData.toString() and see it sorted, but if I try sortedData.get("Zebra") then it returns null. – David Zorychta Jul 31 '13 at 05:10
0

It means you must implement, ie override, the equals() method to compare the same field(s) you are comparing for the compareTo() method.

It is good practice to override the hashCode() method to return a hash based on the same fields too.

Bohemian
  • 412,405
  • 93
  • 575
  • 722