Convert Map<String,List<Person>> to Map<String,List<Employee>>, using Java 8 streams. I did this but how do this without for loop

Masoom Raza :

The for loop in the code is need to be replaced by java8 streams. How can I resolve this?

public class Conversion {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        //Person class (name, age , gender)
        //Employee class (name, gender)
        Map<String, List<Person>> mapPerson = new HashMap<String, List<Person>>();
        Map<String, List<Employee>> employeeMap = new HashMap<>();

        List<Person> personList1 = new ArrayList<Person>();
        personList1.add(new Person("Adam", 22, "Male"));
        personList1.add(new Person("Alon", 21, "Female"));

        List<Person> personList2 = new ArrayList<Person>();
        personList2.add(new Person("Chad", 23, "Male"));
        personList2.add(new Person("Daina", 21, "Female"));

        List<Person> personList3 = new ArrayList<Person>();
        personList3.add(new Person("Mark", 22, "Female"));
        personList3.add(new Person("Helen", 25, "Male"));

        mapPerson.put("A", personList1);
        mapPerson.put("B", personList2);
        mapPerson.put("C", personList3);

        for (Map.Entry<String, List<Person>> entry : mapPerson.entrySet()) {
            String key = entry.getKey();
            List<Person> values = entry.getValue();
            System.out.println("Key = " + key);
            System.out.println("Values = " + values + "n");
        }
        System.out.println("******************************************");
        employeeMap = getEmployeeMap(mapPerson);

        for(Map.Entry<String, List<Employee>> listMap : employeeMap.entrySet())
        {
            System.out.println("Key : " + listMap.getKey());
            System.out.println("Values : " + listMap.getValue());
            System.out.println("===============");
        }
    }

This is the for loop in the function for iteration of MAP. Instead of this I have to use streams.

    public static Map<String, List<Employee>> getEmployeeMap(Map<String, List<Person>> personMap) {

        Map<String, List<Employee>> employeeMap = new HashMap<>();

        for(Map.Entry<String, List<Person>> listMap : personMap.entrySet()) {
            String key = listMap.getKey();
            List<Person> personList = listMap.getValue();   
            List<Employee> employeeList = personList.stream().map(p-> new Employee(p.getName(), p.getGender())).collect(Collectors.toList());

            employeeMap.put(key, employeeList);
        }       
        return employeeMap;
    }   
}
Eklavya :

You can use Collectors.toMap and modify the value inside it.

Map<String, List<Employee>> employeeMap =
    mapPerson.entrySet().stream()
   .collect(Collectors.toMap(Map.Entry::getKey,
        e -> e.getValue().stream().map(p-> new Employee(p.getName(), p.getGender())).collect(Collectors.toList()));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I convert a List<Employee> to Map<Employee, List<String>> using java8?

Using Java 8 streams how do I convert Map<String, List<Something>> to Map<String, Integer>?

Using Java 8 streams, how to transform Map<String, Map<String, List<Person>>> to Map<Integer, Map<String, Integer>>?

How to convert List<Person> into Map<String,List<Employee>>

Convert List<Map<String, Object>> to Map<String, List<Map<String, Object>>> using Java 8 streams API

How do you convert Map<String, Person> to Map<String, Integer> in java streams inside collect?

convert List<E> to Map<String, List<String>> using java 8 streams

Java 8 streams, convert List of object to Map<String, Set<String>>

How to convert Map<String, List<String>> to Set<String> using streams?

Java-8 Streams: Convert Map<String, List<List<DataType>>> to Map<String, List<DataType>>

Java-8 Streams: Convert List<{String,List<String>}> to Map<String, List<String>>

Java 8: How to convert List<String> to Map<String,List<String>>?

Java 8 stream: how to Convert Map<String, List<Integer>> to Map<Integer, List<String>> using groupingBy(.)

How to convert List of Map into Map of List using Java Streams

Java 8 convert Map<Department, List<Person>> to Map<Department, List<String>>

Convert a Collection to Map<String,Collection<String>> using java 8 streams

Create a map of String and sorted list using Java 8 streams API

How to convert List<Object[]> to Map<String,BigInteger> with Streams & Lambda Java8

How to convert List<Person> to Map<String, List<Double>> instead of Map<String, List<Person>>?

How do i convert a String to a List<Map<String,String>>

Convert List of Strings into Map using Java-8 Streams API

convert list of object into a map of <String,Map<String,Integer>> java8 streams

How to convert List<AnyClassObject> to List<Map<String, String>> using Stream in java 8?

Java 8 Convert List of List of String to Map

How do I convert a List<Entry> to Map where value is a list using streams?

How to Convert a Map<String, List<String>> to Map<String, String> in java 8

How to convert List<V> into Map<K, List<V>>, with Java 8 streams and custom List and Map suppliers?

How to map elements of the list to their indices using Java 8 streams?

Convert a List<String> to Map<String, String> using the list values in Java 8