Basic processing flow of java Stream

I'm still a beginner in programming, and I'm going to write a series that spells out descriptions that I thought I could use or use when manufacturing Java in a ** memorandum **. It's just a memorandum, so I plan to use it for myself. This time, which is the first post, it will be like *** Let's write from the very basics and remember the transcendental basics.

First of all, a description that replaces a common For statement with a Stream Suppose you have a list with the following numbers.

List<Integer> iList = Arrays.asList(1,4,5,7,14,15,77,98,100,103);

I want to output only even numbers from here. Below is the code in an ordinary extended For statement that outputs only even numbers.

		for (Integer i : iList) {
			if (i % 2 == 0) {
				System.out.println(i);
			}
		}

Output result


4
14
98
100

If you replace the above with Stream ...

		iList.stream().filter(i -> i % 2 == 0).forEach(i -> System.out.println(i));

Output result


4
14
98
100

Here is a rough description of what you are doing with Stream first

iList.stream()

So, get the stream

.filter(i -> i % 2 == 0)

Perform an operation called intermediate operation with

.forEach(i -> System.out.println(i));

The final operation called the termination operation is performed at.

This flow of "intermediate operation" ⇒ "termination operation" is the basis of Stream, so If you remember this much, it's easy to google even if you forget a little about how to write.

So, as you can see, the above For statement and Stream have the same processing result, but in detail, it seems that what they are actually doing is different.

*** For the familiar For statement *** Loop iList only for the element statement of iList, and if the element has a value divisible by 2, output it, otherwise it will not output and go to the next element! Processing such as.

*** For Stream *** Specify the filtering condition with the "filter method" used for the intermediate operation, Extract the elements filtered by the "for Each method" one by one and execute them! Processing such as (output in this case).

There are various types of intermediate operations and termination operations, and by combining them, you can perform various processing you want to write. I often use the intermediate operation "map method" (conversion) and the termination operation "Collect" (list generation).

Next, I will write what kind of operation method is used at what time.

Recommended Posts

Basic processing flow of java Stream
[Java] Stream processing
[Java11] Stream Summary -Advantages of Stream-
[Java] Stream API --Stream termination processing
[Java] Stream API --Stream intermediate processing
Summary of java error processing
Stream processing of Java 8 can be omitted so far!
About Lambda, Stream, LocalDate of Java8
Basic usage of java Optional Part 1
[Basic knowledge of Java] Scope of variables
Aiming for a basic understanding of the flow of recursive processing
[Java] Exception types and basic processing
Basic structure of Java source code
Basic basis of Android asynchronous processing "AsyncTask"
Data processing using stream API from Java 8
[Processing x Java] Construction of development environment
[Java] Personal summary of conditional statements (basic)
Basic knowledge of Java development Note writing
[Basic knowledge of Java] About type conversion
[JAVA] Stream type
Java basic grammar
Java basic grammar
Try Java 8 Stream
Java Stream API
Java basic knowledge 1
[Java] Basic structure
[Java] [Basic] Glossary
Java basic grammar
Java basic grammar
Studying Java 8 (Stream)
[Java] Multi-thread processing
Stream intermediate processing
[Java] Overview of Java
Java exercises [Basic]
Java Stream termination
Java 9 Optional :: stream
java iterative processing
[Java] Personal summary of classes and methods (basic)
[Note] Java: Speed of List processing by purpose
[Java / Spring Boot] Spring security ⑤ --Implementation of logout processing
Java review ③ (Basic usage of arrays / reference type)
[Java] Basic summary of Java not covered by Progate ~ Part 1 ~
Expired collection of java
Predicted Features of Java
List processing to understand with pictures --java8 stream / javaslang-
[Java] Significance of serialVersionUID
[Java] Stream Collectors notes
NIO.2 review of java
Review of java Shilber
java basic knowledge memo
[Java] Stream API-Stream generation
[Java] Data type ①-Basic type
Use Java lambda expressions outside of the Stream API
java --Unification of comments
Implementation example of simple LISP processing system (Java version)
Image processing: The basic structure of the image read by the program
Java basic date manipulation
Java basic naming conventions
Basic format of Dockefile
java (merits of polymorphism)
Java8 Stream API practice