Sobes.tech
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:

  1. forEach(Consumer<? super T> action): Performs an action for each element in the stream. Returns void.
    // Example: print each element
    Stream.of("a", "b", "c").forEach(System.out::println);
    
  2. 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);
    
  3. toArray(): Collects stream elements into an Object[] array.
    // Example: collect into Object array
    Object[] arr = Stream.of("a", "b").toArray();
    
  4. 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);
    
  5. reduce(BinaryOperator<T> accumulator): Combines stream elements using an accumulator function. Returns an Optional<T>.
    // Example: sum of numbers
    Optional<Integer> sum = Stream.of(1, 2, 3).reduce(Integer::sum);
    
  6. 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);
    
  7. 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);
    
  8. 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);
    
  9. collect(Collector<? super T, A, R> collector): Reduction using a Collector.
    // Example: collect into list with Collectors
    List<String> list = Stream.of("a", "b").collect(Collectors.toList());
    
  10. min(Comparator<? super T> comparator): Finds the minimum element. Returns an Optional<T>.
    // Example: find minimum number
    Optional<Integer> min = Stream.of(3, 1, 2).min(Integer::compareTo);
    
  11. max(Comparator<? super T> comparator): Finds the maximum element. Returns an Optional<T>.
    // Example: find maximum number
    Optional<Integer> max = Stream.of(3, 1, 2).max(Integer::compareTo);
    
  12. count(): Returns the number of elements in the stream as a long.
    // Example: count elements
    long count = Stream.of(1, 2, 3).count();
    
  13. anyMatch(Predicate<? super T> predicate): Checks if any element matches the predicate. Returns boolean.
    // Example: check for even number
    boolean hasEven = Stream.of(1, 2, 3).anyMatch(n -> n % 2 == 0);
    
  14. allMatch(Predicate<? super T> predicate): Checks if all elements match the predicate. Returns boolean.
    // Example: check if all are even
    boolean allEven = Stream.of(2, 4, 6).allMatch(n -> n % 2 == 0);
    
  15. noneMatch(Predicate<? super T> predicate): Checks if no elements match the predicate. Returns boolean.
    // Example: check absence of negative numbers
    boolean noneNegative = Stream.of(1, 2, 3).noneMatch(n -> n < 0);
    
  16. findFirst(): Finds the first element in the stream. Returns an Optional<T>.
    // Example: find first element
    Optional<String> first = Stream.of("a", "b").findFirst();
    
  17. findAny(): Finds any element in the stream (especially useful for parallel streams). Returns an Optional<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();
What are the final methods of working with streams… - sobes.tech