Middle
What are the final methods of working with streams that you know?
sobes.tech AI
Answer from AI
Java stream terminal methods consume stream elements and produce a result:
forEach(Consumer<? super T> action): Performs an action for each element in the stream. Returnsvoid.// Example: print each element Stream.of("a", "b", "c").forEach(System.out::println);forEachOrdered(Consumer<? super T> action): Performs an action for each element in the stream in the given order.// Example: print in order Stream.of("c", "b", "a").parallel().forEachOrdered(System.out::println);toArray(): Collects stream elements into anObject[]array.// Example: collect into Object array Object[] arr = Stream.of("a", "b").toArray();toArray(IntFunction<A[]> generator): Collects stream elements into an array of the specified type.// Example: collect into String array String[] arr = Stream.of("a", "b").toArray(String[]::new);reduce(BinaryOperator<T> accumulator): Combines stream elements using an accumulator function. Returns anOptional<T>.// Example: sum of numbers Optional<Integer> sum = Stream.of(1, 2, 3).reduce(Integer::sum);reduce(T identity, BinaryOperator<T> accumulator): Combines with a starting value.// Example: sum with start 10 Integer sum = Stream.of(1, 2, 3).reduce(10, Integer::sum);reduce(U identity, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner): For parallel streams.// Example: parallel sum Integer sum = Stream.of(1, 2, 3).parallel().reduce(0, Integer::sum, Integer::sum);collect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner): Mutable reduction.// Example: collect into list List<String> list = Stream.of("a", "b").collect(ArrayList::new, ArrayList::add, ArrayList::addAll);collect(Collector<? super T, A, R> collector): Reduction using aCollector.// Example: collect into list with Collectors List<String> list = Stream.of("a", "b").collect(Collectors.toList());min(Comparator<? super T> comparator): Finds the minimum element. Returns anOptional<T>.// Example: find minimum number Optional<Integer> min = Stream.of(3, 1, 2).min(Integer::compareTo);max(Comparator<? super T> comparator): Finds the maximum element. Returns anOptional<T>.// Example: find maximum number Optional<Integer> max = Stream.of(3, 1, 2).max(Integer::compareTo);count(): Returns the number of elements in the stream as along.// Example: count elements long count = Stream.of(1, 2, 3).count();anyMatch(Predicate<? super T> predicate): Checks if any element matches the predicate. Returnsboolean.// Example: check for even number boolean hasEven = Stream.of(1, 2, 3).anyMatch(n -> n % 2 == 0);allMatch(Predicate<? super T> predicate): Checks if all elements match the predicate. Returnsboolean.// Example: check if all are even boolean allEven = Stream.of(2, 4, 6).allMatch(n -> n % 2 == 0);noneMatch(Predicate<? super T> predicate): Checks if no elements match the predicate. Returnsboolean.// Example: check absence of negative numbers boolean noneNegative = Stream.of(1, 2, 3).noneMatch(n -> n < 0);findFirst(): Finds the first element in the stream. Returns anOptional<T>.// Example: find first element Optional<String> first = Stream.of("a", "b").findFirst();findAny(): Finds any element in the stream (especially useful for parallel streams). Returns anOptional<T>.// Example: find any element (may not be the first in parallel stream) Optional<String> any = Stream.of("a", "b").parallel().findAny();
Specialized terminal operations exist for IntStream, LongStream, DoubleStream, such as sum(), average(), summaryStatistics().
// Example: sum of int stream
int sum = IntStream.of(1, 2, 3).sum();
// Example: average of long stream
OptionalDouble average = LongStream.of(1, 2, 3).average();
// Example: statistics of double stream
DoubleSummaryStatistics stats = DoubleStream.of(1.0, 2.0, 3.0).summaryStatistics();