🤹♂️Java Brains 강의🤹♂️
https://www.youtube.com/playlist?list=PLqq-6Pq4lTTa9YGfyhyW2CqdtW9RtY-I3
✅ Streams : A sequence of elements supporting sequential and parallel aggregate operations
✨ stream()
✔️ Returns a sequential stream with this collection as its source
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Person> people = Arrays.asList(
new Person("Charles", "Dickens", 60),
new Person("Lewis", "Carroll", 42),
new Person("Thomas", "Carlyle", 51),
new Person("Charlotte", "Bronte", 45),
new Person("Matthew", "Arnold", 39)
);
// LastName 이 C 로 시작하는 사람 출력하기
people.stream()
.filter(p -> p.getLastName().startsWith("C"))
.forEach(p -> System.out.println(p.getFirstName()));
// LastName 이 C 로 시작하는 사람 수 출력하기
long count = people.stream()
.filter(p -> p.getLastName().startsWith("C"))
.count();
System.out.println(count);
}
}
· filter() : returns a stream consisting of the elements of this stream that match the given predicate
· count() : provides a count of elements in that stream
✨ parallelStream()
✔️ Returns a possibly parallel stream with this collection as its source
✔️ Split the collection into multiple streams
✔️ Do the exact same thing with .stream()
...
public class Main {
public static void main(String[] args) {
...
people.parallelStream()
.filter(p -> p.getLastName().startsWith("C"))
.forEach(p -> System.out.println(p.getFirstName()));
}
}
'Spring > JAVA' 카테고리의 다른 글
[Java8 Lambda] foreach iteration (0) | 2023.10.19 |
---|---|
[Java8 Lambda] Method References (0) | 2023.10.18 |
[Java8 Lambda] this (0) | 2023.10.17 |
[Java8 Lambda] Closures in Lambda Expressions (1) | 2023.10.16 |
[Java8 Lambda] Exception Handling in Lambdas (try - catch) (0) | 2023.10.13 |