我有两个列表,我必须根据它们创建一个地图。首先,我在for循环中进行迭代,其次我想被流抛弃而不是收集来映射,但是我不知道如何在特定情况下使用Collectors.toMap。有可能吗?
我已经提出了解决方案,但没有使用流,但是我很好奇是否可以这样做,如果可以,怎么办?
public void findMatch(List<ObjectA> objectAList, List<ObjectB> objectBList) {
Map<ObjectB, ObjectA> objectBObjectAMap = new HashMap<>();
for (ObjectB objectB : objectBList) {
if (isNull(objectB.getHandoverTime())) {
objectBObjectAMap.putAll(
objectAList
.stream()
.filter(objectA -> {
ObjectC objectC = objectB.getObjectC();
return objectA.getNumber().equals(objectC.getNumber())
&& objectA.getQuality().equals(objectC.getQuality());
})
.collect(Collectors.toMap(???)));
}
}
}
答案 0 :(得分:2)
您可以尝试使用flatMap
:
Map<ObjectB, ObjectA> objectBObjectAMap =
objectBList.stream()
.filter(b -> isNull(b.getHandoverTime()))
.flatMap(b -> objectAList.stream()
.filter(a -> {
ObjectC c = b.getObjectC();
return a.getNumber().equals(c.getNumber()) &&
a.getQuality().equals(c.getQuality());
})
.map(a -> new SimpleEntry<>(b,a)))
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue));
这是假设每个ObjectB
实例都不会与多个ObjectA
实例相关联。
答案 1 :(得分:0)
我试图恢复您的问题,并创建了三个简单的案例
public class ObjectA {
Long number;
String quality;
...
}
public class ObjectB {
Long number;
String quality;
Date handoverTime;
ObjectC objectC;
...
}
public class ObjectC {
Long number;
String quality;
...
}
和主要功能,希望您能处理。
ObjectA a1 = new ObjectA();
a1.setNumber(1L);
a1.setQuality("aaa1");
ObjectA a2 = new ObjectA();
a2.setNumber(2L);
a2.setQuality("aaa2");
List<ObjectA> aList = new ArrayList<>();
aList.add(a1);
aList.add(a2);
ObjectB b1 = new ObjectB();
b1.setNumber(3L);
b1.setQuality("bbb1");
//b1.setHandoverTime(new Date());
ObjectC c1 = new ObjectC();
c1.setNumber(1L);
c1.setQuality("aaa1");
b1.setObjectC(c1);
ObjectB b2 = new ObjectB();
b2.setNumber(4L);
b2.setQuality("bbb2");
//b2.setHandoverTime(new Date());
ObjectC c2 = new ObjectC();
c2.setNumber(2L);
c2.setQuality("aaa2");
b2.setObjectC(c2);
List<ObjectB> bList = new ArrayList<>();
bList.add(b1);
bList.add(b2);
Map<ObjectB, ObjectA> mapzz = findMatch(aList, bList);
System.out.println(mapzz);
和以下方法
public static Map<ObjectB, ObjectA> findMatch(List<ObjectA> objectAList, List<ObjectB> objectBList) {
List<ObjectA> checkPoint1 = new ArrayList<>();
Map<ObjectB, ObjectA> mapzz = new HashMap<>();
mapzz.putAll(
objectBList.stream()
.filter(objB -> isNull(objB.getHandoverTime()))
.collect(Collectors.toMap(Function.identity(),
objB -> objectAList
.stream()
.filter((a) -> a.getNumber().equals(objB.getObjectC().getNumber()) && a.getQuality().equals(objB.getObjectC().getQuality()))
.peek(checkPoint1::add)
.findAny().get()
))
);
return mapzz;
}
结果如下:
{ObjectB {number = 3,quality = bbb1,handoverTime = null,objectC = ObjectC {number = 1,quality = aaa1}} = ObjectA {number = 1,quality = aaa1},ObjectB {number = 4,quality = bbb2,handoverTime = null,objectC = ObjectC {number = 2,quality = aaa2}} = ObjectA {number = 2,quality = aaa2}}
希望对您有帮助。