Crunchify

How to Sort a HashMap by Key and Value in Java 8 – Complete Tutorial

In Java 8 – How to sort a Map?

On Crunchify we have written almost ~400 java tutorials and this one is an addition to Java8 category.

I love Java collection and have multiple tutorials on How to iterate through Map and List, LinkedList, JSONArray and lot more.

In this tutorial we will go over Best way to sort HashMap by Key and Value in Java8.

Let’s get started:

  1. We will create class CrunchifySortMapByKeyValueJava8.java
  2. Create HashMap<String, Integer> crunchifyMap and that’s what we will use for sort by Key and Value.
  3. For KEY: we are going to add random company from list
    • Patter: Random Number between 1 to 10 + (-) + 1 company from list
    • companies list: crunchify.com, google.com, twitter.com
  4. For VALUE:  we are going to add one random number between 1 to 50
  5. We will print original Map, Sorted by Key Map and Sorted by Value Map

Map.Entry.comparingByKey() returns a comparator that compares Map.Entry in natural order on key.

Map.Entry.comparingByValue() returns a comparator that compares Map.Entry in natural order on value.

Here is a complete Java code:

Please take a look at two questions mentioned in below code carefully 🙂 These are simple utilities just incase if you want to use it in your project.

package crunchify.com.tutorial;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;
import java.util.stream.Stream;

/**
 * @author Crunchify.com
 * 
 *         Best way to sort HashMap by Key and Value in Java8 - Tutorial by App Shah
 * 
 */

public class CrunchifySortMapByKeyValueJava8 {

	private static Random crunchifyRandom = new Random();

	public static void main(String[] argv) {

		Map<String, Integer> crunchifyMap = new HashMap<>();

		// Let's first create companies ArrayList
		ArrayList<String> companies = new ArrayList<>();
		companies.add("Crunchify.com");
		companies.add("Google.com");
		companies.add("Twitter.com");

		// Let's add 10 entries to HashMap crunchifyMap
		for (int i = 1; i <= 10; ++i) {

			// How to get Random value from ArrayList?
			String company = companies.get(crunchifyRandom.nextInt(companies.size()));

			// Basically we are creating
			// Key with pattern: 1-Crunchify, 5-Google, and so on...
			// Random Value: Number between 1 to 50
			crunchifyMap.put(crunchifyRandom.nextInt(10) + "-" + company, crunchifyRandom.nextInt(50));
		}

		log("~~~~~~~~~~~~~~Original HashMap (crunchifyMap value)~~~~~~~~~~~~~~");
		log(crunchifyMap);

		log("\n~~~~~~~~~~~~~~Updated HashMap after Sorting by Key~~~~~~~~~~~~~~");
		Map<String, Integer> key = crunchifySortByKey(crunchifyMap);
		iterateThroughHashMapJava8(key);

		log("\n~~~~~~~~~~~~~~Updated HashMap after Sorting by Value~~~~~~~~~~~~~~");
		Map<String, Integer> value = crunchifySortByValue(crunchifyMap);
		iterateThroughHashMapJava8(value);

	}

	// Simple Log Statement
	private static void log(Object string) {
		System.out.println(string);
	}

	// How to Iterate through HashMap in Java 8?
	private static void iterateThroughHashMapJava8(Map<String, Integer> crunchifyMap) {
		crunchifyMap.forEach((k, v) -> {
			System.out.println("Key: " + k + "\t\t Value: " + v);
		});

	}

	// Let's sort HashMap by Key
	public static <K extends Comparable<? super K>, V> Map<K, V> crunchifySortByKey(Map<K, V> crunchifyMap) {

		Map<K, V> crunchifyResult = new LinkedHashMap<>();
		Stream<Map.Entry<K, V>> sequentialStream = crunchifyMap.entrySet().stream();

		// comparingByKey() returns a comparator that compares Map.Entry in natural order on key.
		sequentialStream.sorted(Map.Entry.comparingByKey()).forEachOrdered(c -> crunchifyResult.put(c.getKey(), c.getValue()));
		return crunchifyResult;
	}

	// Let's sort HashMap by Value
	public static <K, V extends Comparable<? super V>> Map<K, V> crunchifySortByValue(Map<K, V> crunchifyMap) {

		Map<K, V> crunchifyResult = new LinkedHashMap<>();
		Stream<Map.Entry<K, V>> sequentialStream = crunchifyMap.entrySet().stream();

		// comparingByValue() returns a comparator that compares Map.Entry in natural order on value.
		sequentialStream.sorted(Map.Entry.comparingByValue()).forEachOrdered(c -> crunchifyResult.put(c.getKey(), c.getValue()));
		return crunchifyResult;
	}

}

Eclipse Console Output:

~~~~~~~~~~~~~~Original HashMap (crunchifyMap value)~~~~~~~~~~~~~~
{9-Google.com=36, 6-Twitter.com=17, 3-Google.com=39, 2-Twitter.com=43, 5-Crunchify.com=2, 8-Google.com=5, 8-Crunchify.com=47, 5-Google.com=10, 7-Google.com=3}

~~~~~~~~~~~~~~Updated HashMap after Sorting by Key~~~~~~~~~~~~~~
Key: 2-Twitter.com		 Value: 43
Key: 3-Google.com		 Value: 39
Key: 5-Crunchify.com	 Value: 2
Key: 5-Google.com		 Value: 10
Key: 6-Twitter.com		 Value: 17
Key: 7-Google.com		 Value: 3
Key: 8-Crunchify.com	 Value: 47
Key: 8-Google.com		 Value: 5
Key: 9-Google.com		 Value: 36

~~~~~~~~~~~~~~Updated HashMap after Sorting by Value~~~~~~~~~~~~~~
Key: 5-Crunchify.com	 Value: 2
Key: 7-Google.com		 Value: 3
Key: 8-Google.com		 Value: 5
Key: 5-Google.com		 Value: 10
Key: 6-Twitter.com		 Value: 17
Key: 9-Google.com		 Value: 36
Key: 3-Google.com		 Value: 39
Key: 2-Twitter.com		 Value: 43
Key: 8-Crunchify.com	 Value: 47

Join the Discussion

Share & leave us some comments on what you think about this topic or if you like to add something.

Buffer