For those who have just started learning programming including the Java language, and those who have already learned it, for review This time I'm writing to learn about ** Stream API **.
[Introduction to Java Table of Contents] -Variables and types ・ Type conversion -Variable Scope -String operation -Array operation ・ Operator ・ Conditional branch (in preparation) ・ Repeat processing (in preparation) ・ About class (in preparation) ・ Abstract class (in preparation) ・ Interface (in preparation) ・ Encapsulation (in preparation) ・ About the module (in preparation) -Exception handling ・ About lambda expression ・ About Stream API ← Now here
A function introduced from Java8, it is an API that allows you to operate on the elements of the collection.
Streaming collection → Intermediate operation (processing data) → Termination operation (obtaining processed data)
It will be the flow of processing.
Since multiple intermediate operations of Stream API can be described, even processes that look complicated can be described neatly.
In some cases, a functional interface is received as an argument of each method, and a lambda expression is used there, but the explanation there is omitted. Please see ** About lambda expression **.
Let's compare with a simple example how to actually handle it.
First, implement without using Stream API.
python
List<String> names = Arrays.asList("Ohsera", "Samura Kawachi", "Kikuchi", "Ridge", "Nagano", "Joey Wheeler");
for (int i = 0; i < names.size(); i++) {
if (names.get(i).length() >= 3) {
if (names.get(i).contains("Inside")) {
System.out.println(names.get(i)); //Samura Kawachi Jonouchi
}
}
}
Then use the `Stream API to implement a similar process.
Implementation using Stream API
List<String> names = Arrays.asList("Ohsera", "Samura Kawachi", "Kikuchi", "Ridge", "Nagano", "Joey Wheeler");
names.stream()
.filter(name -> name.length() >= 3)
.filter(name -> name.contains("Inside"))
.forEach(name -> System.out.println(name)); //Samura Kawachi Jonouchi
The nesting is not deep and it is easy to read, and it is simple to implement.
Extract only the elements that satisfy the conditions with the filter method of the intermediate operation, The elements are output one by one with the forEach method of the termination operation.
Earlier I introduced filter as an intermediate operation method, but I will also introduce some other methods.
1.map
It is a method that performs the processing specified for each element.
map method
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.stream()
.map(num -> num * 2) //Double each element
.forEach(num -> System.out.print(num + " ")); // 2 4 6 8 10
2.limit
This method returns only the specified number of elements (maxSize).
limit method
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.stream()
.limit(3) //Take out only 3
.forEach(num -> System.out.print(num + " ")); // 1 2 3
3.distinct
It is a method that returns the element without duplication.
distinct method
List<String> names = Arrays.asList("Kubo", "Endo", "roll", "Kubo", "Okazaki", "Honda", "roll", "Endo");
names.stream()
.distinct() //Duplicate elements are removed and returned
.forEach(name -> System.out.print(name + " ")); //Kubo Endo Vol. Okazaki Honda
4.sorted
A method that sorts the elements.
sorted method
List<Integer> numbers = Arrays.asList(1, 5, 3, 2, 4);
numbers.stream()
.sorted() //Sort in ascending order
.forEach(num -> System.out.print(num + " ")); // 1 2 3 4 5
You can also sort in descending order by passing the reverseOrder method of the Comparator interface as the argument of the sorted method.
sorted method
List<Integer> numbers = Arrays.asList(1, 5, 3, 2, 4);
numbers.stream()
.sorted(Comparator.reverseOrder()) //Sort in descending order
.forEach(num -> System.out.print(num + " ")); // 5 4 3 2 1
Until now, the forEach method was used as the termination operation method, but we will introduce some other methods as well.
1.anyMatch
A method that returns a boolean depending on the condition. Returns true if any of the elements have a value that matches the condition. (Partial match judgment)
anyMatch method
List<String> fruits = Arrays.asList("banana", "apple", "orange", "pineapple");
boolean result = fruits.stream()
.anyMatch(fruit -> fruit.length() >= 7); //Determine if there is an element with 7 or more characters
System.out.println(result); // true
2.allMatch
A method that returns a boolean depending on the condition. Returns true if all the elements meet the conditions. (All match match)
allMatch method
List<Integer> numbers = Arrays.asList(2, 4, 6, 7, 8, 10);
boolean result = numbers.stream()
.allMatch(num -> num % 2 == 0); //Determining if all elements are even
System.out.println(result); // false
3.noneMatch
A method that returns a boolean depending on the condition. Returns false if any of the elements match the condition. (All non-match judgment)
noneMatch method
List<String> names = Arrays.asList("Suzuki", "Matsui", "Nomo", "Igawa", "Kawasaki", "Shinjo", "Yu Darvish");
boolean result = names.stream()
.noneMatch(name -> name.length() > 4); //Determine if there is an element with 4 or more characters
System.out.println(result); // false
I found that the source code is simpler and easier to read by using the Stream API, rather than writing the operations on the collection elements by myself. It may be confusing at first, but I want to remember it with the lambda expression.
** Stream Oracle Official ** ** [Java] Knowledge gained by continuing to write Stream API **
Recommended Posts