Operation to connect multiple Streams @Java

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!

Concat when connecting two Streams

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-

Three or more is the turn of flatMap

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

I don't know how to break the jshell in the middle ...

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.

Useless of reduce! I hate it anymore!

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.

Postscript: Pointed out by swordone

"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

Operation to connect multiple Streams @Java
[Java] Connect to MySQL
Connect from Java to PostgreSQL
Connect to DB with Java
Connect to MySQL 8 with Java
Connect from Java to MySQL using Eclipse
How to switch between multiple Java versions
[Java] Introduction to Java
Introduction to java
Use jenv to enable multiple versions of Java
Connect to Aurora (MySQL) from a Java application
String operation Try to make Java problem TypeScript 9-3
[Java / PostgreSQL] Connect the WEB application to the database
[Java] How to make multiple for loops single
[Java] File system operation
[Java] [SQL Server] Connect to local SQL Server 2017 using JDBC for SQL Server
8 Corresponds to multiple arguments
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Introduction to bit operation
Java string multiple replacement
Kotlin's improvements to Java
From Java to Ruby !!
Java8 Stream reduction operation
Introduction to java command
Connect to multiple MySQL instances with SSL enabled in JDBC
Connect to database with spring boot + spring jpa and CRUD operation
[Java] How to use Map
How to lower java version
Migration from Cobol to JAVA
[Java] How to use Map
Convert Java Powerpoint to XPS
Java adds table to PDF
How to uninstall Java 8 (Mac)
Java to play with Function
Java --How to make JTable
Basics of character operation (java)
How to use java Optional
New features from Java7 to Java8
How to minimize Java images
How to write java comments
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to display Wingdings
[Java] Introduction to lambda expressions
[Java] How to use string.format
Shell to kill Java process
How to use Java Map
How to set Java constants
[java] Reasons to use static
Connect CentOS 7 to L2TP VPN
How to use Java variables
[Java] Introduction to Stream API
Java8 to start now ~ Optional ~
How to convert Java radix
[Java] Convert ArrayList to array
Connect to oracle with eclipse!
[Android] Connect to MySQL (unfinished)
Java thread to understand loosely
[Java] How to implement multithreading