✅ Common Intermediate Stream Methods
Here’s a full list of the most important ones grouped by purpose:
🔹 1. Transformation
Method | Description |
---|
map() | Transforms each element |
flatMap() | Flattens nested streams |
mapToInt() , mapToDouble() , mapToLong() | Special for primitives |
Example:
stream.map(String::toUpperCase);
🔹 2. Filtering
Method | Description |
---|
filter() | Keeps elements that match a condition |
distinct() | Removes duplicates |
Example:
stream.filter(s -> s.length() > 3);
🔹 3. Slicing
Method | Description |
---|
limit(n) | Keeps only the first n elements |
skip(n) | Skips the first n elements |
Example:
stream.skip(2).limit(5);
🔹 4. Sorting
Method | Description |
---|
sorted() | Sorts elements in natural or custom order |
Example:
stream.sorted(Comparator.reverseOrder());
🔹 5. Peeking (for debugging/logging)
Method | Description |
---|
peek() | Performs an action on each element without changing it |
Example:
stream.peek(System.out::println);
🔹 6. Boxing & Conversion (for primitive streams)
Method | Description |
---|
boxed() | Converts primitive stream to object stream |
asDoubleStream() , asLongStream() | Converts IntStream to other primitive types |
Example:
IntStream.range(1, 5).boxed(); // Stream<Integer>
🧠 Summary Table
Category | Methods |
---|
Transform | map() , flatMap() , mapToInt() etc. |
Filter | filter() , distinct() |
Slice | limit() , skip() |
Sort | sorted() |
Debug/Inspect | peek() |
Primitive Ops | boxed() , asDoubleStream() , asLongStream() |