Core Java

Sorting a HashMap In Java

Introduction:

In this tutorial, we’ll learn how to sort a Java HashMap. We can sort a HashMap either by keys or by values. We’ll discuss both of these strategies.

Sorting a Java HashMap:

To follow along with the rest of the article, let’s first construct a HashMap:

where Student is a POJO with id and name as its fields:

Now, let’s look at the different approaches we can take to sort a HashMap:

1. Using TreeMap:

As we know, TreeMap is a sorted Map where, by default, the elements are sorted by the natural ordering of their keys. So, a basic approach would be to push all the elements from a HashMap to a TreeMap:

Note that we can either pass the original HashMap itself into the constructor or else we can use a putAll() method. With this, our sortedMap will hold the elements in sorted order of key values:

2. With ArrayList:

If we just need the sorted keys or values and don’t really care about the Map,  we can rather use an ArrayList. 

Here, we’ll first extract either the keys or the values into an ArrayList and then sort that List using Collections.sort():

Here, sorting a Map by values will only work when our Student implements Comparable:

This is so as the values in our map is a custom type object – Student.

3. With TreeSet:

If we also intend to avoid any duplicates in the resulting list of keys or values, we can opt for a TreeSet:

Similarly, we can create a sorted set of map values. However, we’ll have to override the equals() and hashCode() method for this to work for custom objects.

4. Java 8 Streams:

Java 8 onwards, we can use the Stream API to sort our map. To sort a Java Map using keys, we’ll have:

Here, we have collected the sorted map into a LinkedHashMap to preserve the sorted ordering

Similarly, to sort by the map by values, we’ll use comparingByValue():

As usual, when dealing with custom objects, that object must implement the Comparable interface.

We have a detailed article on Java 8 Collectors, which will help you understand Collectors.toMap() method usage.

In order to sort the map in descending order, we can simply use Collections.reverseOrder():

Let’s print out sortedMapDesc on the console:

As we can see, the map is now sorted in the descending order by keys.

Conclusion:

In this article, we covered various approaches for sorting a Java HashMap – using TreeMap, ArrayList or TreeSet. We also covered the Java 8 Stream API sorted() method which can help us sort our map either by keys or values.

 

Be the First to comment.

Leave a Comment

Your email address will not be published. Required fields are marked *