How to sort LinkedHashMap by its Keys ?

In this article, we will discuss how to convert ordered LinkedHashMap into Sorted TreeMap by its Keys

Sorting LinkedHashMap in both Ascending/descending order by its Keys:

  1. Ascending order: pass LinkedHashMap entries as arguments to TreeMap class’ inter-conversion constructor
  2. Descending order: Implement Comparator interface by providing reverse sorting logic and finally putting all entries of HashMap into TreeMap classs using putAll(); method

 

Example 1: Sorting in ascending order of keys:

  • LinkedHashMap maintains insertion-order
  • In below demo example, we will sort/convert LinkedHashMap into natural-ordering of keys i.e.; ascending-order of keys

Syntax:

// convert to TreeMap
Map<String, String> ts = new TreeMap<String, String>(linkMap);

SortingLinkedHashMapInAscOrder.java

package in.bench.resources.collection;

import java.util.LinkedHashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class SortingLinkedHashMapInAscOrder {

	public static void main(String[] args) {

		// creating LinkedHashMap object of type <String, String>
		LinkedHashMap<String, String> linkMap = 
				new LinkedHashMap<String, String>(); 

		// adding key-value pairs to LinkedHashMap object
		linkMap.put("Microsoft", "Bill Gates");
		linkMap.put("Apple", "Steve Jobs");
		linkMap.put("Google", "Sundar Pichai");
		linkMap.put("LinkedIn", "Reid Hoffman");
		linkMap.put("Facebook", "Mark Zuckerberg");

		// Insertion-Order iterating
		System.out.println("Before Sorting : Insertion-Order \n");

		// getting keySet() into Set
		Set<String> set = linkMap.keySet();

		// get Iterator from key set
		Iterator<String> itr = set.iterator();

		// iterating in Insertion-order
		while(itr.hasNext()) {
			String key = itr.next();
			System.out.println("Key : "  + key 
					+ "\t\t" + "Value : "  + linkMap.get(key));
		}

		// After Sorting : Ascending order
		System.out.println("\n\n\nAfter Sorting : Ascending-Order\n");

		// convert to TreeMap
		Map<String, String> ts = new TreeMap<String, String>(linkMap);

		// iterate acc to ascending order of keys
		for(String strKey : ts.keySet()){
			System.out.println("Key : "  + strKey 
					+ "\t\t" + "Value : "  + linkMap.get(strKey));
		}
	}
}

Output:

Before Sorting : Insertion-Order 

Key : Microsoft		Value : Bill Gates
Key : Apple		Value : Steve Jobs
Key : Google		Value : Sundar Pichai
Key : LinkedIn		Value : Reid Hoffman
Key : Facebook		Value : Mark Zuckerberg



After Sorting : Ascending-Order

Key : Apple		Value : Steve Jobs
Key : Facebook		Value : Mark Zuckerberg
Key : Google		Value : Sundar Pichai
Key : LinkedIn		Value : Reid Hoffman
Key : Microsoft		Value : Bill Gates

 

Example 2: Sorting in descending order of keys:

  • LinkedHashMap maintains insertion-order
  • In below demo example, we will sort/convert LinkedHashMap into reverse-ordering of keys i.e.;  descending-order of keys

Syntax:

// convert to TreeMap
// descending or reverse-order sorting logic
Map<String, String> tMap = new TreeMap<String, String>(
		new Comparator<String>() {

			@Override
			public int compare(String str1, String str2) {
				return str2.compareTo(str1);
			}
		});

// put all key-value into TreeMap
tMap.putAll(linkMap);

SortingLinkedHashMapInDesOrder.java

package in.bench.resources.collection;

import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class SortingLinkedHashMapInDesOrder {

	public static void main(String[] args) {

		// creating LinkedHashMap object of type <String, String>
		LinkedHashMap<String, String> linkMap = 
				new LinkedHashMap<String, String>(); 

		// adding key-value pairs to LinkedHashMap object
		linkMap.put("Microsoft", "Bill Gates");
		linkMap.put("Apple", "Steve Jobs");
		linkMap.put("Google", "Sundar Pichai");
		linkMap.put("LinkedIn", "Reid Hoffman");
		linkMap.put("Facebook", "Mark Zuckerberg");

		// Insertion-Order iterating
		System.out.println("Before Sorting : Insertion-Order \n");

		// getting keySet() into Set
		Set<String> set = linkMap.keySet();

		// get Iterator from key set
		Iterator<String> itr = set.iterator();

		// iterating in insertion-order
		while(itr.hasNext()) {
			String key = itr.next();
			System.out.println("Key : "  + key 
					+ "\t\t" + "Value : "  + linkMap.get(key));
		}

		// After Sorting : Descending order
		System.out.println("\n\n\nAfter Sorting : Descending-Order\n");

		// convert to TreeMap
		// descending or reverse-order sorting logic
		Map<String, String> tMap = new TreeMap<String, String>(
				new Comparator<String>() {

					@Override
					public int compare(String str1, String str2) {
						return str2.compareTo(str1);
					}
				});

		// put all key-value into TreeMap
		tMap.putAll(linkMap);

		// iterate acc to descending order of keys
		for(String strKey : tMap.keySet()){
			System.out.println("Key : "  + strKey 
					+ "\t\t" + "Value : "  + linkMap.get(strKey));
		}
	}
}

Output:

Before Sorting : Insertion-Order 

Key : Microsoft		Value : Bill Gates
Key : Apple		Value : Steve Jobs
Key : Google		Value : Sundar Pichai
Key : LinkedIn		Value : Reid Hoffman
Key : Facebook		Value : Mark Zuckerberg



After Sorting : Descending-Order

Key : Microsoft		Value : Bill Gates
Key : LinkedIn		Value : Reid Hoffman
Key : Google		Value : Sundar Pichai
Key : Facebook		Value : Mark Zuckerberg
Key : Apple		Value : Steve Jobs

 

Important points:

  • HashMap stores entries (key-value pairs) in random-order
  • LinkedHashMap stores entries (key-value pairs) as per insertion-order
  • TreeMap stores entries (key-value pairs) in some sorting-order

 

References:

 

Happy Coding !!
Happy Learning !!

How to sort LinkedHashMap by its Values ?
How to sort LinkedHashSet contents in Java ?