Java 如何从flatMap创建HashMap?

Java 如何从flatMap创建HashMap?,java,java-8,hashmap,java-stream,collect,Java,Java 8,Hashmap,Java Stream,Collect,我在方法param中有两个映射 private Map<String, List<Attr>> getPropAttr(Map<String, List<Attr>> redundantProperty, Map<String, List<Attr>> notEnoughProper

我在方法param中有两个映射

 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;
私有映射getPropAttr(映射冗余属性,
地图注释(NoughProperty){
Map propAttr=new HashMap();
redundantProperty.forEach((secondPropertyName,secondPropertyAttributes)->notEnoughProperty.entrySet().stream()
.filter(firstPropertyName->secondPropertyName.contains(firstPropertyName.getKey()))
.forEach(firstProperty->{
List firstPropertyAttrs=firstProperty.getValue();
List redundantPropAttrs=getRedundantPropAttrs(secondPropertyAttrs,firstPropertyAttrs);
字符串propName=firstProperty.getKey();
propAttr.put(propertyName,RedundantPropattr);
}));
返回propAttr;
我想在流上重写此方法。但是,我在流收集器中遇到一些问题。这是因为看不到从流到flatmap的返回值(列表)。在下面-我尝试在流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()));
私有映射getPropAttr(映射冗余属性,
地图注释(NoughProperty){
返回redundantProperty.entrySet().stream()
.flatMap(secondProperty->notEnoughProperty.entrySet().stream())
.filter(firstPropertyName->secondProperty.getKey().contains(firstPropertyName.getKey()))
.map(firstProperty->{
列出onlinePropertyAttrs=firstProperty.getValue();
列出冗余Propattrs=
getRedundantPropAttrs(secondProperty.getValue(),firstPropertyAttrs);
返回redundantPropertyAttrs;
}))
.collect(toMap(属性::getName,toList());

调用
flatMap
后,您的
将成为
。此时,您似乎丢失了要用作输出
映射
键的属性

相反,我建议
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));
返回redundantProperty.entrySet()
.stream()
.flatMap(第二属性->
notEnoughProperty.entrySet()
.stream()
.filter(firstPropertyName->secondProperty.getKey().contains(firstPropertyName.getKey()))
.map(firstProperty->{
列出冗余Propattrs=。。。
...
返回新的SimpleEntry(firstProperty.getKey(),redundantPropertyAttrs);
}))
.collect(toMap(Map.Entry::getKey,Map.Entry::getValue));