Stream은 생성, 가공, 결과 만들기 단계로 구성됩니다.
✔️ 생성
Collection, Array, File
-> 데이터 소스로 부터 스트림 생성
✔️ 가공(중간연산자)
filter, map, peek, sorted, distinct, limit
-> 스트림을 변환하거나 필터링하는 데 사용. 새로운 Stream 반환
✔️ 결과 만들기(최종연산자)
collect(권장), forEach(신중), findAny, findFirst, anyMatch, allMatch
-> 스트림을 닫고 결과 생성
List<String> words = Arrays.asList("apple", "Pear", "grape", "Kiwi");
// 중간 연산: 문자열 길이가 5보다 큰 것만 필터링
// 최종 연산: 결과를 리스트로 모음
List<String> filteredWords = words.stream()
.filter(word -> word.length() < 5)
.collect(Collectors.toList());
> filteredWords = ["Pear", "Kiwi"];