- Given the list of objects, convert list of objects to map.
- We will use lambda streams to convert list objects to map & vice versa.
- We will create Person class & we will convert list of person objects to/from map.
1. Convert list of objects to/from map in java8 lambda stream
- Convert list of objects to Map <Key,Value>
- We will convert List<Person> to Map<String,Person>
- Convert Map<Key,Value> to list of objects.
- Convert Map<String,Person> to List<String>
- Convert map of key value pairs to List of Keys
- Convert Map<String,Person> to List<Person>
- Convert map of key value pairs to List of Values
- Convert Map<String,Person> to List<String>
2. Program – convert list of objects to/from map in java8 (lambda streams)
2.1.) Person Class:
- Person class containing required data members is as follows.
public class Person { private String firstName; private String lastName; private String uniqueId; public Person(String firstName, String lastName, String uniqueId) { this .firstName = firstName; this .lastName = lastName; this .uniqueId = uniqueId; } public String toString() { return "[" + firstName + " " + lastName + " " + uniqueId + "]" ; } public String getUniqueId() { return uniqueId; } public void setUniqueId(String uniqueId) { this .uniqueId = uniqueId; } } |
2.2. ListMapConversion Class:
- ListMapConversion class is responsible for following operations
- Convert List<Person> to Map<String,Person>
- Convert Map<String,Person> to List<String> (List of keys).
- Convert Map<String,Person> to List<Person> (List of values).
package org.learn.ConvertListToMap; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class ListMapConversion { public static void main( String[] args ) { List <Person> personList = new ArrayList<Person>(); personList.add( new Person( "Mike" , "harvey" , "100268" )); personList.add( new Person( "Nick" , "young" , "32654" )); personList.add( new Person( "Jack" , "slater" , "123642" )); System.out.println( "1. Convert List of objects to map :" ); Map<String, Person> mapIdToPerson = convertListToMap(personList); System.out.println( "\n2. Convert Map to list of objects :" ); convertMapToList(mapIdToPerson); } private static Map<String, Person> convertListToMap(List <Person> personList) { Map<String, Person>mapIdToPerson = null ; mapIdToPerson = personList.stream() .collect( Collectors .toMap( Person::getUniqueId, person->person) ); System.out.println(mapIdToPerson); return mapIdToPerson; } private static void convertMapToList(Map<String, Person>mapIdToPerson) { System.out.println( "2.1. List of keys :" ); List<String>uniqueIdList = new ArrayList<>(mapIdToPerson.keySet()); System.out.println(uniqueIdList); System.out.println( "\n2.2. List of person objects :" ); List<Person>personList = new ArrayList<>(mapIdToPerson.values()); System.out.println(personList); } } |
3. Output – convert list of objects to/from map in java (lambda streams)
1. Convert List of objects to map : {32654=[Nick young 32654], 123642=[Jack slater 123642], 100268=[Mike harvey 100268]} 2. Convert Map to list of objects : 2.1. List of keys : [32654, 123642, 100268] 2.2. List of person objects : [[Nick young 32654], [Jack slater 123642], [Mike harvey 100268]] |