By the way, I updated Java, so I will do it with REPL. Just type jshell on the console (java9 or above?)
jshell>
How to do it!
jshell> var streamA = Stream.of(1,2,3);
streamA ==> java.util.stream.ReferencePipeline$Head@1f36e637
jshell> var streamB = Stream.of(4,5);
streamB ==> java.util.stream.ReferencePipeline$Head@578486a3
jshell> Stream.concat(streamA, streamB).forEach(System.out::println);
1
2
3
4
5
As you can see, concat has only two arguments? https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#concat-java.util.stream.Stream-java.util.stream.Stream-
In the first place, there was no problem with the operation of connecting due to the nature of stream. Let's go honestly.
jshell> var streamA = Stream.of(1,2,3);
streamA ==> java.util.stream.ReferencePipeline$Head@536aaa8d
jshell> var streamB = Stream.of(4,5);
streamB ==> java.util.stream.ReferencePipeline$Head@e320068
jshell> var streamC = Stream.of(6,7,8,9);
streamC ==> java.util.stream.ReferencePipeline$Head@1f57539
jshell> Stream.of(streamA, streamB, streamC).flatMap(Function.identity()).forEach(System.out::println);
1
2
3
4
5
6
7
8
9
jshell> var streamA = Stream.of(1,2,3);
streamA ==> java.util.stream.ReferencePipeline$Head@27a8c74e
jshell> var streamB = Stream.of(4,5);
streamB ==> java.util.stream.ReferencePipeline$Head@2d8f65a4
jshell> var streamC = Stream.of(6,7,8,9);
streamC ==> java.util.stream.ReferencePipeline$Head@1b68ddbd
jshell> Stream.of(streamA, streamB, streamC).
...> flatMap(Function.identity()).
...> forEach(System.out::println);
1
2
3
4
5
6
7
8
9
This method was my limit. If you find out something, I will add it (maybe). I mean, the period is unpleasant at the end. No, in natural language this is normal driving.
As you can see, reducing it makes it optional and it's a hassle twice. YO
jshell> var streamA = Stream.of(1,2,3);
streamA ==> java.util.stream.ReferencePipeline$Head@2781e022
jshell> var streamB = Stream.of(4,5);
streamB ==> java.util.stream.ReferencePipeline$Head@57e1b0c
jshell> var streamC = Stream.of(6,7,8,9);
streamC ==> java.util.stream.ReferencePipeline$Head@4232c52b
jshell> Stream.of(streamA, streamB, streamC).
...> reduce(Stream::concat)
$38 ==> Optional[java.util.stream.ReferencePipeline$Head@ea1a8d5]
That's all for today. Tell yourself that it is important to continue little by little.
"If you don't want reduce to be Optional, you can use a 3-argument reduce."
Stream.of(streamA, streamB, streamC).reduce(Stream.empty(), Stream::concat, Stream::concat);
You're right. No, but then reduce argument 2 is fine, right?
Stream.of(streamA, streamB, streamC).reduce(Stream.empty(), Stream::concat)
The story of reduce is Beckenbauer.
Recommended Posts