Examples of Converting List to Map using Streams

Learn different ways of using Java Streams API to Convert a List of elements to a Map.

Prepare the Input List

Let’s consider you have a User class and a List of users you want to convert to Map.

User.java

public class User {
  private Long id;
  private String name;
  private String lastName;
  private int age;

  // Getter, Setter, and Constructors
}Code language: Java (java)

Create a List of User objects

List<User> users = List.of(
  new User(1l, "Bob", "Taylor", 30),
  new User(2l, "Tom", "Richard", 32),
  new User(3l, "Bob", "Wills", 31),
  new User(4l, "Nick", "Jackson", 29),
  new User(5l, "john", "Anderson", 31),
  new User(6l, "Tom", "Taylor", 30),
  new User(7l, "Bob", "Mills", 29)
);Code language: Java (java)

In the below sections, we will use Streams to convert the above List into different types of Maps.

Convert List to Simple Map using Streams – toMap() Collector

The Java Stream Collectors.toMap() is a convenient method for creating maps. The method accepts two Functions – the first for mapping the key and the other for mapping the value.

Here, were are streaming through a List of User objects and creating a Map of their Id to the last names.

Map<Long, String> map = users.stream()
    .collect
        (Collectors.toMap(User::getId, User::getLastName));Code language: Java (java)

Here, instead of providing implementations of the Function interface, we use Method reference notation to simplify the toMap() invocation.

As expected, the resulting map has the correct key and value mapping.

1=Taylor 
2=Richard 
3=Wills 
4=Jackson 
5=Anderson 
6=Taylor 
7=MillsCode language: plaintext (plaintext)

Convert List<K> to Map<String, K> Using Streams – toMap() Collectors

We saw how to convert a List to a simple map. However, instead of putting just the last name into the value, we may want to store the entire User object as a value. To do that, we can use Function.identity() function.

Let’s group the users into a Map by their Id fields.
List<User> ——-> Map<Long, User>

Map<Long, User> map = users.stream()
    .collect
        (Collectors.toMap(User::getId, Function.identity()));Code language: Java (java)

Now, we get a Map of the user Id to the user instance.

1=[id: 1, name: Bob, last name: Taylor, age: 30]
2=[id: 2, name: Tom, last name: Richard, age: 32]
3=[id: 3, name: Bob, last name: Wills, age: 31]
4=[id: 4, name: Nick, last name: Jackson, age: 29]
5=[id: 5, name: john, last name: Anderson, age: 31]
6=[id: 6, name: Tom, last name: Taylor, age: 30]
7=[id: 7, name: Bob, last name: Mills, age: 29]Code language: plaintext (plaintext)

How to group by a Field using Stream – groupingBy() Collector

Our previously created Maps had a single value. That is because we grouped them using a unique key. However, some keys can have multiple values – for example, grouping a list of users based on their first names. To do so, we can use the groupingBy() collector that takes the key mapper. The value is a list of grouped elements.

Let’s convert a List of User objects to a Map of user names to a list of users.

List<Users> ————> Map<String, List<User>>

Map<String, List<User>> groupedByName = users.stream()
    .collect
        (Collectors.groupingBy(User::getName));Code language: Java (java)

The outcome is as expected.

Tom=[[id: 2, name: Tom, last name: Richard, age: 32], [id: 6, name: Tom, last name: Taylor, age: 30]]
Bob=[[id: 1, name: Bob, last name: Taylor, age: 30], [id: 3, name: Bob, last name: Wills, age: 31], [id: 7, name: Bob, last name: Mills, age: 29]]
Nick=[[id: 4, name: Nick, last name: Jackson, age: 29]]
john=[[id: 5, name: john, last name: Anderson, age: 31]]Code language: plaintext (plaintext)

Summary

This example-oriented tutorial discussed converting Java Lists to Maps using Java Streams API. It first demonstrated toMap() function to convert a List of objects to a Map of a specific key and value pairs. Then it used the Function.identity() with the toMap() collector to create a Map of a particular key and the element as value. Lastly, it demonstrated groupingBy() collector to create a Map of a non-unique key and element list as the value.


One thought on “Examples of Converting List to Map using Streams

  1. Hello there,

    My name is George, and I was wondering if you would like to have your website amitph.com promoted as a resource on my blog georgemartjr.com ?

    We are updating our broken link resources to include up to date resources for our readers. Our resource links are manually approved as a do follow link.
    If you are interested in having your site included as a resource on our blog, please let me know.

    Thanks for your consideration,
    George

Comments are closed.