Java.Java8.What intermediate methods for working with streams do you know?

✅ Common Intermediate Stream Methods

Here’s a full list of the most important ones grouped by purpose:


🔹 1. Transformation

MethodDescription
map()Transforms each element
flatMap()Flattens nested streams
mapToInt(), mapToDouble(), mapToLong()Special for primitives

Example:

stream.map(String::toUpperCase);

🔹 2. Filtering

MethodDescription
filter()Keeps elements that match a condition
distinct()Removes duplicates

Example:

stream.filter(s -> s.length() > 3);

🔹 3. Slicing

MethodDescription
limit(n)Keeps only the first n elements
skip(n)Skips the first n elements

Example:

stream.skip(2).limit(5);

🔹 4. Sorting

MethodDescription
sorted()Sorts elements in natural or custom order

Example:

stream.sorted(Comparator.reverseOrder());

🔹 5. Peeking (for debugging/logging)

MethodDescription
peek()Performs an action on each element without changing it

Example:

stream.peek(System.out::println);

🔹 6. Boxing & Conversion (for primitive streams)

MethodDescription
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

CategoryMethods
Transformmap(), flatMap(), mapToInt() etc.
Filterfilter(), distinct()
Slicelimit(), skip()
Sortsorted()
Debug/Inspectpeek()
Primitive Opsboxed(), asDoubleStream(), asLongStream()
This entry was posted in Без рубрики. Bookmark the permalink.

Leave a Reply

Your email address will not be published.