Java 8 – How to sort HashMap by its Values first and then by its Keys ?

In this article, we will discuss how to sort a HashMap by its Values first and then by its Keys

1. Sorting HashMap by Value first and then by Key :

  • Here, Map Values will be sorted first in decreasing-order and then by Keys in decreasing-order
  • While sorting if there are 2/more same Map Values then sort according to Keys

SortHashMapByValuesAndThenByKeys.java

package in.bench.resources.find.duplicate.count;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class SortHashMapByValuesAndThenByKeys {

	public static void main(String[] args) {

		// create HashMap object
		Map<Integer, Long> counts = new HashMap<>();


		// add entries to HashMap
		counts.put(7, 2L);
		counts.put(11, 2L);
		counts.put(8, 1L);
		counts.put(2, 1L);
		counts.put(10, 1L);
		counts.put(5, 3L);
		counts.put(9, 5L);


		// sort HashMap by Values in decreasing-order & Keys in decreasing-order
		LinkedHashMap<Integer, Long> sortedMap = counts
				.entrySet()
				.stream()
				.sorted(
						Map.Entry.<Integer, Long>comparingByValue().reversed()
						.thenComparing(Map.Entry.<Integer, Long>comparingByKey().reversed())
						)
				.collect(
						Collectors.toMap(
								Map.Entry::getKey, 
								Map.Entry::getValue, 
								(oldValue, newValue) -> oldValue, 
								LinkedHashMap::new)
						);


		// print to console
		System.out.println("Value\tKey\n");
		sortedMap.forEach((key, value) -> System.out.println(value + "\t" + key));
	}
}

Output :

Value	Key

5	9
3	5
2	11
2	7
1	10
1	8
1	2

2. Sorting in Natural order :

  • Here again Map Values will be sorted first and then Keys but both in increasing-order (or ascending-order)

SortHashMapByValuesAndThenByKeys.java

// sort HashMap by Values in increasing-order & Keys in increasing-order
		LinkedHashMap<Integer, Long> sortedMap = counts
				.entrySet()
				.stream()
				.sorted(
						Map.Entry.<Integer, Long>comparingByValue()
						.thenComparing(Map.Entry.<Integer, Long>comparingByKey())
						)
				.collect(
						Collectors.toMap(
								Map.Entry::getKey, 
								Map.Entry::getValue, 
								(oldValue, newValue) -> oldValue, 
								LinkedHashMap::new)
						);

3. Different order for Map Values & Keys :

  • Sorting Map Values in ascending-order and Keys in descending-order

SortHashMapByValuesAndThenByKeys.java

// sort HashMap by Values in increasing-order & Keys in decreasing-order
		LinkedHashMap<Integer, Long> sortedMap = counts
				.entrySet()
				.stream()
				.sorted(
						Map.Entry.<Integer, Long>comparingByValue()
						.thenComparing(Map.Entry.<Integer, Long>comparingByKey().reversed())
						)
				.collect(
						Collectors.toMap(
								Map.Entry::getKey, 
								Map.Entry::getValue, 
								(oldValue, newValue) -> oldValue, 
								LinkedHashMap::new)
						);
  • Sorting Map Values in descending-order and Keys in ascending-order

SortHashMapByValuesAndThenByKeys.java

// sort HashMap by Values in decreasing-order & Keys in increasing-order
		LinkedHashMap<Integer, Long> sortedMap = counts
				.entrySet()
				.stream()
				.sorted(
						Map.Entry.<Integer, Long>comparingByValue().reversed()
						.thenComparing(Map.Entry.<Integer, Long>comparingByKey())
						)
				.collect(
						Collectors.toMap(
								Map.Entry::getKey, 
								Map.Entry::getValue, 
								(oldValue, newValue) -> oldValue, 
								LinkedHashMap::new)
						);

4. Trivia :

Related Articles :

References:

Happy Coding !!
Happy Learning !!

Java 8 - How to find common & uncommon elements from 2 Lists ?
Java 8 - Find duplicate count from Integer arrays ?