How to Sort a Map by key in Java?

DeeKay November 20, 2016 at 2:00 am · java.util, sorting

Here we will see how to sort a Map by key in java. There are two ways to sort a Map by Key –

 

First Approach

<br />
/**<br />
 * Created on Nov 19, 2016 Copyright(c) http://kodehelp.com All Rights Reserved.<br />
 */<br />
package com.kodehelp.util;</p>
<p>import java.util.HashMap;<br />
import java.util.Map;<br />
import java.util.TreeMap;</p>
<p>/**<br />
 * @author http://kodehelp.com<br />
 *<br />
 */<br />
public class SortMapByKey {</p>
<p>	/**<br />
	 * @param args<br />
	 */<br />
	public static void main(String[] args) {</p>
<p>		Map<String, String> unSortedMap = new HashMap<String, String>();</p>
<p>		unSortedMap.put("Z", "Zack");<br />
		unSortedMap.put("D", "David");<br />
		unSortedMap.put("E", "Emily");<br />
		unSortedMap.put("B", "Bob");<br />
		unSortedMap.put("C", "Catherine");<br />
		unSortedMap.put("A", "Alex");<br />
		unSortedMap.put("N", "Nancy");<br />
		unSortedMap.put("J", "Joe");</p>
<p>		System.out.println("=================Unsorted Map ================== ");<br />
		printMap(unSortedMap);</p>
<p>		System.out.println("=================Sorted Map ================== ");<br />
		Map<String, String> treeMap = new TreeMap<String, String>(unSortedMap);<br />
                printMap(treeMap);</p>
<p>	}</p>
<p>	public static <K, V> void printMap(Map<K, V> map) {<br />
        for (Map.Entry<K, V> entry : map.entrySet()) {<br />
            System.out.println("Key : " + entry.getKey()<br />
				+ " Value : " + entry.getValue());<br />
        }<br />
    }</p>
<p>}<br />

Second Approach

<br />
/**<br />
 * Created on Nov 19, 2016 Copyright(c) http://kodehelp.com All Rights Reserved.<br />
 */<br />
package com.kodehelp.util;</p>
<p>import java.util.Comparator;<br />
import java.util.HashMap;<br />
import java.util.Map;<br />
import java.util.TreeMap;</p>
<p>/**<br />
 * @author http://kodehelp.com<br />
 *<br />
 */<br />
public class SortMapByKeyExample {</p>
<p>	/**<br />
	 * @param args<br />
	 */<br />
	public static void main(String[] args) {</p>
<p>		Map<String, String> unSortedMap = new HashMap<String, String>();</p>
<p>		unSortedMap.put("Z", "Zack");<br />
		unSortedMap.put("D", "David");<br />
		unSortedMap.put("E", "Emily");<br />
		unSortedMap.put("B", "Bob");<br />
		unSortedMap.put("C", "Catherine");<br />
		unSortedMap.put("A", "Alex");<br />
		unSortedMap.put("N", "Nancy");<br />
		unSortedMap.put("J", "Joe");</p>
<p>		System.out.println("=================Unsorted Map ================== ");<br />
		printMap(unSortedMap);</p>
<p>		System.out.println("=================Sorted Map ================== ");<br />
		Map<String, String> treeMap = new TreeMap<String, String>(new Comparator<String>() {<br />
			@Override<br />
			public int compare(String s1, String s2) {<br />
				return s1.compareTo(s2);<br />
			}<br />
		});<br />
		treeMap.putAll(unSortedMap);<br />
		printMap(treeMap);</p>
<p>	}</p>
<p>	public static <K, V> void printMap(Map<K, V> map) {<br />
		for (Map.Entry<K, V> entry : map.entrySet()) {<br />
			System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());<br />
		}<br />
	}</p>
<p>}<br />

SUBSCRIBE/FOLLOW US