You say you want to sort by value, but you don't have that in your code. Pass a lambda (or method reference) to sorted to tell it how you want to sort.

And you want to get the keys; use map to transform entries to keys.

List<Type> types = countByType.entrySet().stream()
        .sorted(Comparator.comparing(Map.Entry::getValue))
        .map(Map.Entry::getKey)
        .collect(Collectors.toList());
Answer from Jesper on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › stream-map-java-examples
Stream map() in Java with examples - GeeksforGeeks
January 4, 2025 - // Java code for Stream map(Function mapper) // to get a stream by applying the // given function to this stream. import java.util.*; import java.util.stream.Collectors; class GFG { // Driver code public static void main(String[] args) { ...
Discussions

collections - Java 8 streams - collect and stream map of lists - Stack Overflow
I have a Map of Lists, and would like to collect all the values and then stream on the collected List, so given:- Map effectiveDatePublicationM... More on stackoverflow.com
🌐 stackoverflow.com
[Java 8] Help converting a nested list of objects into a map using lambdas/streams
This is actually pretty straightforward as far as streams go. The methods you'll want to use are Stream.flatMap and Stream.collect with Collectors.groupingBy. More on reddit.com
🌐 r/learnprogramming
2
1
June 28, 2018
Java 8 functional alternative to for loop break
The filter() method (from the Stream interface) along with the findFirst() method would solve your need. More on reddit.com
🌐 r/javahelp
6
7
March 6, 2015
Proposal for stream: a (casting) filter by class
It can be "simplified" with .filter(Circle.class::isInstance) and .map(Circle.class::cast) ;) More on reddit.com
🌐 r/java
8
5
October 12, 2016
🌐
Baeldung
baeldung.com › home › java › java streams › collecting stream elements into a list in java
Collecting Stream Elements into a List in Java | Baeldung
December 3, 2025 - We also know that the Stream API provides the implementation only for the toList() method. It doesn’t contain similar methods for getting a map or a set. So if we want a uniform approach to get any converters, like list, map, or set, we’ll ...
🌐
Medium
medium.com › @AlexanderObregon › javas-stream-map-method-explained-df0d0d461d39
Java’s Stream.map() Method Explained | Medium
August 27, 2024 - For instance, let's say you have a list of Employee objects, and you want to extract a specific field, such as the employee's name or age. With map(), you can create a new stream that contains only the extracted data. Consider the following example where we extract employee names from a list of Employee objects: import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; class Employee { private String name; private int age; public Employee(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return a
🌐
Baeldung
baeldung.com › home › java › java collections › java map › how to convert list to map in java
How to Convert List to Map in Java | Baeldung
April 4, 2025 - Learn several ways to convert a List into a Map using Custom Suppliers. ... Learn how to convert a List to a String using different techniques. ... How to convert between a List and a Set using plain Java, Guava or Apache Commons Collections.
Find elsewhere
🌐
Marc Nuri
blog.marcnuri.com › home › java streams: convert list into map
Java Streams: Convert List into Map - Marc Nuri
January 27, 2026 - When you're certain that the List contains no duplicate keys, use the basic two-argument version of Collectors.toMap(). ... The first step is to convert the list into a Stream and then collect the results using Collectors.toMap(). For the Map ...
🌐
Coderanch
coderanch.com › t › 680277 › java › Creating-map-maps-list-Java
Creating a map of maps from a list using Java 8 streams [Solved] (Java in General forum at Coderanch)
The key of the first map is MonitoredData::getDayOfYear and the key to the second map is MonitoredData::getActivityLabel. The value of the second data should be the number of times the certain activity has been found in that specific day. Thanks in advance, tell me if there's something else ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › stream › Stream.html
Stream (Java Platform SE 8 )
July 21, 2026 - Returns an IntStream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Each mapped stream is closed after its contents have been placed into this stream.
🌐
Mkyong
mkyong.com › home › java8 › java 8 – convert list to map
Java 8 – Convert List to Map | mkyong.com
May 26, 2017 - Map<String, Long> result1 = list.stream().collect( Collectors.toMap(Hosting::getName, Hosting::getWebsites)); System.out.println("Result 1 : " + result1); } } Output – The error message below is a bit misleading, it should show “linode” instead of the value of the key. Exception in thread "main" java.lang.IllegalStateException: Duplicate key 90000 at java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133) at java.util.HashMap.merge(HashMap.java:1245) //... 2.2 To solve the duplicated key issue above, pass in the third mergeFunction argument like this : Map<String, Long> result1 = list.stream().collect( Collectors.toMap(Hosting::getName, Hosting::getWebsites, (oldValue, newValue) -> oldValue ) ); Output ·
🌐
GeeksforGeeks
geeksforgeeks.org › java › conversion-of-java-maps-to-list
Conversion of Java Map to List - GeeksforGeeks
July 11, 2025 - We can stream the map key or values and collect them into a list. Example: This example demonstrates how to convert the keys and values of a Map into separate List object using Java Streams.
🌐
Baeldung
baeldung.com › home › java › java list › create list of object from another type using java
Create List of Object From Another Type Using Java | Baeldung
March 7, 2025 - As the code above shows, preparing an empty list for TennisPlayerCandidate objects is unnecessary. The filter().map() pipeline delivers a Stream of TennisPlayerCandidate instances. What we need to do is just collect the objects into a list.
🌐
How to do in Java
howtodoinjava.com › home › collections framework › convert list to map in java: tomap(), duplicate keys and order
Convert List to Map in Java: toMap(), Duplicate Keys and Order
September 23, 2022 - List<String> names = Arrays.asList("ann", null, "bo"); Map<String, Integer> broken = names.stream().collect(Collectors.toMap(n -> String.valueOf(n), n -> n == null ? null : n.length())); // NullPointerException Map<String, Integer> safe = names.stream().filter(Objects::nonNull).collect(Collectors.toMap(Function.identity(), String::length)); Integer bo = safe.get("bo"); // 2 · When the map must not change after we build it, Java 10’s Collectors.toUnmodifiableMap() returns an unmodifiable map.
🌐
Blogger
javarevisited.blogspot.com › 2016 › 04 › 10-examples-of-converting-list-to-map.html
10 Examples of Converting a List to Map in Java 8
private Map<String, Choice> toMap(List books) { final Map hashMap = new HashMap<>(); for (final Book book : books) { hashMap.put(book.getISBN(), book); } return hashMap; } You can see we have iterated through the List using enhanced for loop of Java 5 and put each element into a HashMap, where ISBN code is the key, and the book object itself is the value. This is the best way to convert a List to Map in pre-JDK 8 worlds. It's clear, concise, and self-explanatory, but iterative. Now, let's see how we can do the same in Java 8 by using lambda expression and Stream API, here is my first attempt:
🌐
Reverse Coding
reversecoding.net › java-8-list-to-map
Java 8 Stream - From List to Map | Reverse Coding
June 4, 2016 - We just need to change the two parameters of the Collectors.toMap, by specifying: //Name as key person -> person.getName() //Age as value person -> person.getAge(); ... List<Person> people = Arrays.asList( new Person("mario@reversecoding.net", "Mario", 27), new Person("luigi@reversecoding.net", "Luigi", 30), new Person("steve@reversecoding.net", "Steve", 20) ); Map<String, Integer> mapNameAge = people.stream() .collect(Collectors.toMap(person -> person.getName(), person -> person.getAge())); System.out.println(mapNameAge); //{Steve=20, Luigi=30, Mario=27}
🌐
Baeldung
baeldung.com › home › java › java collections › converting list to map with a custom supplier
Converting List to Map With a Custom Supplier | Baeldung
November 13, 2025 - In this tutorial, we’re going to convert a List<E> into a Map<K, List<E>>. We’ll achieve this with Java’s Stream API and the Supplier functional interface.
🌐
Medium
medium.com › geekculture › java-streams-collect-to-map-of-lists-with-grouping-by-8cb309df4598
Java Streams: Collect to Map of Lists with Grouping By | Geek Culture
May 7, 2022 - How to collect a stream into a map of lists, to avoid duplicate key exception, using the Collectors . groupingBy method method reference lambda functions
🌐
JRebel
jrebel.com › blog › using-java-stream-map-and-java-stream-filter
Using Java 8 Stream Map and Java 8 Stream Filter | JRebel
June 25, 2025 - Further in this post we'll show how to filter a list or set a Java 8 stream filter and how to convert a stream to a regular set or a list.Back to top · The new java.util.stream package has been added to JDK which allows us to perform filter/map/reduce-like operations with the Java 8 collections stream.
🌐
Medium
medium.com › @kiarash.shamaii › list-map-vs-stream-map-in-java-44440284386
Medium
August 28, 2024 - Mutability: You can modify the list during iteration, but it’s generally not recommended as it can lead to ConcurrentModificationException.: List<String> list = Arrays.asList("a", "b", "c"); list.forEach(System.out::println); Functional Programming: The Stream API promotes a functional style of programming, allowing for operations like filtering, mapping, and reducing.