Method | Description | Example |
---|---|---|
filter() | Filters elements based on a predicate. | stream.filter(e -> e > 5); |
map() | Transforms each element using a function. | stream.map(e -> e * 2); |
flatMap() | Flattens a Stream of Streams into a single Stream. | stream.flatMap(e -> Arrays.stream(e.split(” “))); |
distinct() | Removes duplicate elements from the stream. | stream.distinct(); |
sorted() | Sorts elements in natural order or by a comparator. | stream.sorted(); |
peek() | Performs an action on each element without modifying the stream. | stream.peek(e -> System.out.println(e)); |
limit() | Limits the number of elements to the given size. | stream.limit(5); |
skip() | Skips the first N elements. | stream.skip(2); |
concat() | Concatenates two streams into a single stream. | Stream.concat(stream1, stream2); |
mapToInt() | Converts each element into an int stream. |
stream.mapToInt(e -> e.length()); |
mapToLong() | Converts each element into a long stream. |
stream.mapToLong(e -> e.hashCode()); |
mapToDouble() | Converts each element into a double stream. |
stream.mapToDouble(e -> e.doubleValue()); |
boxed() | Converts a primitive stream (e.g., IntStream ) to a stream of boxed elements. |
stream.mapToInt(e -> e).boxed(); |
forEach() | Iterates over the elements, performing an action for each. | stream.forEach(e -> System.out.println(e)); |
forEachOrdered() | Iterates over elements in the order they appear in the stream. | stream.forEachOrdered(e -> System.out.println(e)); |
collect() | Collects elements into a container (e.g., List, Set). | stream.collect(Collectors.toList()); |
reduce() | Reduces the stream to a single value by applying a binary operator. | stream.reduce(0, (a, b) -> a + b); |
count() | Returns the number of elements in the stream. | stream.count(); |
min() | Returns the minimum element according to the comparator. | stream.min(Comparator.naturalOrder()); |
max() | Returns the maximum element according to the comparator. | stream.max(Comparator.naturalOrder()); |
anyMatch() | Returns whether any element matches the predicate. | stream.anyMatch(e -> e > 5); |
allMatch() | Returns whether all elements match the predicate. | stream.allMatch(e -> e > 5); |
noneMatch() | Returns whether no element matches the predicate. | stream.noneMatch(e -> e > 5); |
findFirst() | Returns the first element in the stream. | stream.findFirst(); |
findAny() | Returns any element in the stream (may not be the first). | stream.findAny(); |
toArray() | Converts the stream into an array. | stream.toArray(); |
of() | Creates a stream from a sequence of elements. | Stream.of(1, 2, 3); |
empty() | Returns an empty stream. | Stream.empty(); |
iterate() | Generates an infinite stream based on a function. | Stream.iterate(0, n -> n + 1).limit(5); |
generate() | Generates an infinite stream using a supplier. | Stream.generate(Math::random).limit(5); |
concat() | Concatenates multiple streams into one stream. | Stream.concat(stream1, stream2); |
builder() | Creates a stream builder. | Stream.builder().add(1).add(2).build(); |
sum() | Computes the sum of elements in a stream. | int sum = intStream.sum(); |
average() | Computes the average of elements in a stream. | OptionalDouble avg = doubleStream.average(); |
min() | Returns the minimum element in the stream. | OptionalInt min = intStream.min(); |
max() | Returns the maximum element in the stream. | OptionalInt max = intStream.max(); |
reduce() | Reduces the stream to a single value. | OptionalInt result = intStream.reduce((a, b) -> a + b); |
toArray() | Converts the stream to an array of primitives. | int[] array = intStream.toArray(); |
Explore Topics