I participated in JJUG CCC 2018 Fall. I personally summarized the contents of @ skrb's # ccc_g4 lecture.
** Stream is a simple iterator **
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
Many people who tried it once but did not understand
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. ** **
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;
}
** 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. **
1 argument with return value Function <T, R>
1 argument return value boolean Predicate
// 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.
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
The person himself has already posted the Presentation Material, so this article is for reference only.
Recommended Posts