This post will discuss various methods to sort map in Java according to the natural ordering of its keys.

1. Using TreeMap

TreeMap is a Red-Black tree-based implementation of Map, which is sorted according to its keys’ natural ordering. We can pass an unsorted map to the TreeMap constructor, which will then construct a new tree map containing the same mappings as the given map but ordered according to its keys’ natural ordering.

Download  Run Code

Output:

Sorted map by keys :
{China=Beijing, India=New Delhi, Japan=Tokyo, USA=Washington D.C.}

 
Using Guava’s TreeMap:

Google’s Guava library also provides TreeMap implementation, which we can use to create a mutable, empty TreeMap instance, which is sorted according to a Comparator provided at map creation time. We can pass Guava’s Ordering.natural() for the natural ordering of keys.

Download Code

2. Using LinkedHashMap

LinkedHashMap is a hash table and linked list implementation of the Map interface, with predictable iteration order, which is the order in which keys were inserted into the map. We can use this property to produce a copy of a map that is sorted according to the natural ordering of its keys.

The idea is to create a list of keys present on the map and sort it. Then, we insert a key-value pair in an empty LinkedHashMap for every key in the sorted list. Keys will sort the resultant LinkedHashMap.

Download  Run Code

Output:

Sorted map by keys :
{China=Beijing, India=New Delhi, Japan=Tokyo, USA=Washington D.C.}

 
Here’s an equivalent version using the Stream API.

Download  Run Code

Output:

Sorted map by keys :
{China=Beijing, India=New Delhi, Japan=Tokyo, USA=Washington D.C.}

3. Using Java 8

We can also use Java 8 Stream to sort map by keys. Following are the steps:

  1. Obtain stream from the set view of the mappings contained in the map.
  2. Sort the stream in natural order of keys using Stream.sorted() method by passing comparator returned by Map.Entry.comparingByKey().
  3. Collect all sorted elements in a LinkedHashMap using Stream.collect() with Collectors.toMap().

 
Please note that a stream is a sequence of items, not a sequence of key/value pairs. So, we can’t construct a map out of a stream without specifying how to extract keys and values from it. Java 8 provides Collectors.toMap() method for this purpose. We need to use an overloaded version of toMap() that returns LinkedHashMap instead of HashMap to retain the sorted order.

Download  Run Code

Output:

Sorted map by keys :
{China=Beijing, India=New Delhi, Japan=Tokyo, USA=Washington D.C.}

 
Following is another approach that doesn’t involve using a collector. It uses the Stream.forEachOrdered() method on the sorted stream that inserts each element of the stream into a new LinkedHashMap.

Download  Run Code

Output:

Sorted map by keys :
{China=Beijing, India=New Delhi, Japan=Tokyo, USA=Washington D.C.}

That’s all about sorting Map by keys in Java.

 
Related Post:

Sort Map by values in Java

 
References:

1. TreeMap Javadoc – Java SE 8
2. LinkedHashMap Javadoc – Java SE 8 Javadoc