【问题标题】:How create HashMap from flatMap?如何从 flatMap 创建 HashMap?
【发布时间】:2019-05-20 08:07:22
【问题描述】:

我在方法参数中有两个地图。

 private Map<String, List<Attr>> getPropAttr(Map<String, List<Attr>> redundantProperty,
                                                                       Map<String, List<Attr>> notEnoughProperty) {
        Map<String, List<Attr>> propAttr = new HashMap<>();
        redundantProperty.forEach((secondPropertyName, secondPropertyAttributes) -> notEnoughProperty.entrySet().stream()
                .filter(firstPropertyName -> secondPropertyName.contains(firstPropertyName.getKey()))
                .forEach(firstProperty -> {
                    List<Attr> firstPropertyAttrs = firstProperty.getValue();
                    List<Attr> redundantPropAttrs = getRedundantPropAttrs(secondPropertyAttrs, firstPropertyAttrs);

                    String propName = firstProperty.getKey();
                    propAttr.put(propertyName, redundantPropAttrs);
                }));
        return propAttr;

我想在流上重写这个方法。但是,我在流收集器中遇到了一些问题。从流到平面图中看不到返回值(List)。在下面 - 我尝试在流 API 上重写此方法。如何在 collect(toMap(first::get, second::get)) 中设置第二个参数? 谢谢你的提前。

private Map<String, List<Attr>> getPropAttr(Map<String, List<Attr>> redundantProperty,
                                                                       Map<String, List<Attr>> notEnoughProperty) {
    return redundantProperty.entrySet().stream()
            .flatMap(secondProperty -> notEnoughProperty.entrySet().stream()
                    .filter(firstPropertyName -> secondProperty.getKey().contains(firstPropertyName.getKey()))
                    .map(firstProperty -> {
                        List<Attr> onlinePropertyAttrs = firstProperty.getValue();
                        List<Attr> redundantPropAttrs = 
                                getRedundantPropAttrs(secondProperty.getValue(), firstPropertyAttrs);
                        return redundantPropertyAttrs;
                    }))
            .collect(toMap(Property::getName, toList()));

【问题讨论】:

    标签: java java-8 hashmap java-stream collect


    【解决方案1】:

    在您拨打flatMap 之后,您的Stream 将变为Stream&lt;List&lt;Attr&gt;&gt;。此时您似乎丢失了要用作输出 Map 的键的属性。

    相反,我建议flatMap 中的map 返回一个包含所需键和值的Map.Entry

    return redundantProperty.entrySet()
                           .stream()
                           .flatMap(secondProperty ->
                               notEnoughProperty.entrySet()
                                                .stream()
                                                .filter(firstPropertyName -> secondProperty.getKey().contains(firstPropertyName.getKey()))
                                                .map(firstProperty -> {
                                                    List<Attr> redundantPropAttrs = ...
                                                    ...
                                                    return new SimpleEntry<String,List<Attr>>(firstProperty.getKey(),redundantPropertyAttrs);
                                                }))
                           .collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
    

    【讨论】:

      猜你喜欢
      • 2018-02-17
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 2019-09-30
      • 2013-12-19
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多