gpt4 book ai didi

java - 将两个对象列表合并到具有 Java 8 中不同对象值的 Map

转载 作者:搜寻专家 更新时间:2023-10-31 19:59:32 26 4
gpt4 key购买 nike

我有两个相同类型“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> ,其中 key 是签名

例如我的输入是:

    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]],
}

我想使用 Streams of java 8 来实现。

我想到的一种方法是:

  1. 创建Map<String, List<Long>>来自 aList , 说 aBuckets
  2. 迭代 bList 并创建 resultant Map<String, BucketOfAandB>经过
    • 2a。将具有相同签名的 aBuckets 中的列表设置为结果,将其从 aBuckets 中删除
    • 2b。添加 bList 的元素到所需的签名桶
  3. 迭代aBuckets的所有剩余元素并将它们添加到 resultant

我想知道使用 Streams of Java 8 实现此目的的更好方法。

提前致谢!

编辑:我尝试使用流,但对实现不是很满意。以下是我的逻辑:

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

关于java - 将两个对象列表合并到具有 Java 8 中不同对象值的 Map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51931371/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com