gpt4 book ai didi

Java 8 流 : list to flat map grouped by

转载 作者:行者123 更新时间:2023-12-01 20:08:49 25 4
gpt4 key购买 nike

我有以下情况。 (伪代码)

class A {
id;
List<B> bs;
}

class B {}

我想知道如何将 List os 转换为 -> Map of Bs

List<A> as;

// the Map key is A.id (Map<A.id, List<B>>)
Map<Integer, List<B>> bs = as.stream()
.map(a ->a.getBs())
.collect(// I dont know what to add here ???);

最佳答案

似乎您想要这样的时间:

 Map<Integer, List<B>> bs = as.stream()
.collect(Collectors.toMap(A::getId, A::getBs));

关于Java 8 流 : list to flat map grouped by,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47030384/

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