최종 연산은 스트림의 요소를 소모해서 결과를 만들어낸다. 그래서 최종 연산후에는 스트림이 닫히게 되고 더 이상 사용할 수 없다. 최종 연산의 결과는 스트림 요소의 합과 같은 단일 값이거나, 스트림의 요소가 담긴 배열 또는 컬렉션일 수 있다.
forEach()
forEach()는 peek()과 달리 스트림의 요소를 소모하는 최종연산이다.
반환 타입이 voi이므로 스트림의 요소를 출력하는 용도로 많이 사용된다.
조건검사 - allMatch(), anyMatch(), noneMatch()
스트림의 요소에 대해 지정된 조건에 모든 요소가 일치하는 지, 일부가 일치하는지 아니면 어떤 요소도 일치하지 않는지 확인하는데 사용할 수 있는 메서드 들이다. 이 메서드들은 모두 매개변수로 Predicate를 요구하며, 연산결과로 boolean을 반환한다.
List<Integer> allMatch = List.of(2, 4, 6, 8);
boolean isAllMatch = allMatch.stream().allMatch(f -> f % 2 == 0);
System.out.println("isAllMatch = " + isAllMatch);
boolean isAllMatchFail = allMatch.stream().allMatch(f -> f > 3);
System.out.println("isAllMatchFail = " + isAllMatchFail);
List<Integer> anyMatch = List.of(1, 2, 3, 4, 5);
boolean isAnyMatch = anyMatch.stream().anyMatch(f -> f % 2 == 0);
System.out.println("isAnyMatch = " + isAnyMatch);
boolean isAnyMatchFail = anyMatch.stream().anyMatch(f -> f > 5);
System.out.println("isAnyMatchFail = " + isAnyMatchFail);
List<Integer> noneMatch = List.of(1, 2, 3, 4, 5);
boolean isNoneMatch = noneMatch.stream().noneMatch(f -> f > 5);
System.out.println("isNoneMatch = " + isNoneMatch);
boolean isNoneMatchFail = noneMatch.stream().noneMatch(f -> f > 4);
System.out.println("isNoneMatchFail = " + isNoneMatchFail);
--- 결과 ---
isAllMatch = true
isAllMatchFail = false
isAnyMatch = true
isAnyMatchFail = false
isNoneMatch = true
isNoneMatchFail = false
스트림 연산결과 반환 - findFirst(), findAny()
스트림의 요소 중에서 조건에 일치하는 첫 번째 것을 반환하는 findFirst()가 있는데, 주로 filter()와 함께 사용되어 조건에 맞는 스트림의 요소가 있는지 확인하는데 사용된다. 병렬 스트림의 경우에는 findFirst() 대신 findAny()를 사용해야 한다.
List<Integer> list = List.of(1, 2, 3, 4, 5, 6);
Integer findFirst = list.stream().filter(x -> x % 2 == 0).findFirst().get();
Integer findAny = list.stream().parallel().filter(x -> x % 2 == 0).findAny().get();
System.out.println("findFirst = " + findFirst);
System.out.println("findAny = " + findAny);
--- 결과 ---
findFirst = 2
findAny = 4
통계 - count(), sum(), average(), max(), min()
기본형 스트림인 경우에는 전부 사용할 수 있지만, 기본형 스트림이 아닌 경우에는 count, max, min만 사용할 수 있다.
인자로 Comparator가 필요하다.
리듀싱 - reduce()
스트림의 요소를 줄여나가면서 연산을 수행하고 최종결과를 반환한다. 처음 두요소를 가지고 연산한 결과를 가지고 그 다음 요소와 연산한다.
List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Integer integer = list.stream().reduce((x, y) -> {
System.out.println(x + " + " + y + " = " + (x + y));
return x + y;
}).get();
System.out.println("sum = " + integer);
--- 결과 ---
1 + 2 = 3
3 + 3 = 6
6 + 4 = 10
10 + 5 = 15
15 + 6 = 21
21 + 7 = 28
28 + 8 = 36
36 + 9 = 45
45 + 10 = 55
sum = 55
위와같이 첫번째 인자와 두번째 인자를 가지고 연산한다. 연산된 결과를 가지고 세번째 인자와 연산하고, 이것을 반복한다.
그리고 연산결과의 초기값을 갖는 reduce도 있다.
List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Integer integer = list.stream().reduce(100, (x, y) -> {
System.out.println(x + " + " + y + " = " + (x + y));
return x + y;
});
System.out.println("sum = " + integer);
--- 결과 ---
100 + 1 = 101
101 + 2 = 103
103 + 3 = 106
106 + 4 = 110
110 + 5 = 115
115 + 6 = 121
121 + 7 = 128
128 + 8 = 136
136 + 9 = 145
145 + 10 = 155
sum = 155
'Java' 카테고리의 다른 글
| Stream - 중간연산 (0) | 2023.09.20 |
|---|---|
| Stream (0) | 2023.09.20 |
| 함수형 인터페이스 (0) | 2023.09.13 |
| 람다식 (0) | 2023.09.12 |
| 날짜와 시간 - parse (0) | 2023.09.11 |