How to sort Map by Key or Value in Java 8


A Map can also be sorted with stream.sorted() method in Java 8. We can sort the Map based on its Key or Value.

But before calling sorted() method, we have to convert the Map into a Stream and then we can sort it.

Similar Post-

1. How to convert Map into Stream?

It’s so simple, by calling entrySet() method on the Map object and it returns a Set view of the mappings contained in this map. After that, we can call the stream() method which returns a sequential Stream with this collection as its source.

map.entrySet().stream();

2. Sort by Key

Sorting a Map object by its keys.

package org.websparrow.sorting;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

public class MapSortByKey {

	public static void main(String[] args) {

		Map<Integer, String> map = new HashMap<>();
		map.put(114, "Prince");
		map.put(99, "Sandeep");
		map.put(201, "Gaurav");
		map.put(4, "Abhinav");
		map.put(299, "Pallavi");

		System.out.println("--- Unsorted Map ---");
		System.out.println(map);

		System.out.println("--- Sorted Map (Sorted by Key) ---");
		map.entrySet()
				.stream()
				.sorted(Map.Entry.comparingByKey())
				.forEach(System.out::println);

		// Another way to sort a Map by keys
		System.out.println("--- Sorted Map (Sorted by Key) ---");
		map.entrySet()
				.stream()
				.sorted(Comparator.comparing(Map.Entry::getKey))
				.forEach(System.out::println);

	}
}

Output

--- Unsorted Map ---
{114=Prince, 99=Sandeep, 4=Abhinav, 201=Gaurav, 299=Pallavi}
--- Sorted Map (Sorted by Key) ---
4=Abhinav
99=Sandeep
114=Prince
201=Gaurav
299=Pallavi
--- Sorted Map (Sorted by Key) ---
4=Abhinav
99=Sandeep
114=Prince
201=Gaurav
299=Pallavi

Alternatively, we can also call collect(Collectors.toMap())  method which returns HashMap by default and we need LinkedHashMap to keep the order.

Map<Integer, String> sortedMap = map.entrySet().stream()
		.sorted(Map.Entry.comparingByKey())
		.collect(Collectors.toMap(Map.Entry::getKey,
				Map.Entry::getValue, (oldValue, newValue) -> oldValue,
				LinkedHashMap::new));

System.out.println(sortedMap);

Output

{4=Abhinav, 99=Sandeep, 114=Prince, 201=Gaurav, 299=Pallavi}

3. Sort by Value

Similarly, we can also sort the Map object by its values.

package org.websparrow.sorting;

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

public class MapSortByValue {

	public static void main(String[] args) {

		Map<Integer, String> map = new HashMap<>();
		map.put(114, "Prince");
		map.put(99, "Sandeep");
		map.put(201, "Gaurav");
		map.put(4, "Abhinav");
		map.put(299, "Pallavi");

		System.out.println("--- Unsorted Map ---");
		System.out.println(map);

		System.out.println("--- Sorted Map (Sorted by Value) ---");
		map.entrySet()
				.stream()
				.sorted(Map.Entry.comparingByValue())
				.forEach(System.out::println);

		// Another way to sort a Map by values
		System.out.println("--- Sorted Map (Sorted by Value) ---");
		map.entrySet()
				.stream()
				.sorted(Comparator.comparing(Map.Entry::getValue))
				.forEach(System.out::println);

	}
}

Output

--- Unsorted Map ---
{114=Prince, 99=Sandeep, 4=Abhinav, 201=Gaurav, 299=Pallavi}
--- Sorted Map (Sorted by Value) ---
4=Abhinav
201=Gaurav
299=Pallavi
114=Prince
99=Sandeep
--- Sorted Map (Sorted by Value) ---
4=Abhinav
201=Gaurav
299=Pallavi
114=Prince
99=Sandeep

Alternatively

Map<Integer, String> sortedMap = map.entrySet().stream()
		.sorted(Map.Entry.comparingByValue())
		.collect(Collectors.toMap(Map.Entry::getKey,
				Map.Entry::getValue, (oldValue, newValue) -> oldValue,
				LinkedHashMap::new));

System.out.println(sortedMap);

Output

{4=Abhinav, 201=Gaurav, 299=Pallavi, 114=Prince, 99=Sandeep}

References

  1. Java 8 – How to sort Set with stream.sorted()
  2. Interface Stream<T>
  3. Stream<T> sorted()
  4. Stream<T> sorted(Comparator<? super T> comparator)
  5. Interface Comparator<T>
  6. Collectors.toMap()

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.