在Java 8中将两个对象列表合并为Map,其中值为不同的对象

6

我有两个相同类型的列表(称为A和B),类型为"MyInfoObject",具体如下:

public class MyInfoObject {
  private Long id;
  private String signature;

  public MyInfoObject(Long id, String signature) {
      super();
      this.id = id;
      this.signature = signature;
  }
}

我希望创建一个这两个列表的映射,使列表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中的流来完成它。

我想到了一种方法:

  1. 从aList创建Map<String,List<Long>>,称为aBuckets
  2. 遍历bList并通过以下方式创建resultantMap<String,BucketOfAandB>
    • 2a. 将具有相同签名的aBuckets中的列表设置到resultant中,并将其从aBuckets中删除
    • 2b. 将bList的元素添加到所需的签名桶中
  3. 遍历所有剩余的aBuckets元素并将它们添加到resultant

我想知道如何更好地使用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);

1
如果您添加算法的实现,将更容易将其转换为流和lambda等内容。 - Jack Flamp
将数据分成a、b、c、d、e的逻辑是什么?如果您告诉我们,这可能会有所帮助。 - Emre Savcı
@JackFlamp 我在上面的编辑中添加了我的实现。希望这有所帮助。 - Mak
4个回答

3
这个怎么样:
    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 中出现。

感谢您的快速回答。但是,由于我们正在迭代mapA,它将会错过e= [aIds=null, bIds=[21, 101]条目。 - Mak
aListbList可以在一个列表中存在而在另一个列表中不存在。此外,这两个列表都包含大量数据,因此性能也非常重要。 - Mak

2
如果您为MyInfoObject添加getter,并且像这样延迟初始化其列表(即没有构造函数):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()));

如果您正在使用并行流,请对add 方法进行同步处理,这不会造成实际的性能损失,因为它只是在桶上可能发生碰撞。

@Bohemian对TimSort说,请。 - Eugene
我尝试使用相同的输入集。还运行了大约30次实现。请告诉我是否需要考虑任何其他参数来检查性能。 - Mak
@Mak,您是否追求原始速度而忽略了一些复杂性? - Bohemian
.getAIds().add(o.getId()).getBIds().add(o.getId())更好的方法是分别在BucketOfAandB中添加addAId(...)addBId(...)方法。 - fps
1
@fed 你说得对。我本来想懒惰地初始化列表,但是你的想法给了我一个“机会”。回答已更新。 - Bohemian
显示剩余5条评论

0
你可以写类似这样的代码:
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));
});

谢谢您的回答。这个实现很好用。它所需的时间几乎与我的实现相等。 - Mak

0

就像你说的,首先可以创建一个 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())
    ));

也可以随意将第一部分提取为一个函数,以提高可读性。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接