Use Java streams to convert list of objects to a map

 Java 8 introduced the concept of Streams. Streams is an alternative way of iterating over a collection. Below sample code will show how to convert a list of objects to a key-value map.

Let's assume the objects to be:

Below code shows how to use streams to convert a List of Person objects to a Map with key as Id and values as Person object.

Above code can be verifies with below test.


Post a Comment