Java 8 Streams - Creating a Map from a Collection, List, or Set

Java 8 Streams – Create a map from a collection, list or set

Preamble

With the introduction of streams in Java 8 the writing of Java code has changed. With Java 8 you can now transfer objects from a list to a map in a comfortable way. How to implement this I will show you in this tutorial.

Requirement

Java knowledge

Convert Collection, List or Set to Map with the Java 8 Streaming API

As a developer you sometimes face the problem that objects of a collection, list or set have to be transferred into a map. There are different reasons for this. One reason might be that the access to the elements of a map is much faster than the access to the elements of a list. In this case it makes sense to transfer the collection, list or set into a map in advance.

In this tutorial we assume that we have a list of the class Customer.java and that it has the properties id, firstname, lastname.

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

packagede.ertantoker.tutorial.model;

publicclassCustomer{

privateStringid;

privateStringfirstname;

privateStringlastname;

publicCustomer(Stringid,Stringfirstname,Stringlastname){

this.id=id;

this.firstname=firstname;

this.lastname=lastname;

}

publicStringgetId(){

returnid;

}

publicvoidsetId(Stringid){

this.id=id;

}

publicStringgetFirstname(){

returnfirstname;

}

publicvoidsetFirstname(Stringfirstname){

this.firstname=firstname;

}

publicStringgetLastname(){

returnlastname;

}

publicvoidsetLastname(Stringlastname){

this.lastname=lastname;

}

}

With the Streamin API of Java 8 we can use streams and at the end of the processing we can transfer them via collectors into a target structure suitable for us. In this example we want to convert from a list to a map.

The following code shows how to transfer a List<Customer> to Map<String, Customer>.

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

packagede.ertantoker.tutorial;

importde.ertantoker.tutorial.model.Customer;

importjava.util.Arrays;

importjava.util.List;

importjava.util.Map;

importjava.util.stream.Collectors;

publicclassJava8ListToMap{

publicstaticvoidmain(String[]args){

Customer customer1=newCustomer("1","Albert","Einstein");

Customer customer2=newCustomer("2","Otto","Hahn");

Customer customer3=newCustomer("3","Isaac","Newton");

List customerList=Arrays.asList(customer1,customer2,customer3);

Map<String,Customer>customerMap=customerList

.stream()

.collect(Collectors.toMap(Customer::getId,customer->customer));

}

}

The Result

We have now learned how to convert lists or sets into a map with Java 8 and Streams Collection. With the collector classes it is also possible to convert the list into a set or to convert the objects in the list into another class. The Streams API offers a lot of possibilities. How to convert the objects of a list into another class you can see in my next tutorial Java 8 Stream Object mapping.

I hope you enjoyed my little tutorial. If you should have questions, criticism or suggestions, then I would be pleased, if you would leave a comment.

You always wanted to create RESTful API’s that store their data in a database? In this tutorial I will show you how you can use Spring Boot, Java Persistence API (JPA) and MySQL to create a small RESTful API that stores your data in a local MySQL database.