gpt4 book ai didi

java - 使用 Guava 将多个 Collections 合并到一个 Collection 中的单个 Collection

转载 作者:搜寻专家 更新时间:2023-11-01 01:23:01 29 4
gpt4 key购买 nike

最佳答案

您可以使用一个函数来提取列表并使用 concat 来展平列表。这会产生一个 Iterable。

List<A> input;
Function<A, List<B>> t = new Function<A, List<B>>() {
@Override public List<B> apply(A input) {
return input.myListOfB;
}
};
Iterable<B> transform = Iterables.concat(Iterables.transform(input, t));

如果需要,您可以创建一个列表:

ImmutableList<B> asList = ImmutableList.copyOf(transform);
//or
List<B> newArrayList = Lists.newArrayList(transform);

注意:通常,类的公共(public)字段是静态且不可变的或私有(private)的。其他一切都会给你带来麻烦。

关于java - 使用 Guava 将多个 Collections<A> 合并到一个 Collection<B> 中的单个 Collection<A>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10293120/

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