Java 25 (LTS) reference · 12 entries
Java Stream API
The Java Stream API: filter(), map(), reduce(), sorted(), distinct(), limit(), toList(), anyMatch(), Collectors.joining() and groupingBy(), with runnable examples.
A stream pipeline has a source (list.stream()), lazy intermediate operations (filter, map, sorted)
and one terminal operation (toList, count, collect) that runs it. Streams don't change their source.
| Name | Description | Since |
|---|---|---|
stream.filter() |
Keep only the elements that match a condition. Intermediate and lazy: nothing runs until a terminal operation. | Java 8 |
stream.map() |
Transform each element with a function and return a stream of the results. | Java 8 |
stream.toList() |
Collect a stream's elements into an unmodifiable List. Shorter than collect(Collectors.toList()). | Java 16 |
stream.reduce() |
Combine all elements into one value with an accumulator function, starting from an identity value. | Java 8 |
stream.mapToInt() |
Map to an IntStream of primitive ints, which has sum(), average(), min(), max() and summaryStatistics(). | Java 8 |
stream.sorted() |
Return a stream sorted by natural order or by a Comparator. The source collection is not changed. | Java 8 |
stream.distinct() |
Remove duplicate elements (by equals()), keeping the first occurrence of each, in encounter order. | Java 8 |
stream.limit() |
Keep at most the first n elements. skip(n) drops the first n. Together they paginate a stream. | Java 8 |
stream.count() |
Return the number of elements in the stream (a long). A terminal operation. | Java 8 |
stream.anyMatch() |
Return true if any element matches a condition, stopping at the first match. allMatch() and noneMatch() are similar. | Java 8 |
stream.collect(Collectors.joining()) |
Join a stream of strings into one string, with an optional separator, prefix and suffix. | Java 8 |
stream.collect(Collectors.groupingBy()) |
Group elements into a Map by a key function, optionally counting or summing each group. | Java 8 |