다양한 데이터 소스를 표준화된 방법으로 다루기 위한 것이다.
쉽게 말하면 데이터의 연속적인 흐름.
데이터 소스 -> Stream -> 중간 작업 -> 최종 작업
중간 연산: 연산 결과가 스트림인 연산. 반복적으로 적용 가능
최종 연산: 연산 결과가 스트림이 아닌 연산. 단 한번만 적용 가능(스트림의 요소를 소모)
예시
stream.distinct().limit(5).sorted().forEach(System.out::println)
distinct: 중복 제거
limit(n): n개로 자르기
forEach: stream의 요소를 하나씩 꺼내기
List<Integer> list = Arrays.asList(3, 1, 5, 4, 2);
List<Integer> sortedList = list.stream().sorted()
.collect(Collectors.toList()); // 새로운 list에 저장
System.out.println(list); // [3, 1, 5, 4, 2]
System.out.println(sortedList); // [1, 2, 3, 4, 5]
strStream.forEach(System.out::println); // 모든 요소를 화면에 출력(최종 연산)
int numOfStr = strStream.count(); // 에러. 스트림이 이미 닫혔음.
IntStream intStream = new Random().ints(1, 46); // 1~45 범위의 무한 스트림
intStream.distinct().limit(6).sorted() // 중간 연산
.forEach(i -> System.out.print(i + ", ")); // 최종연산
무한 스트림에 대해서 중복을 제거하고 자르고 정렬하는 연산을 할 수 있는 이유는 지연된 연산 때문이다.
void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action); // 매개변수의 널 체크
for(T t : src) // 내부 반복(for문을 메서드 안으로 넣음)
action.accept(T);
}
Stream<String> strStream = Stream.of("dd", "aa", "CC", "cc", "b");
int sum = strStream.parallel()
.mapToInt(s -> s.length()).sum(); // 모든 문자열 길이의 합