简体   繁体   English

转换 Map <string,list<person> &gt; 至 Map <string,list<employee> &gt;,使用 Java 8 个流。 我这样做了,但是没有 for 循环怎么做</string,list<employee></string,list<person>

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

The for loop in the code is need to be replaced by java8 streams.代码中的 for 循环需要替换为 java8 流。 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.这是 function 中的 for 循环,用于 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;
    }   
}

You can use Collectors.toMap and modify the value inside it.您可以使用Collectors.toMap并修改其中的值。

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()));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何转换列表<Employee>到地图<Employee, List<String> &gt; 使用 java8? - How do I convert a List<Employee> to Map<Employee, List<String>> using java8? 使用Java 8流,如何转换Map <String, Map<String, List<Person> &gt;&gt;到地图 <Integer, Map<String, Integer> &gt;? - Using Java 8 streams, how to transform Map<String, Map<String, List<Person>>> to Map<Integer, Map<String, Integer>>? 如何生成列表的 Map 的 Map(地图<string, map<enum, list<string> &gt;&gt;) 在 java 中使用流</string,> - How do I generate a Map of Map of List (Map<String, Map<Enum, List<String>>>) in java using streams 转换清单 <E> 到地图 <String, List<String> &gt;使用Java 8流 - convert List<E> to Map<String, List<String>> using java 8 streams 你如何转换地图 <String, Person> 到地图 <String, Integer> 在java流里面收集? - How do you convert Map<String, Person> to Map<String, Integer> in java streams inside collect? 如何转换地图<String, List<String> &gt; 设置<String>使用流? - How to convert Map<String, List<String>> to Set<String> using streams? 如何转换清单 <Object> 到地图 <String , List<String> &gt;使用Java 8流? - How to convert List<Object> to Map<String , List<String>> using java 8 streams? 转换 HttpServletRequest.getParameterMap() [Map<string, string[]> ] 至 Map <string, list<string> &gt; 使用 Java 流</string,></string,> - Convert HttpServletRequest.getParameterMap() [Map<String, String[]>] to Map<String, List<String>> using Java Streams 如何转换List <Person> 到地图 <String, List<Double> &gt;而不是地图 <String, List<Person> &gt;? - How to convert List<Person> to Map<String, List<Double>> instead of Map<String, List<Person>>? 如何转换清单 <DataObject> 到地图 <Integer, String> 使用流 - How to convert List<DataObject> to Map<Integer, String> using streams
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM