Home > Tutorial > Java HashMap Sort By Key

Java HashMap Sort By Key

The Java HashMap is a good option to store data that you need to define as a key and value pair. The HashMap does not allow for duplicate keys, but does allow for duplicate values.

If you do a HashMap.put() using the same key, it will overwrite an existing key with the latest key. For our demonstration, the HashMap will be a list of first names (String) followed by an age value (Integer).

When you add key/value pairs to a HashMap, the order is not known or maintained so you may need to sort the HashMap by keys.


Java HashMap Sort By Key

The sample Java program defines a new HashMap with String as the key and Integer as the value. We add some sample data to our HashMap. After printing out the HashMap, you can see the keys are in no particular order. Here comes the Java TreeMap, which does a natural ordering of the keys. That a a fancy way of saying that the keys are ordered alphabetically as Strings.

package com.jcd.java.tutorials;

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

public class JavaSortHashMap 
{

	public static void main(String[] args) 
	{
		
		//Create an empty HashMap to hold String as Key and Integer as Value
		HashMap<String,Integer> javaCodeDepotHashMap = new HashMap<String,Integer>();
		
		//Add some kay/value pairs
		javaCodeDepotHashMap.put("Seymour", 25);
		javaCodeDepotHashMap.put("Smedley", 35);
		javaCodeDepotHashMap.put("Vince", 21);
		javaCodeDepotHashMap.put("Kevin", 45);
		javaCodeDepotHashMap.put("Arthur", 95);
		javaCodeDepotHashMap.put("Arthur", 94);
		
		System.out.println("Unsorted HashMap:\n");
		
		//Loop through our HashMap and print out key/value pairs
		for (Map.Entry<String,Integer> myEntry : javaCodeDepotHashMap.entrySet()) 
		{
			System.out.println("Key="+myEntry.getKey()+" Value="+myEntry.getValue());
		}
		
		//TreeMap maintains keys in sorted order
		Map<String,Integer> javaCodeDepotHashMapSorted = new TreeMap<String,Integer>(javaCodeDepotHashMap);
		

		System.out.println("\nSorted HashMap:\n");
		
		//Loop through our HashMap and print out key/value pairs
		for (Map.Entry<String,Integer> myEntry : javaCodeDepotHashMapSorted.entrySet()) 
		{
			System.out.println("Key="+myEntry.getKey()+" Value="+myEntry.getValue());
		}	 
		
	}

}


Java HashMap Sort By Key Output

Unsorted HashMap:

Key=Vince Value=21
Key=Kevin Value=45
Key=Smedley Value=35
Key=Arthur Value=94
Key=Seymour Value=25

Sorted HashMap:

Key=Arthur Value=94
Key=Kevin Value=45
Key=Seymour Value=25
Key=Smedley Value=35
Key=Vince Value=21



Concluding Comments

If your program or application is a HashMap and you need to order the keys alphabetically, load your HashMap into a TreeMap because the TreeMap will automatically order the keys for you. You may be asking yourself why not just TreeMap over a HashMap. Good question. There are some good reasons but mainly it comes down to performance. The Java HashMap is going to be faster.



Java References:

Seymour
Posted by

Seymour

I have been a software developer for twenty years now focusing on writing high quality Java applications. If you are in a programming field, you know that technology is constantly changing. You have to keep current or you will get left behind!

You may also like...

Leave a Reply

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

Copyright © 2018 JavaCodeDepot