【问题标题】:Java 8 convert Map<Department, List<Person>> to Map<Department, List<String>>Java 8 将 Map<Department, List<Person>> 转换为 Map<Department, List<String>>
【发布时间】:2015-03-20 05:35:31
【问题描述】:

使用Collectors.groupingBy() 我可以轻松获得Map&lt;Department, List&lt;Person&gt;&gt; - 这为我提供了属于Department 一部分的所有Person 对象:

allPersons.stream().collect(Collectors.groupingBy(Person::getDepartment));

现在我想转换生成的“multimap”,使其包含所有 Persons 的名称,而不是 Person 对象。

实现此目的的一种方法是:

final Map<Department, List<String>> newMap = new HashMap<>();
personsByDepartmentMap.stream
    .forEach((d, lp) -> newMap.put(
         d, lp.stream().map(Person::getName).collect(Collectors.toList())));

有没有办法在不使用newMap 对象的情况下实现这一点? 类似的东西

final Map<Department, List<String>> newMap = 
                personsByDepartmentMap.stream().someBigMagic();

【问题讨论】:

    标签: dictionary collections java-8 java-stream


    【解决方案1】:

    您可以使用转换地图

    Map<Department, List<String>> result =
        personsByDepartmentMap.entrySet().stream()
          .map(e -> new AbstractMap.SimpleImmutableEntry<>(
            e.getKey(),
            e.getValue().stream().map(Person::getName).collect(Collectors.toList())))
      .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    

    这段代码显然没有标准的Pair类型,但你可以通过使用static imports来改进它。

    (更新:)作为Brian Goetz has shown,您可以在这种特定情况下通过将映射和集合合并为一个步骤来解决它,例如

    Map<Department, List<String>> result =personsByDepartmentMap.entrySet().stream()
        .collect(Collectors.toMap(
            Map.Entry::getKey,
            e->e.getValue().stream().map(Person::getName).collect(Collectors.toList())));
    

    但是,我仍然认为通过一次操作从原始List 中检索地图更容易:

    Map<Department, List<String>> collect = allPersons.stream()
        .collect(Collectors.groupingBy(
            Person::getDepartment,
            Collectors.mapping(Person::getName, Collectors.toList())
        ));
    

    这也将受益于static imports:

    Map<Department, List<String>> collect = allPersons.stream().collect(
        groupingBy(Person::getDepartment, mapping(Person::getName, toList())));
    

    【讨论】:

      【解决方案2】:
      Map<Dept, List<String>> namesInDept
          = peopleInDept.entrySet().stream()
                        .collect(toMap(Map.Entry::getKey, 
                                       e -> e.getValue().stream()
                                                        .map(Person::getName)
                                                        .collect(toList()));
      

      【讨论】:

        【解决方案3】:

        我怀疑你不需要中介 Map&lt;Person, List&lt;Department&gt;&gt; 类型。如果是这种情况,您可以一步完成:

        Map<Department, List<String>> result = allPersons.stream().collect(
            Collectors.groupingBy(Person::getDepartment,
                Collectors.mapping(Person::getName, Collectors.toList())
            )
        );
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-31
          • 2020-03-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多