Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java TreeMap Comparator

I need a comparator for a TreeMap. Should I write this anonymously in the constructor for my TreeMap? How else could I write my comparator. Currently, Java does not like my code (can I do this anonymously?):

SortedMap<String, Double> myMap =      new TreeMap<String, Double>(new Comparator<Entry<String, Double>>()     {         public int compare(Entry<String, Double> o1, Entry<String, Double> o2)         {             return o1.getValue().compareTo(o2.getValue());         }      }); 
  1. Can I do the above anonymously?
  2. How else could I do this?
  3. I want to sort myMap by the Value not the Key
like image 344
CodeKingPlusPlus Avatar asked Oct 18 '12 04:10

CodeKingPlusPlus


People also ask

Can we use Comparator with TreeMap in Java?

The comparator() method of java. util. TreeMap class is used to return the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys.

How do I add a Comparator to TreeMap in Java?

SortedMap<String, Double> myMap = new TreeMap<String, Double>(new Comparator<Entry<String, Double>>() { public int compare(Entry<String, Double> o1, Entry<String, Double> o2) { return o1. getValue(). compareTo(o2. getValue()); } });

How do I sort TreeMap with Comparator?

To sort keys in TreeMap by using a comparator with user-defined objects in Java we have to create a class that implements the Comparator interface to override the compare method. In the below code, we are passing a custom object as a key in TreeMap i.e Student user-defined class.

Can we use Comparator with HashMap in Java?

Sort HashMap by Values using Comparator InterfaceTo sort the HashMap by values, we need to create a Comparator. It compares two elements based on the values. After that get the Set of elements from the Map and convert Set into the List.


1 Answers

You can not sort TreeMap on values.

A Red-Black tree based NavigableMap implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used You will need to provide comparator for Comparator<? super K> so your comparator should compare on keys.

To provide sort on values you will need SortedSet. Use

SortedSet<Map.Entry<String, Double>> sortedset = new TreeSet<Map.Entry<String, Double>>(             new Comparator<Map.Entry<String, Double>>() {                 @Override                 public int compare(Map.Entry<String, Double> e1,                         Map.Entry<String, Double> e2) {                     return e1.getValue().compareTo(e2.getValue());                 }             });    sortedset.addAll(myMap.entrySet()); 

To give you an example

    SortedMap<String, Double> myMap = new TreeMap<String, Double>();     myMap.put("a", 10.0);     myMap.put("b", 9.0);     myMap.put("c", 11.0);     myMap.put("d", 2.0);     sortedset.addAll(myMap.entrySet());     System.out.println(sortedset); 

Output:

  [d=2.0, b=9.0, a=10.0, c=11.0] 
like image 103
Amit Deshpande Avatar answered Sep 22 '22 12:09

Amit Deshpande