如何将 Set<Set<String>> 转换为 Set<String>?
How to convert Set<Set<String>> to Set<String>?
假设允许使用 Streams 和 Collections,Lambda?
我尝试使用 for 循环,但它没有解决我的问题。
// Set<Set<String>> to Set<String>
for(Set<String> s : set) {
result.addAll(s);
set.add(result);
}
set 是 Set<Set<String>>
类型,结果是 Set<String>
.
类型
这里是使用 Stream 的选项 API:
Set<String> result = sets.stream()
.flatMap(Collection::stream)
.collect(Collectors.toSet());
假设允许使用 Streams 和 Collections,Lambda? 我尝试使用 for 循环,但它没有解决我的问题。
// Set<Set<String>> to Set<String>
for(Set<String> s : set) {
result.addAll(s);
set.add(result);
}
set 是 Set<Set<String>>
类型,结果是 Set<String>
.
这里是使用 Stream 的选项 API:
Set<String> result = sets.stream()
.flatMap(Collection::stream)
.collect(Collectors.toSet());