Skip to content

Java Stream API

The Java Stream API is a feature introduced in Java 8 that facilitates functional-style operations on collections of elements.^[600-developer__java__java8__java8.md]

Stream Creation

Streams can be instantiated from various data sources, including arrays, existing collections, or static utility methods.^[600-developer__java__java8__java8.md]

  • From Arrays: The Arrays.stream method supports int, long, double, and object arrays^[600-developer__java__java8__java8.md].
  • From Collections: The Collection.stream() method (e.g., on a List) creates a sequential stream^[600-developer__java__java8__java8.md].
  • Static Methods: Stream.of() creates a stream from specific values, and Stream.concat() merges two streams into one^[600-developer__java__java8__java8.md].

Common Operations

Intermediate Operations

These operations transform a stream into another stream and are typically lazy.

  • filter(Predicate): Excludes elements that do not match the given condition^[600-developer__java__java8__java8.md].
  • map(Function): Applies a function to each element to produce a new stream (e.g., transforming objects or extracting properties)^[600-developer__java__java8__java8.md].
  • flatMap: Transforms each element into a stream, flattening the result into a single stream^[600-developer__java__java8__java8.md].
  • distinct: Removes duplicate elements from the stream^[600-developer__java__java8__java8.md].
  • sorted: Sorts elements using natural order or a provided Comparator^[600-developer__java__java8__java8.md].
  • limit(long): Truncates the stream to a specified length^[600-developer__java__java8__java8.md].

Terminal Operations

These operations consume the stream to produce a result or a side-effect.

  • forEach / forEachOrdered: Iterates over elements; forEachOrdered respects the encounter order of the source stream^[600-developer__java__java8__java8.md].
  • collect: Accumulates elements into a mutable container (e.g., List, Set, Map) using a Collector^[600-developer__java__java8__java8.md].
  • reduce: Performs a reduction on the elements to produce a single summary result (e.g., sum or concatenation)^[600-developer__java__java8__java8.md].
  • count: Returns the number of elements in the stream^[600-developer__java__java8__java8.md].
  • min / max: Finds the minimum or maximum element based on a comparator^[600-developer__java__java8__java8.md].
  • findFirst / findAny: Returns an Optional describing the first or any element of the stream^[600-developer__java__java8__java8.md].
  • anyMatch / allMatch / noneMatch: Returns a boolean indicating whether elements match the given predicate^[600-developer__java__java8__java8.md].

Collectors

The Collectors class provides common implementations of the Collector interface for use with the collect operation.^[600-developer__java__java8__java8.md]

Grouping

  • groupingBy: Classifies elements into a Map<K, List> based on a classifier function^[600-developer__java__java8__java8.md]. It supports multi-level grouping and downstream collectors (e.g., summing, finding max)^[600-developer__java__java8__java8.md].

Reduction and Statistical

  • summingInt / summingLong / summingDouble: Produces the sum of a numeric property^[600-developer__java__java8__java8.md].
  • averagingInt / averagingLong / averagingDouble: Calculates the arithmetic mean^[600-developer__java__java8__java8.md].
  • counting: Counts the number of input elements^[600-developer__java__java8__java8.md].
  • maxBy / minBy: Finds the maximum or minimum element based on a comparator^[600-developer__java__java8__java8.md].

Transformation

  • toList / toSet: Accumulates elements into a List or Set^[600-developer__java__java8__java8.md].
  • toMap: Transforms elements into a Map^[600-developer__java__java8__java8.md].
  • toCollection: Accumulates elements into a specific Collection type (e.g., TreeSet)^[600-developer__java__java8__java8.md].
  • joining: Concatenates input elements into a String, optionally with delimiters, prefixes, and suffixes^[600-developer__java__java8__java8.md].
  • mapping: Adapts a collector to apply a mapping function before accumulation^[600-developer__java__java8__java8.md].
  • collectingAndThen: Transforms the result of a collection with a finisher function^[600-developer__java__java8__java8.md].

Method References

The Stream API often utilizes method references (::) to pass existing functions as arguments concisely^[600-developer__java__java8__java8.md].

  • Class::static_method: Refers to a static method^[600-developer__java__java8__java8.md].
  • Class::instance_method: Refers to an instance method where the first argument is the receiver of the method^[600-developer__java__java8__java8.md].
  • instance::method: Refers to a method bound to a specific object instance^[600-developer__java__java8__java8.md].
  • Class::new: References a constructor^[600-developer__java__java8__java8.md].
  • [[Functional Programming]]
  • [[Java 8]]

Sources

^[600-developer__java__java8__java8.md]