[JAVA] Now is the time to get started with the Stream API

I participated in JJUG CCC 2018 Fall. I personally summarized the contents of @ skrb's # ccc_g4 lecture.

What is Stream?

** Stream is a simple iterator **

Lambda-style standing position

Muhammad Ali dances like a butterfly and stabs like a bowl A lambda expression that is written like a function and behaves like a class

 Function <============ Lambda expression ============> class
 Processing: Described like a function, state + processing
 Behave like a class

Why don't you understand Stream?

Many people who tried it once but did not understand

If there is a new technology ...

Example: To make an omelet, the heat denaturation of the protein is 60 degrees, and there is a Maillard reaction ... Even if you know that, you can make it without worrying about that, right? ??

** Because it's a new concept, get used to it rather than learning it. You only have to understand the mechanism after you start calling. ** **

Stream = data flow

I think that it is processed and processed rather than a loop. I don't want to make a class to process it. → Lambda expression How to describe processing that determines only arguments and return values

 (Argument)-> {
	toDO
	return val;
}

What is Stream after all

** Reassemble what you want to do from the perspective of data processing. Simplify the process and divide it into 1 process-> simple 4 processes-> Finally boxed with toList () etc. It is good to start with for each and gradually try map-> filter etc. **

Commonly used lambda expression

1 argument with return value Function <T, R> 1 argument return value boolean Predicate 1 argument no return value Consumer No arguments, no return value, Supplier

Basic writing

 // Basic guy
(String s) -> { return s.length();
 // Type is optional
(s) -> { return s.length();
 // If it is one line, return can be omitted
(s) -> s.length();
 // () can be omitted if it is one argument
s -> s.length();

The IDE will do it for you even if you don't remember this.

Patterns that you should at least remember

Example: Extract words that start with a lowercase a from the List.

example.java


List<String> words = new ArrayList<>() {
    {
        add("apple");
        add("toast");
        add("about");
        add("sun");
    }
};
var results = words.stream()
    .map(word -> word.toLowerCase())
    .filter(word -> word.startsWith("a"))
    .collect(Collectors.toList());
System.out.println(results);

Stream is null-guaranteed, so you don't have to do a new ArrayList () for an array of outputs. There are some that you can't play with Stream, so don't overdo it.

Q&A Q1. I think the conversion processing section can refer to the method. A1. You can write. However, it is not very preferable because it is not friendly to the reader. Q2. The exception does not work. Is there a best practice A2. There is no correct answer. Use either

Supplement

The person himself has already posted the Presentation Material, so this article is for reference only.

Recommended Posts

Now is the time to get started with the Stream API
Getting Started with Doma-Introduction to the Criteria API
How to get started with slim
[java8] To understand the Stream API
Java 8 ~ Stream API ~ to start now
[DDD] What is the most accessible architecture to get started with Domain Driven Design?
I tried to get started with WebAssembly
[Note] How to get started with Rspec
Java 8 to start now ~ Date time API ~
I tried to summarize the Stream API
How to convert an array of Strings to an array of objects with the Stream API
Getting Started with Doma-Using Projection with the Criteira API
Getting Started with Doma-Using Subqueries with the Criteria API
How to get started with Eclipse Micro Profile
Try to get redmine API key with ruby
Rails beginners tried to get started with RSpec
Get started with Gradle
Get started with "Introduction to Practical Rust Programming" (Day 3)
How to get started with creating a Rails app
Get started with serverless Java with the lightweight framework Micronaut!
Getting Started with Doma-Dynamicly construct WHERE clauses with the Criteria API
Getting Started with Reactive Streams and the JDK 9 Flow API
Get started with Spring boot
How to get values in real time with TextWatcher (Android)
I want to use the Java 8 DateTime API slowly (now)
[Java] [Play Framework] Until the project is started with Gradle
How to get started with Gatsby (TypeScript) x Netlify x Docker
Access Web API on Android with Get and process Json (Java for the time being)
Get started with DynamoDB with docker
What I was addicted to with the Redmine REST API
[Java] Introduction to Stream API
Time is wrong with the application launched on the Docker container
How to get started with JDBC using PostgresSQL on MacOS
I tried to get started with Swagger using Spring Boot
Convert 2D array to csv format with Java 8 Stream API
How to get the latest live stream ID for a channel without using the YouTube Data API
When requested access to the resource is denied when pushing with Docker
Easy code review to get started with Jenkins / SonarQube: Static analysis
Returning to the beginning, getting started with Java ② Control statements, loop statements
The design concept of Java's Date and Time API is interesting
Setting to start multiple units at the same time with Vagrant
Let's get started with parallel programming
[Introduction to Java] About Stream API
[Swift] Get the current time numerically
How to get along with Rails
Memo to get with Struts2 + Ajax
[IOS] What to do when the image is filled with one color
[Java] How to get the current date and time and specify the display format
Get the type of an array element to determine if it is an array
Get started with "Introduction to Practical Rust Programming" (Day 4) Call Rust from Ruby
Android development-WEB access (GET) Try to get data by communicating with the outside. ~
[Rails] How to get the user information currently logged in with devise
If the HTTP method is not specified, the browser side requests with GET
How to compare only the time with Rails (from what time to what time, something like)
To you who were told "Don't use Stream API" in the field
Java8 / 9 Beginners: Stream API addiction points and how to deal with them
Getting Started with Doma-Criteria API Cheat Sheet
Articles to learn more about Stream API
Output system time to MANIFEST.MF with Maven
Introduction to java for the first time # 2
Try using the Stream API in Java