Main Tutorials

How to sort a Map in Java

Few Java examples to sort a Map by its keys or values.

Note
If you are using Java 8, refer to this article – How to use Stream APIs to sort a Map

1. Sort by Key

1.1 Uses java.util.TreeMap, it will sort the Map by keys automatically.

SortByKeyExample1.java

package com.mkyong.test;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class SortByKeyExample1 {

    public static void main(String[] args) {

        Map<String, String> unsortMap = new HashMap<String, String>();
        unsortMap.put("Z", "z");
        unsortMap.put("B", "b");
        unsortMap.put("A", "a");
        unsortMap.put("C", "c");
        unsortMap.put("D", "d");
        unsortMap.put("E", "e");
        unsortMap.put("Y", "y");
        unsortMap.put("N", "n");
        unsortMap.put("J", "j");
        unsortMap.put("M", "m");
        unsortMap.put("F", "f");

        System.out.println("Unsort Map......");
        printMap(unsortMap);

        System.out.println("\nSorted Map......By Key");
        Map<String, String> treeMap = new TreeMap<String, String>(unsortMap);
        printMap(treeMap);

    }

    //pretty print a map
    public static <K, V> void printMap(Map<K, V> map) {
        for (Map.Entry<K, V> entry : map.entrySet()) {
            System.out.println("Key : " + entry.getKey() 
				+ " Value : " + entry.getValue());
        }
    }

}

Output

Unsort Map......
Key : A Value : a
Key : B Value : b
Key : C Value : c
Key : D Value : d
Key : E Value : e
Key : F Value : f
Key : Y Value : y
Key : Z Value : z
Key : J Value : j
Key : M Value : m
Key : N Value : n

Sorted Map......By Key
Key : A Value : a
Key : B Value : b
Key : C Value : c
Key : D Value : d
Key : E Value : e
Key : F Value : f
Key : J Value : j
Key : M Value : m
Key : N Value : n
Key : Y Value : y
Key : Z Value : z

1.2 Yet another java.util.TreeMap example, provide a custom Comparator to sort the key in descending order.

SortByKeyExample2.java

package com.mkyong.test;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class SortByKeyExample2 {

    public static void main(String[] args) {

        Map<Integer, String> unsortMap = new HashMap<Integer, String>();
        unsortMap.put(10, "z");
        unsortMap.put(5, "b");
        unsortMap.put(6, "a");
        unsortMap.put(20, "c");
        unsortMap.put(1, "d");
        unsortMap.put(7, "e");
        unsortMap.put(8, "y");
        unsortMap.put(99, "n");
        unsortMap.put(50, "j");
        unsortMap.put(2, "m");
        unsortMap.put(9, "f");

        System.out.println("Unsort Map......");
        printMap(unsortMap);

        System.out.println("\nSorted Map......By Key");
        Map<Integer, String> treeMap = new TreeMap<Integer, String>(
                new Comparator<Integer>() {

                    @Override
                    public int compare(Integer o1, Integer o2) {
                        return o2.compareTo(o1);
                    }

                });

	    /* For Java 8, try this lambda
		Map<Integer, String> treeMap = new TreeMap<>(
		                (Comparator<Integer>) (o1, o2) -> o2.compareTo(o1)
		        );
		*/
        treeMap.putAll(unsortMap);

        printMap(treeMap);

    }

    public static <K, V> void printMap(Map<K, V> map) {
        for (Map.Entry<K, V> entry : map.entrySet()) {
            System.out.println("Key : " + entry.getKey() 
				+ " Value : " + entry.getValue());
        }
    }

}

Output

Unsort Map......
Key : 1 Value : d
Key : 50 Value : j
Key : 2 Value : m
Key : 99 Value : n
Key : 20 Value : c
Key : 5 Value : b
Key : 6 Value : a
Key : 7 Value : e
Key : 8 Value : y
Key : 9 Value : f
Key : 10 Value : z

Sorted Map......By Key
Key : 99 Value : n
Key : 50 Value : j
Key : 20 Value : c
Key : 10 Value : z
Key : 9 Value : f
Key : 8 Value : y
Key : 7 Value : e
Key : 6 Value : a
Key : 5 Value : b
Key : 2 Value : m
Key : 1 Value : d

2. Sort by Value

Converts the Map into a List<Map>, sorts the List<Map> with a custom Comparator and put it into a new insertion order map – LinkedHashMap

Map ---> List<Map> ---> Collections.sort() --> List<Map> (Sorted) ---> LinkedHashMap
SortByValueExample1.java

package com.mkyong.test;

import java.util.*;

public class SortByValueExample1 {

    public static void main(String[] args) {

        Map<String, Integer> unsortMap = new HashMap<String, Integer>();
        unsortMap.put("z", 10);
        unsortMap.put("b", 5);
        unsortMap.put("a", 6);
        unsortMap.put("c", 20);
        unsortMap.put("d", 1);
        unsortMap.put("e", 7);
        unsortMap.put("y", 8);
        unsortMap.put("n", 99);
        unsortMap.put("j", 50);
        unsortMap.put("m", 2);
        unsortMap.put("f", 9);

        System.out.println("Unsort Map......");
        printMap(unsortMap);

        System.out.println("\nSorted Map......By Value");
        Map<String, Integer> sortedMap = sortByValue(unsortMap);
        printMap(sortedMap);

    }

    private static Map<String, Integer> sortByValue(Map<String, Integer> unsortMap) {

        // 1. Convert Map to List of Map
        List<Map.Entry<String, Integer>> list =
                new LinkedList<Map.Entry<String, Integer>>(unsortMap.entrySet());

        // 2. Sort list with Collections.sort(), provide a custom Comparator
        //    Try switch the o1 o2 position for a different order
        Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
            public int compare(Map.Entry<String, Integer> o1,
                               Map.Entry<String, Integer> o2) {
                return (o1.getValue()).compareTo(o2.getValue());
            }
        });

        // 3. Loop the sorted list and put it into a new insertion order Map LinkedHashMap
        Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
        for (Map.Entry<String, Integer> entry : list) {
            sortedMap.put(entry.getKey(), entry.getValue());
        }

        /*
        //classic iterator example
        for (Iterator<Map.Entry<String, Integer>> it = list.iterator(); it.hasNext(); ) {
            Map.Entry<String, Integer> entry = it.next();
            sortedMap.put(entry.getKey(), entry.getValue());
        }*/


        return sortedMap;
    }

    public static <K, V> void printMap(Map<K, V> map) {
        for (Map.Entry<K, V> entry : map.entrySet()) {
            System.out.println("Key : " + entry.getKey()
                    + " Value : " + entry.getValue());
        }
    }

}

Output

Unsort Map......
Key : a Value : 6
Key : b Value : 5
Key : c Value : 20
Key : d Value : 1
Key : e Value : 7
Key : f Value : 9
Key : y Value : 8
Key : z Value : 10
Key : j Value : 50
Key : m Value : 2
Key : n Value : 99

Sorted Map......By Value
Key : d Value : 1
Key : m Value : 2
Key : b Value : 5
Key : a Value : 6
Key : e Value : 7
Key : y Value : 8
Key : f Value : 9
Key : z Value : 10
Key : c Value : 20
Key : j Value : 50
Key : n Value : 99

2.2 Upgrade the above sortByValue() method to support generics.


    public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> unsortMap) {

        List<Map.Entry<K, V>> list =
                new LinkedList<Map.Entry<K, V>>(unsortMap.entrySet());

        Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
            public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
                return (o1.getValue()).compareTo(o2.getValue());
            }
        });

        Map<K, V> result = new LinkedHashMap<K, V>();
        for (Map.Entry<K, V> entry : list) {
            result.put(entry.getKey(), entry.getValue());
        }

        return result;

    }

Update History

  1. July 7, 2010 – First draft.
  2. July 8, 2014 – Add more examples and fix typos.
  3. August 12, 2016 – Add generic method, reformat the code, fix typos and rewrite some content.

References

  1. TreeMap JavaDoc
  2. LinkedHashMap JavaDoc
  3. Comparator JavaDoc
  4. Java 8 – How to sort a Map
  5. More Fun with Wildcards
  6. Collections.sort() signature
  7. Difference between and in Java

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
50 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Larss
6 years ago

How do you sort Stringed Keys that contain numbers e.g
“London 1”
“London 10”
“London 2”
“London 3”

Irfan Maulana
10 years ago

[ask]

master… how if I want sort Map by length value from bigger.

mkyong
9 years ago
Reply to  Irfan Maulana

from bigger? Example?

viji
8 months ago

Write a program to create a TreeMap that stores the names of cities as keys and their populations as values. Add a few entries to the map and display them in alphabetical order of the city names 

danuka
2 years ago

thank you

arun singh
5 years ago

thanks very good blog

??????? ?????
6 years ago

How to sort in descending order?

arun singh
5 years ago
Reply to  ??????? ?????

make just a little change here:
return (o2.getValue()).compareTo(o1.getValue());

SolrWind
7 years ago

Awesome post. I love the detail you put into it. I was trying to sort a map by its values and your implementation worked perfectly. I put it into a MapUtils class and put the proper attribution into the Javadoc so you get credit in case anyone ever sees it. Thanks.

Teja
8 years ago

How to iterate only 10 values in a HashMap ?

Elitsa Venchova
7 years ago
Reply to  Teja

You can just make for() and counter. When counter is equal to 10 then just call “break”
int i=0;
for(Map.Entry entry: myMap.entrySet()){
//do something
i++;
if(i==10){
break;
}
}

Bharat Savani
8 years ago

Hi, you can also use generic version for sorting HashMap by values. I have discussed it here
http://goo.gl/WvOBfZ

Maneesh Ambaati
8 years ago

how can i sort if Value is a String object

Réda Serhani
8 years ago

Thanks from Morocco.

Pravin Rane
8 years ago

// Program of Map Sort By VALUES

package com.base.utilities;

import java.util.Iterator;

import java.util.LinkedHashMap;

import java.util.Map;

import java.util.Set;

import java.util.TreeMap;

/**

*

* @author PRAVIN RANE

*

* @param

* @param

*/

public class ExtendedMap {

@SuppressWarnings(“rawtypes”)

private Map map = new TreeMap();

@SuppressWarnings(“rawtypes”)

private Map orignalHashMap= new LinkedHashMap();

@SuppressWarnings(“unchecked”)

public void put(K k , V v) throws Exception

{

if(v==null){

throw new Exception(“Null value passed in map!”);

}

map.put(v.toString()+”$”+k.toString(), v);

}

@SuppressWarnings({ “rawtypes”, “unchecked” })

public Map backSortedMap(){

Set set =map.keySet();

Iterator it=set.iterator();

while(it.hasNext()){

String key = (String) it.next();

String value = map.get(key)+””;

key = key.replace(value+”$”, “”);

orignalHashMap.put(key, value);

}

return orignalHashMap;

}

public void setMap(Map map){

Set keys = map.keySet();

Iterator iterator = keys.iterator();

while(iterator.hasNext()){

String key = (String) iterator.next();

try {

put((K) key, map.get(key));

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

viji
8 months ago
Reply to  Pravin Rane

Nice

Ab
9 years ago

Hi, Just a small question from performance point of view…for a sorted Map , would you prefer to use TreeMap or HashMap which is then sorted by some Comaparator.

Thx

Ab
9 years ago

Hi, Just a small question from performance point of view…for a sorted Map , would you prefer to use TreeMap or HashMap which is at the sorted by some Comaparator.

Thx

mkyong
9 years ago

Thanks.

GOWRI SANKAR
8 years ago
Reply to  mkyong

only problem is that the values are put in a TreeSet which removes duplicates, if any.

Soumyasanta Bhowmik
10 years ago

Hello mkyong,

I have used below and I am getting key as sorted way.

Map<String, List> objNormalTemplateHashMap=new TreeMap<String, List>();

Please let me know, is this correct way to do?

Thanks , Soumyasanta Bhowmik.

Java
10 years ago

Sorting TreeMap with key will not work when the key is more than 10. The result will not be in a soreted manner.

mkyong
9 years ago
Reply to  Java

Mind to give me an example, TreeMap works well at my end.

Free Job Posting and CV or Resume download Job Portal
10 years ago

It is very useful information in my one of functional part in my Project

andresrolo
11 years ago

this is perfect.
Thanks
from colombia

Mahendra
11 years ago

Very good example but it will fail if you try using null as value.

Alex
11 years ago

Why not to use SortedMap? It exists in the Java API since 1.2 version. There is no need to re-invent anything!

mkyong
9 years ago
Reply to  Alex

TreeMap is one of the SortedMap Implementation.

Daniel
10 years ago
Reply to  Alex

Because SortedMap orders by Keys, this implementation sorts by the values.

Potney
12 years ago

Thanks for the very nice piece of code 🙂

Balasubramaniyan Murugesan
12 years ago

Very nice website thanks for ur work..

Marian
13 years ago

Nice man,
Mine, very quick attempt without much thinking, after having a few beers:) :
sortedValues = new TreeSet(unsortMap.values()).asList();
HashMap sortedMap = new HashMap
for (Object value : sortedValues) { //sorted iteration
for ( Set entry : unsortMap.entrySet() {
if (entry.getValue().equals(value) {
sortedMap.put(entry.getKey(), entry.getValue());
}
}
}

Anonymous
13 years ago

There’s no guarantee the posted implementation will work. Or work on every platform.

The contract for a Map does not guarantee the ordering of its Map.Entry members.

So, just because you sort the values and then put them back in the map in that order, there’s no contract guarantee that the collection returned from values() or an iteration of the entrySet() will be in that order.

The replies that use TreeMap with a special comparator might work, but they defy the contract of TreeMap, which says the entries are sorted by KEY; wrap that TreeMap in a new class that makes an explicit contract to order its entries by value and hide all the implementation details inside.

TymTheEnchanter
13 years ago
Reply to  Anonymous

Isn’t that what the LinkedHashMap does? Provide a predictable iteration order based on input order?

sudheer kumar
6 years ago
Reply to  mkyong

for sorting Map.Entry values, you can use Collections.sort(list,Map.Entry.comparingByValue());
This is easy than to write a custom comparator.

cane
11 years ago
Reply to  mkyong

I think, it possible to Sort a Map by values using Set instead of List.

private static final Comparator<Map.Entry<String, String>> valueComparator = new Comparator<Map.Entry<String, String>>() {
	@Override
	public int compare(Map.Entry<String, String> e1,
			Map.Entry<String, String> e2) {
		//comparing by values, if it's equals, compare by keys
		// in other case, entries with equals values will be removed
		if(e1.getValue().equals(e2.getValue()))
			return e1.getKey().compareTo(e2.getKey());

		return (e1.getValue()).compareTo(e2.getValue());
	}
};

private static Map<String, String> sortByComparator(
			Map<String, String> unsortMap) {

		// sorted set based on comparator
		SortedSet<Map.Entry<String, String>> set = new TreeSet<>(
				valueComparator);
		set.addAll(unsortMap.entrySet());

		// LinkedHashMap make sure order in which keys were inserted
		Map<String, String> sortedMap = new LinkedHashMap<>();
		for (Iterator<Map.Entry<String, String>> it = set.iterator(); it
				.hasNext();) {
			Map.Entry<String, String> entry = it.next();
			sortedMap.put(entry.getKey(), entry.getValue());
		}
		return sortedMap;
	}
Anonymous
13 years ago
Reply to  mkyong

why does one need to do this, I wonder… then I may have a solution…

LinkedHashMap is a concrete class? I’ve never needed to do what is being described, so… ignorance is bliss… I stick to using Map as an interface and do not write code that requires a specific implementation.

Nan Shi
11 years ago
Reply to  Anonymous

A use case of a sorted map is that: 1. when u put pairs into a map, u want to remove or update on duplicated keys, i.e. keep keys uniq. 2. you need a sorted list, say then u can ask for the top 5 keys that have the bigger values. This is actually a very valuable and useful case.

Chris
13 years ago

Take it to the next level and make it like TreeMap but on the values so whenever you insert it goes into the sorted by value spot.

Anthony Goubard
13 years ago

Here is a shorter version I’ve written that doesn’t use a list

public static  Map sortByValues(final Map map) {
	   
    Comparator valueComparator = new Comparator() {
	   public int compare(K k1, K k2) {
			 
	   int compare = ((String) map.get(k2)).compareTo((String) map.get(k1));
		   
	   if (compare == 0) {
		   return 1;
	   } else {
		   return compare;
	   }
    }  
   };
	
   Map sortedByValues = new TreeMap(valueComparator);
   sortedByValues.putAll(map);
	   
   return sortedByValues;
}
Harikrishnan
11 years ago

Still more shorter 🙂 try this..

TreeMap sortedByValues = new TreeMap(unsortedMap);

sortedMap – map will be sorted by key.

Anthony Goubard
13 years ago
Reply to  mkyong

Yes inverse k1 and k2 in the comparator

Zemian Deng
13 years ago

Few comments:
1) The example is probably better named ValueSortedMap, to not confusing with existing java.util.SortedMap, which is by keys.

2) Your example mixed with Java generics and then not using it all the way through. Very annoying. You should correctly typing the List and Map.Entry for example will save you from casting.

3) Probably good to mention a great use case for this is where you use a Map model to display a HTML form (spring form) option list, and want to sort the listing values.

4) If you are not going to have duplicated values in the map, consider using the commons-collections library, which provided TreeBidiMap just for this. You just call inverseBidiMap() to get a reversed map with values sorted as keys! But the library is not fitted with 1.5 generics though, minor thing consider you get all the implementation for free. 🙂

Nan Shi
11 years ago
Reply to  mkyong

mkyong, you can probably use Eclipse instead of some geeky code editors, so you will see all those warnings and get it fixed before publishing your snippets.

mkyong
9 years ago
Reply to  Nan Shi

Thanks, article is updated, all warnings are fixed 🙂

Hfunes
4 years ago

Very helpful, thank you so much!!! 🙂

Mitali
7 years ago

In Sort by value, what if the values are duplicate? Will this code sort by key in case of similar values? If no then how to do that?