/ JAVAJUNGSUK

Ch14-23~25. 스트림 연산

자바의 정석 기초편

0. 목차



Chapter14. 람다와 스트림

Ch14 - 23. 스트림의 연산

Ch14 - 24. 스트림의 연산 : 중간연산

Ch14 - 25. 스트림의 연산 : 중간연산



Ch14 - 23. 스트림의 연산


▶ 스트림이 제공하는 기능

▷ 중간 연산 : 연산 결과가 스트림O, 0 ~ n번 가능
▷ 최종 연산 : 연산 결과가 스트림X, 1번만 가능(스트림의 요소를 소모해서)




Ch14 - 24. 스트림의 연산 : 중간연산


distinct()

 Stream<T> distinct()
▷ 중복을 제거



filter()

 Stream<T> filter(Predicate<T> predicate)
▷ 조건에 안 맞는 요소 제외



limit()

 Stream<T> limit(long maxSize)
▷ 스트림의 일부 자르기



skip()

 Stream<T> skip(long n)
▷ 스트림의 일부 건너뛰기



peek()

 Stream<T> peek(Consumer<T> action)
▷ 스트림의 요소에 작업 수행



sorted()

 Stream<T> sorted()
 Stream<T> sorted(Comparator<T> comparator)
▷ 스트림의 요소 정렬



map()

 Stream<R> map(Function<T, R> mapper)
 DoubleStream() mapToDouble(ToDoubleFunction<T> mapper)
 IntStream() mapToInt(ToDoubleFunction<T> mapper)
 LongStream() mapToLong(ToLongFunction<T> mapper)
▷ 스트림의 요소를 변환



flatMap()

 Stream<R> flatMap(Function<T, Stream<R> mapper)
 DoubleStream() flatMapToDouble(Function<T, DoubleStream> mapper)
 IntStream() flatMapToInt(Function<T, IntStream> mapper)
 LongStream() flatMapToLong(Function<T, LongStream> mapper)
▷ 스트림의 요소를 변환





Ch14 - 25. 스트림의 연산 : 최종연산


forEach()

 void forEach(Consumer<? super T> action)
 void forEachOrdered(Consumer<? super T> action) // 병렬 스트림, 순서O
▷ 각 요소에 지정 된 작업 수행



count()

 long count()
▷ 스트림의 요소 개수 반환



max(), min()

 Optional<T> max(Comparator<? super T> comparator)
 Optional<T> min(Comparator<? super T> comparator)
▷ 스트림의 최대값/최소값을 반환
Optional = 래퍼 클래스 = 값을 Optional 객체로 감싸서 반환
  • 작업 결과가 null이라도 그냥 반환하지 않고 객체로 감싸서 반환


find()

 Optional<T> findAny() // 아무거나 하나
 Optional<T> findFirst() // 첫 번째 요소
▷ 스트림의 요소 하나를 반환
filter()와 사용, 조건에 맞도록 반환
Optional 객체로 감싸서 작업결과 반환



Match()

boolean allMatch(Predicate<T> p) // 모두 만족하는가
boolean anyMatch(Predicate<T> p) // 하나라도 만족하는가
boolean noneMatch(Predicate<T> p) // 모두 만족하지않는가
▷ 주어진 조건을 얼마나 만족하는가 확인



toArray()

 Object[] toArray()
 A[] toArray(IntFunction<A[]> generator)
▷ 스트림의 모든 요소를 배열로 변환



reduce()

 Optional<T> reduce(BinaryOperator<T> accumulator)
 T reduce(T identity, BinaryOperator<T> accumulator)
 U reduce(U identity, BiFunction<U, T, U> accumulator, BinaryOperator<U> combiner)
▷ 스트림의 요소를 하나씩 줄여가며(rdeducing) 계산



collect()

 R collect(Collector<T, A, R> collector)`
 R collect(Supplier<R> supplier, BiConsumer<R, T> accumulator, BiConsumer<R, R> combiner)
▷ 스트림의 요소를 수집
▷ 주로 요소를 그룹화하거나 분활한 결과를 컬렉션에 담아 반환하는 데 사용