【发布时间】: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