Stream Operations - 3 - Peek - Distinct - Limit - Skip
Stream Operations - 3 - Peek - Distinct - Limit - Skip
Java 8+
Quick Recap -
Project Structure:
Model Classes:
1. Employee
2. Department
3. Address
1. EmployeeRepository 1. EmployeeService
2. DepartmentRepository 2. DepartmentService
#CodingWalaShree
STREAM OPERATIONS - 3
Java 8+
Quick Recap -
Sorting Streams:
Sorting is a fundamental operation in programming, often required
when dealing with collections of data. In Java, the Stream API
provides a convenient way to perform sorting using the sorted()
method.
#CodingWalaShree
STREAM OPERATIONS - 3
Java 8+
Quick recap -
Stream Operation - sorted (Comparable)
Output:
#CodingWalaShree
STREAM OPERATIONS - 3
Java 8+
Quick recap -
Sort after Implementing Comparable
Implement Comparable interface and override compareTo()
method to enable natural ordering .
Operations like sorting and grouping are based on ordering
criteria. When you implement Comparable interface, you provide
your class capability to order instances of it’s own type based on
ordering key.
sorted() method without any argument can be used for natural
sorting
Example: Applying sorted() on Employees class after implementing
Comparable - sorting on employee ID
Output:
#CodingWalaShree
STREAM OPERATIONS - 3
Java 8+
Quick recap -
The sorted() with Comparator
Output:
#CodingWalaShree
STREAM OPERATIONS - 3
Java 8+
#CodingWalaShree
STREAM OPERATIONS - 3
Java 8+
Examples - distinct()
Output:
#CodingWalaShree
STREAM OPERATIONS - 3
Java 8+
Debugging Streams - peek()
peek() is an intermediate operation.
It returns a stream consisting of the elements of this stream,
additionally performing the provided action on each element as
elements are consumed from the resulting stream.
peek() method exists mainly to support debugging, where you
want to see the elements as they flow past a certain point in a
pipeline.
peek() method is used to observe elements during processing
without changing it.
#CodingWalaShree
STREAM OPERATIONS - 3
Java 8+
Examples - peek()
Output:
Observe that after distinct
statements are printed only
for distinct elements.
#CodingWalaShree
STREAM OPERATIONS - 3
Java 8+
Fetch ‘n’ elements from a Stream - limit()
limit(n) method returns a stream consisting of the elements of
this stream, truncated to be no longer than maxSize in length.
This is a short-circuiting stateful intermediate operation.
limit(n) stops processing the stream after retrieving n elements,
even if more elements are present; so it is a short-circuiting
intermediate operation.
limit(n) must count the elements as it processes the stream to
determine when to stop; so it is a stateful intermediate operation
#CodingWalaShree
STREAM OPERATIONS - 3
Java 8+
Example 1 - Limit results from stream of strings
Output:
Output:
#CodingWalaShree
STREAM OPERATIONS - 3
Java 8+
Ignore ‘n’ elements from a Stream - skip()
skip(n) method returns a stream consisting of the remaining
elements of this stream after discarding the first n elements of
the stream. If this stream contains fewer than n elements then an
empty stream will be returned.
This is a stateful intermediate operation; as it tracks and
discards the first n elements before passing the remaining ones
to the next operation in the pipeline.
Unlike limit(n), skip(n) is not short-circuiting. skip(n) does not
stop processing the stream early — it only ensures the first n
elements are ignored before continuing with the rest.
#CodingWalaShree
STREAM OPERATIONS - 3
Java 8+
Output:
Output:
#CodingWalaShree
STREAM OPERATIONS - 3
Java 8+
Output:
#CodingWalaShree
STREAM OPERATIONS - 3
Java 8+
Output:
#CodingWalaShree
STREAM OPERATIONS - 3
Java 8+
Subscribe
CodingWalaShree
Examples in this presentation are covered in my YouTube Video on
Stream API Part 5🚀
Follow me on https://www.linkedin.com/in/shrikrishna-
prabhumirashi-717b2356/ for interesting insights.
#CodingWalaShree