1

I have this code to populate a HashMap and pass it to TreeMap to sort it in natural order in Key values.

    Map<Integer, String[]> hashMap = new HashMap<Integer, String[]>();
    hashMap.put(3, new String[]{"1","2"});
    hashMap.put(2, new String[]{"1","2"});
    hashMap.put(4, new String[]{"1","2"});
    hashMap.put(1, new String[]{"1","2"});

    System.out.println(hashMap);

    Map<Integer, String[]> treeMap = new TreeMap<Integer, String[]>(hashMap);

    System.out.println(hashMap); // Natural Order, Ascending

Now my problems is, How can I sort my treeMap in Descending order? I've prepared my Comparator class named KeyComparator that sort Key to descending order. Here is my code below:

public class KeyComparator implements Comparator<Integer> {
    @Override
    public int compare(Integer o1, Integer o2) {
        if (o1 < o2) {
            return 1;
        } else if (o1 > o2) {
            return -1;
        } else {
            return 0;
        }
    }
}

The TreeMap has no 2 parameterized Constructor like for example TreeMap(new KeyComparator(),hashMap). How can I use my KeyComparator class at the same time use to load the hashMap into my treeMap.

1
  • 1
    If all you need is descending natural order then you could use new TreeMap<Integer, String[]>(hashMap).descendingMap() Commented Nov 29, 2016 at 11:33

5 Answers 5

3

Do it like below, Create a treemap with Comparator and then use putAll method to pass the hashmap to it.

Map<Integer, String[]> treeMap = new TreeMap<Integer, String[]>(new Comparator<Integer>() {
    @Override
    public int compare(Integer o1, Integer o2) {
        if (o1 < o2) {
            return 1;
        } else if (o1 > o2) {
            return -1;
        } else {
            return 0;
        }
    }
});
treeMap.putAll(hashMap);

System.out.println(treeMap); // Descending 

Note:- In the example above, I have passed an anonymous implementation of Comparator. You can always pass the KeyComparator in the constructor.

Sign up to request clarification or add additional context in comments.

1 Comment

treeMap.putAll really helps. This is the first time I used it. thanks.
1

While this may not answer your question, why not simply use an Array to sort it?

System.out.println("map: " + hashMap);
List<Integer> keys = new ArrayList<Integer>(hashMap.keySet());
System.out.println("unsorted keys: " + keys);
Collections.sort(keys);
System.out.println("sorted (asc) keys: " + keys);
Collections.reverse(keys);
System.out.println("sorted (desc) keys: " + keys);

1 Comment

Sorry but I need it in a TreeMap way, btw thanks for some help.
1

As already mentioned, you need first to use the constructor with the key comparator which is TreeMap(Comparator<? super K> comparator) then use putAll(Map<? extends K,? extends V> m) to add all entries.

If you use Java 8 no need to reimplement it, simply use Comparator.reverseOrder() to get the reverse of the natural ordering as next:

Map<Integer, String[]> treeMap = new TreeMap<>(Comparator.reverseOrder());
treeMap.putAll(hashMap);
System.out.println(treeMap);

1 Comment

Im using Java 7. treeMap.putAll is all I need, thanks by the way to overview me in Java 8.
0

The TreeMap has a nice method for this. You can do

 TreeMap<Integer, String[]> map = new TreeMap<>(hashMap);
 NavigleMap<Integer, String[]> reversedMap = map.descendingMap();

Comments

0

Create an empty TreeMap with your comparator first

Map<Integer, String[]> treeMap = new TreeMap<Integer, String[]>(new KeyComparator());

treeMap.putAll(hashMap);

http://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html#TreeMap(java.util.Comparator)

You may have to tweak the syntax

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.