Operators in Java Streams
1️. Source Operators
Create a stream from a collection, array, or I/O source.
• stream() – Converts a collection into a sequential stream.
• parallelStream() – Creates a parallel stream for multi-threading.
• Stream.of() – Generates a stream from given values.
• Arrays.stream() – Converts an array into a stream.
• IntStream.range() – Creates a stream of numbers within a range.
• BufferedReader.lines() – Converts lines of a file into a stream.
2️. Intermediate Operators
Transform or filter data without executing immediately (lazy evaluation).
• filter() – Selects elements that match a given condition.
• map() – Transforms each element using a function.
• flatMap() – Flattens nested structures into a single stream.
• distinct() – Removes duplicate elements from the stream.
• sorted() – Sorts elements in natural or custom order.
• limit(n) – Restricts the stream to the first n elements.
• skip(n) – Skips the first n elements in the stream.
• peek() – Used for debugging without modifying elements.
3️. Terminal Operators
Trigger execution and produce a final result, consuming the stream.
• collect() – Converts a stream into a List, Set, or Map.
• forEach() – Performs an action for each element.
• count() – Returns the number of elements in the stream.
• findFirst() – Retrieves the first element of the stream.
• findAny() – Retrieves any element from the stream.
• reduce() – Aggregates elements into a single result.
• toArray() – Converts stream elements into an array.
4️. Short-Circuiting Operators
Stop execution early when a condition is met for better performance.
• anyMatch() – Returns true if any element matches a condition.
• allMatch() – Returns true if all elements match a condition.
• noneMatch() – Returns true if no elements match a condition.
• limit(n) – Stops processing after selecting n elements.
• findFirst() – Retrieves the first element and stops further operations.