Skip to content

Instantly share code, notes, and snippets.

@rolroralra
Last active September 18, 2022 06:40
Show Gist options
  • Save rolroralra/1d63b150028f3aba6af07298aa2b4fc8 to your computer and use it in GitHub Desktop.
Save rolroralra/1d63b150028f3aba6af07298aa2b4fc8 to your computer and use it in GitHub Desktop.
Stream (Java)

Intermediate Operation (중간 연산)

Operation Stateless Style Return Type Parameter Function Descriptor Description
filter Filtering Stream<T> Predicate<T> T -> boolean
distinct stateful, unbound Filtering Stream<T>
sorted stateful, unbound Sorting Stream<T> Comparator<T> (T, T) -> int
takeWhile Slicing Stream<T> Predicate<T> T -> boolean limit
dropWhile Slicing Stream<T> Predicate<T> T -> boolean skip
limit stateful, bound Slicing Stream<T> long short circuit
skip stateful, bound Slicing Stream<T> long
map Mapping Stream<T> Function<T, R> T -> R
flatMap Mapping Stream<T> Function<T, Stream<R>> T -> Stream<R>

Terminal Operation (최종 연산)

Operation Stateless Style Return Type Parameter Function Descriptor Description
anyMatch Matching boolean Predicate<T> T -> boolean short circuit
allMatch Matching boolean Predicate<T> T -> boolean short circuit
noneMatch Matching boolean Predicate<T> T -> boolean short circuit
findAny Finding Optional<T>
findFirst Finding Optional<T>
forEach Iteration void Consumer<T> T -> void
collect Reduce R Collector<T, A, R> Supplier<A> supplier

BiConsumer<A, T> accumulator

BinaryOperator<A> combiner

Function<A, R> finisher
reduce stateful, bound Reduce Optional<T> BinaryOperator<T> (T, T) -> T
count Reduce long

Filtering

Operation Operation Type Style Return Type Parameter Function Descriptor Description
filter intermediate Filtering Stream<T> Predicate<T> T -> boolean
distinct intermediate Filtering Stream<T>

Slicing

Operation Operation Type Style Return Type Parameter Function Descriptor Description
takeWhile intermediate Slicing Stream<T> Predicate<T> T -> boolean limit
dropWhile intermediate Slicing Stream<T> Predicate<T> T -> boolean skip
limit intermediate Slicing Stream<T> long short circuit
skip intermediate Slicing Stream<T> long

Mapping

Operation Operation Type Style Return Type Parameter Function Descriptor Description
map intermediate Mapping Stream<T> Function<T, R> T -> R
flatMap intermediate Mapping Stream<T> Function<T, Stream<R>> T -> Stream<R>

Matching

Operation Operation Type Style Return Type Parameter Function Descriptor Description
anyMatch terminal Matching boolean Predicate<T> T -> boolean short circuit
allMatch terminal Matching boolean Predicate<T> T -> boolean short circuit
noneMatch terminal Matching boolean Predicate<T> T -> boolean short circuit

Finding

Operation Operation Type Style Return Type Parameter Function Descriptor Description
findAny terminal Finding Optional<T> Short Circuit
findFirst terminal Finding Optional<T> Short Circuit

Reduce (Fold)

Operation Operation Type Style Return Type Parameter Function Descriptor Description
forEach terminal Iteration void Consumer<T> T -> void
collect terminal Reduce R Collector<T, A, R> Supplier<A> supplier

BiConsumer<A, T> accumulator

BinaryOperator<A> combiner

Function<A, R> finisher
reduce termianl Reduce Optional<T> BinaryOperator<T> (T, T) -> T
count terminal Reduce long

Collector

interface Collector<T, A, R> {
  Supplier<A> supplier();
  BiConsumer<A, T> accumulator();
  BinaryOperator<A> combiner();
  Function<A, R> finisher();
  Set<Characteristics> characteristics();
}
Static Method Collector Type (Return Type) Collect Type (Reduce Type) Parameter Types
toList Collector<T, ?, List<T>> List<T>
toSet Collector<T, ?, Set<T>> Set<T>
toCollection Collector<T, ?, C> C extends Collection<T> Supplier<C>
joining Collector<CharSequence, ?, String> String CharSequence delimiter (optional)
CharSequence prefix (optional)
CharSequence suffix (optional)
counting Collector<T, ?, Long> Long
summingInt
summingLong
summingDouble
Collector<T, ?, Integer>
Collector<T, ?, Long>
Collector<T, ?, Double>
Integer
Long
Double
ToIntFunction<? super T>
ToLongFunction<? super T>
ToDoubleFunction<? super T>
averagingInt
averagingLong
averagingDouble
Collector<T, ?, Double>
Collector<T, ?, Double>
Collector<T, ?, Double>
Double
Double
Double
ToIntFunction<? super T>
ToLongFunction<? super T>
ToDoubleFunction<? super T>
summarizingInt
summarizingLong
summarizingDouble
Collector<T, ?, IntSummaryStatistics>
Collector<T, ?, LongSummaryStatistics>
Collector<T, ?, DoubleSummaryStatistics>
IntSummaryStatistics
LongSummaryStatistics
DoubleSummaryStatistics
ToIntFunction<? super T>
ToLongFunction<? super T>
ToDoubleFunction<? super T>
maxBy Collector<T, ?, Optional<T>> Optional<T> Comparator<? super T>
minBy Collector<T, ?, Optional<T>> Optional<T> Comparator<? super T>
mapping Collector<T, ?, R> R Function<? super T, ? extends U> mapper
Collector<? super U, A, R> downstream
flatMapping Collector<T, ?, R> R Function<? super T, ? extends Stream<? extends U>> mapper
Collector<? super U, A, R> downstream
filtering Collector<T, ?, R> R Predicate<? super T> predicate
Collector<? super T, A, R> downstream
reducing Collector<T, ?, T>
Collector<T, ?, Optional<T>>
Collector<T, ?, U>
T
Optional<T>
U
T identity, BinaryOperator<T> op
BinaryOperator<T> op
U identity, Function<? super T, ? extends U> mapper, BinaryOperator<U> op
collectingAndThen Collector<T, A, RR> RR Collector<T, A, R> downstream, Function<R, RR> finisher
groupingBy Collector<T, ?, Map<K, List<T>>>
Collector<T, ?, Map<K, D>>
Collector<T, ?, M>
Map<K, List<T>>
Map<K, D>
M extends Map<K, D>
Function<? super T, ? extends K> classfier
Function<? super T, ? extends K> classfier, Collector<? super T, A, D> downstream
Function<? super T, ? extends k> classfier, Supplier<M> mapFactory, Collector<? super T, A, D>
partitioningBy Collector<T, ?, Map<Boolean, List<T>>>
Collector<T, ?, Map<Boolean, D>
Map<Boolean, List<T>>
Map<Boolean, D>
Predicate<? super T> predicate
Predicate<? super T> predicate, Collector<? super T, A, D> downstream
teeing Collector<T, ?, R> R Collector<? super T, ?, R1> downstream1, Collector<? super T, ?, R2> downstream2, BiFunction<? super R1, ? super R2, R> merger

Parallel Stream

https://n1tjrgns.tistory.com/292


Comparator

public interface Comparator<T> {
  int compare(T o1, T o2);
}
Static Method Parameter Types Return Type
comparing Function<? super T, ? extends U> keyExtractor,
Comparator<? super U> keyComparator (optional)
Comparator<T>
comparingInt
comparingLong
comparingDouble
ToIntFunction<? super T> keyExtractor
ToLongFunction<? super T> keyExtractor
ToDoubleFunction<? super T> keyExtractor
Comparator<T>
naturalOrder Comparator<T>
reverseOrder Comparator<T>
nullsFirst Comparator<? super T> comparator Comparator<T>
nullsLast Comparator<? super T> comparator Comparator<T>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment