[英]Merge two lists of objects into Map having value as different object in java 8
我有两个相同类型“ MyInfoObject”的列表(例如A和B):
public class MyInfoObject {
private Long id;
private String signature;
public MyInfoObject(Long id, String signature) {
super();
this.id = id;
this.signature = signature;
}
}
我想创建这两个列表的Map,以使列表A的所有ID和具有相同签名的列表B的所有ID创建一个类型为“ BucketOfAandB”的存储桶:
public class BucketOfAandB {
private List<Long> aIds ;
private List<Long> bIds ;
public BucketOfAandB(List<Long> aIds, List<Long> bIds) {
super();
this.aIds = aIds;
this.bIds = bIds;
}
}
因此,我的输出将是Map<String, BucketOfAandB>
,其中键是签名
例如,我的输入是:
List<MyInfoObject> aList = new ArrayList<>();
aList.add(new MyInfoObject(1l, "a"));
aList.add(new MyInfoObject(2l, "d"));
aList.add(new MyInfoObject(3l, "b"));
aList.add(new MyInfoObject(4l, "a"));
aList.add(new MyInfoObject(5l, "a"));
aList.add(new MyInfoObject(6l, "c"));
aList.add(new MyInfoObject(7l, "a"));
aList.add(new MyInfoObject(8l, "c"));
aList.add(new MyInfoObject(9l, "b"));
aList.add(new MyInfoObject(10l, "d"));
List<MyInfoObject> bList = new ArrayList<>();
bList.add(new MyInfoObject(11l, "a"));
bList.add(new MyInfoObject(21l, "e"));
bList.add(new MyInfoObject(31l, "b"));
bList.add(new MyInfoObject(41l, "a"));
bList.add(new MyInfoObject(51l, "a"));
bList.add(new MyInfoObject(61l, "c"));
bList.add(new MyInfoObject(71l, "a"));
bList.add(new MyInfoObject(81l, "c"));
bList.add(new MyInfoObject(91l, "b"));
bList.add(new MyInfoObject(101l, "e"));
在这种情况下,我的输出将是:
{
a= BucketOfAandB[aIds=[1, 4, 5, 7], bIds=[11, 41, 51, 71]],
b= BucketOfAandB[aIds=[3, 9], bIds=[31, 91]],
c= BucketOfAandB[aIds=[6, 8], bIds=[61, 81]],
d= BucketOfAandB[aIds=[2, 10], bIds=null],
e= BucketOfAandB[aIds=null, bIds=[21, 101]],
}
我想使用Java 8 Streams做到这一点。
我发现的一种方法是:
aList
创建Map<String, List<Long>>
,说aBuckets resultant
Map<String, BucketOfAandB>
aBuckets
删除 bList
元素添加到所需的签名存储区 aBuckets
所有剩余元素,并将它们添加到resultant
我想知道一种使用Java 8 Streams实施此方法的更好方法。
提前致谢!
编辑:我尝试使用流,但对实施不是很满意。 以下是我的逻辑:
Map<String, BucketOfAandB> resultmap = new HashMap<>();
// get ids from aList grouped by signature
Map<String, List<Long>> aBuckets = aList.stream().collect(Collectors.groupingBy(MyInfoObject::getSignature,
Collectors.mapping(MyInfoObject::getId, Collectors.toList())));
// iterate bList and add it to bucket of its signature
bList.forEach(reviewInfo -> {
BucketOfAandB bucket = resultmap.get(reviewInfo.getSignature());
if(null == bucket) {
bucket = new BucketOfAandB();
resultmap.put(reviewInfo.getSignature(), bucket);
List<Long> sourceReviewBucket = aBuckets.remove(reviewInfo.getSignature());
if(null !=sourceReviewBucket) {
bucket.setaIds(sourceReviewBucket);
}
}
bucket.addToB(reviewInfo.getId());
});
Map<String, BucketOfAandB> result = aBuckets.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> new BucketOfAandB(e.getValue(), null)));
resultmap.putAll(result);
这个怎么样:
Map<String, List<Long>> mapA = aList.stream()
.collect(Collectors.groupingBy(
MyInfoObject::getSignature,
Collectors.mapping(MyInfoObject::getId, Collectors.toList())));
Map<String, List<Long>> mapB = bList.stream()
.collect(Collectors.groupingBy(
MyInfoObject::getSignature,
Collectors.mapping(MyInfoObject::getId, Collectors.toList())));
Map<String, BucketOfAandB> overAll = new HashMap<>();
Set<String> allKeys = new HashSet<>();
allKeys.addAll(mapA.keySet());
allKeys.addAll(mapB.keySet());
allKeys.forEach(x -> overAll.put(x, new BucketOfAandB(mapA.get(x), mapB.get(x))));
但这假设listA
中存在的每个键都将存在于listB
如果将getters添加到MyInfoObject
,并且让BucketOfAandB
惰性地初始化其列表(即,没有构造函数),如下所示:
public class BucketOfAandB {
private List<Long> aIds;
private List<Long> bIds;
public void addAId(Long id) {
if (aIds == null) {
aIds = new ArrayList<>();
}
aIds.add(id);
}
public void addBId(Long id) {
if (bIds == null) {
bIds = new ArrayList<>();
}
bIds.add(id);
}
}
您只需三行就可以做到,同时保留您意图的语义:
Map<String, BucketOfAandB> map = new HashMap<>();
aList.forEach(o -> map.computeIfAbsent(o.getSignature(), s -> new BucketOfAandB())
.addAId(o.getId()));
bList.forEach(o -> map.computeIfAbsent(o.getSignature(), s -> new BucketOfAandB())
.addBId(o.getId()));
如果您正在使用并行流,请synchronize
add方法,因为这只是存储桶上的潜在冲突,因此实际上不会增加性能损失。
你可以这样写:
Function<List<MyInfoObject>, Map<String, List<Long>>> toLongMap =
list -> list.stream()
.collect(groupingBy(MyInfoObject::getSignature,
mapping(MyInfoObject::getId, toList())));
Map<String, List<Long>> aMap = toLongMap.apply(aList);
Map<String, List<Long>> bMap = toLongMap.apply(bList);
Map<String, BucketOfAandB> finalMap = new HashMap<>();
aMap.forEach((sign, listA) -> {
finalMap.put(sign, new BucketOfAandB(listA, bMap.get(sign)));
});
bMap.forEach((sign, listB) -> {
finalMap.putIfAbsent(sign, new BucketOfAandB(null, listB));
});
如您所说,首先可以创建Map<String, List<Long>>
,然后构建Map<String, BucketOfAandB>
:
Map<String, List<Long>> idsBySignatureA = aList.stream()
.collect(Collectors.groupingBy(
MyInfoObject::getSignature,
Collectors.mapping(
MyInfoObject::getId,
Collectors.toList())));
Map<String, List<Long>> idsBySignatureB = bList.stream()
.collect(Collectors.groupingBy(
MyInfoObject::getSignature,
Collectors.mapping(
MyInfoObject::getId,
Collectors.toList())));
Map<String, List<BucketOfAandB>> result = Stream.concat(idsBySignatureA.entrySet().stream(), idsBySignatureB.entrySet().stream())
.collect(Collectors.groupingBy(
Map.Entry::getKey,
Collectors.mapping(entry ->
new BucketOfAandB(
idsBySignatureA.get(entry.getKey()),
idsBySignatureB.get(entry.getKey())),
Collectors.toList())
));
也可以随意将第一部分提取到函数中以提高可读性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.