Site icon

Convert list of objects to /from map in java 8 lambda stream (example)

1. Convert list of objects to/from map in java8 lambda stream

  1. Convert list of objects to Map <Key,Value>
    • We will convert List<Person> to Map<String,Person>
  2. Convert Map<Key,Value> to list of objects.
    1. Convert Map<String,Person> to List<String>
      • Convert map of key value pairs to List of Keys
    2. Convert Map<String,Person> to List<Person>
      • Convert map of key value pairs to List of Values

2. Program – convert list of objects to/from map in java8 (lambda streams)

2.1.) Person Class:

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:

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]]
Exit mobile version