英文:
How to merge sets using java stream?
问题
You can merge the two sets into a single Set<String>
using flatMap()
like this:
Set<String> mergedSet = Stream.of(set1, set2)
.flatMap(Set::stream)
.collect(Collectors.toSet());
This code flattens the individual sets into a stream of strings and then collects them into a single set.
英文:
I have two Set
s like this:
Set<String> set1;
Set<String> set2;
And I want to merge them with
Set<String> s = Stream.of(set1, set2).collect(Collectors.toSet());
but I get the following error:
> no instance(s) of type variable(s) exist so that Set<String>
conforms to String
>
>inference variable T
has incompatible bounds:
>
> equality constraints: String
lower bounds: Set<String>
How can I convert the Set
s to a single Set<String>
with flatMap()
?
Is there any other solution that can accomplish this operation gracefully?
答案1
得分: 10
如果您坚持使用Stream
,您可以使用flatMap
将您的Stream<Set<String>>
转换为Stream<String>
,然后可以将其收集到Set<String>
中:
Set<String> s = Stream.of(set1, set2).flatMap(Set::stream).collect(Collectors.toSet());
英文:
If you insist on using Stream
s, you can use flatMap
to convert your Stream<Set<String>>
to a Stream<String>
, which can be collected into a Set<String>
:
Set<String> s = Stream.of(set1, set2).flatMap(Set::stream).collect(Collectors.toSet());
答案2
得分: 2
你可以使用 Stream.concat
来合并两个集合的流并收集成一个集合。
Set<String> s = Stream.concat(set1.stream(), set2.stream()).collect(Collectors.toSet());
英文:
You can use Stream.concat
to merge the stream of two sets and collect as set.
Set<String> s = Stream.concat(set1.stream(), set2.stream()).collect(Collectors.toSet());
答案3
得分: 2
Concat
有几种可能的方法 -
***连接***
Set<String> s = Stream.concat(set1.stream(), set2.stream()).collect(Collectors.toSet());
当有超过2个流时,会稍微变得复杂,因为我们必须编写
Stream.concat(Stream.concat(set1.stream(), set2.stream()), set3.stream())
**连接可能会对深度连接的流造成问题**。从文档中可以看出 -
> 当从重复的连接构造流时要小心。访问深度连接的流的元素可能会导致深度调用链,甚至是StackOverflowException。
***减少***
还可以使用Reduce来执行流的连接操作 -
Set<String> s = Stream.of(set1.stream(), set2.stream()).reduce(Stream::concat)
.orElseGet(Stream::empty).collect(Collectors.toSet());
这里 `Stream.reduce(`) 返回一个可选项,这就是为什么调用 `orElseGet` 方法的原因。也可以连接多个集合,如下所示 -
Stream.of(set1.stream(), set2.stream(), set3.stream()).reduce(Stream::concat).orElseGet(Stream::empty).collect(Collectors.toSet());
与深度连接的流相关的问题也适用于Reduce。
***Flatmap***
可以使用Flatmap来获得与以下相同的结果 -
Set<String> s = Stream.of(set1, set2).flatMap(Set::stream).collect(Collectors.toSet());
要连接多个流,您可以使用 -
Set<String> s = Stream.of(set1, set2, set3).flatMap(Set::stream).collect(Collectors.toSet());
Flatmap避免了`StackOverflowException`。
英文:
There are couple of approach possible -
Concat
Set<String> s = Stream.concat(set1.stream(), set2.stream()).collect(Collectors.toSet());
It's get slightly ugly for more than 2 streams as we have to write
Stream.concat(Stream.concat(set1.stream(), set2.stream()), set3.stream())
Concat could be a problem for deeply concatenated stream. From documentation -
> Use caution when constructing streams from repeated
> concatenation.Accessing an element of a deeply concatenated stream can
> result in deep call chains, or even StackOverflowException.
Reduce
Reduce can also be used to perform concatenation of stream as -
Set<String> s = Stream.of(set1.stream(), set2.stream()).reduce(Stream::concat)
.orElseGet(Stream::empty).collect(Collectors.toSet());
Here Stream.reduce(
) returns optional that's the reason for orElseGet
method call. It's also possible to contact multiple set as
Stream.of(set1.stream(), set2.stream(), set2.stream()).reduce(Stream::concat).orElseGet(Stream::empty).collect(Collectors.toSet());
Problem associated with deeply contacted stream applies to reduce as well
Flatmap
Flatmap can be used to get same result as -
Set<String> s = Stream.of(set1, set2).flatMap(Set::stream).collect(Collectors.toSet());
To concat multiple stream you can use -
Set<String> s = Stream.of(set1, set2, set3).flatMap(Set::stream).collect(Collectors.toSet());
flatmap avoids StackOverflowException
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论