[go: up one dir, main page]

0% found this document useful (0 votes)
12 views16 pages

Stream Operations - 3 - Peek - Distinct - Limit - Skip

The document outlines various stream operations in Java 8+, focusing on sorting, removing duplicates, debugging, and limiting or skipping elements in streams. It discusses the use of methods like sorted(), distinct(), peek(), limit(), and skip() with examples related to employee data. Additionally, it highlights the importance of implementing the Comparable interface for natural ordering and provides a project structure involving model classes and repository/service layers.

Uploaded by

aarav rubral
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views16 pages

Stream Operations - 3 - Peek - Distinct - Limit - Skip

The document outlines various stream operations in Java 8+, focusing on sorting, removing duplicates, debugging, and limiting or skipping elements in streams. It discusses the use of methods like sorted(), distinct(), peek(), limit(), and skip() with examples related to employee data. Additionally, it highlights the importance of implementing the Comparable interface for natural ordering and provides a project structure involving model classes and repository/service layers.

Uploaded by

aarav rubral
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

STREAM OPERATIONS - 3

Java 8+
Quick Recap -
Project Structure:

Model Classes:
1. Employee
2. Department
3. Address

EmpDeptDto: DTO class having some fields.

Repository Layer: Service Layer:

1. EmployeeRepository 1. EmployeeService

2. DepartmentRepository 2. DepartmentService

Database: DatabaseProxy class consists of static data


Project Structure:

#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.

The sorted() method

1. sorted() (Natural Order): Uses the natural ordering of


elements, requiring them to implement the Comparable
interface.
2. sorted(Comparator c) (Custom Order): Accepts a Comparator to
define a custom sorting order.

Primitive Wrapper Types, Strings,


and Other Predefined Comparable Types

Primitive wrapper classes in Java (e.g., Integer, Double), String, and


other predefined types (e.g., LocalDate, BigDecimal) already
implement the Comparable interface. Therefore, sorting a list of
these types works seamlessly

#CodingWalaShree
STREAM OPERATIONS - 3
Java 8+
Quick recap -
Stream Operation - sorted (Comparable)

Example: Applying sorted() on Integers

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

Example: Sort all employees by experience in years in ascending


fashion.

Output:

#CodingWalaShree
STREAM OPERATIONS - 3
Java 8+

Removing Duplicates from Streams - distinct()


distinct() method is used to remove duplicates from a stream
distinct() is a Stateful Intermediate Operation that returns a
stream consisting of the distinct elements (according to Object.
equals(Object)) of this stream.
Duplicate data is a common issue and distinct() helps clean it up.

#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.

That’s how we can debug how


elements are flowing through
the stream at a point in time.

#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:

2. Get top 3 highest paid employees (EmployeeRepository)

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+

Example 1 - Skip first 3 elements from stream of strings

Output:

2 - Skip top 3 highest paid employees and get the remaining


employees (EmployeeRepository)

Output:

#CodingWalaShree
STREAM OPERATIONS - 3
Java 8+

Example 3 - The company has already shortlisted the


top 3 highest-paid employees. Now, we need to fetch the
next 2 highest-paid employees for further evaluation.

Output:

#CodingWalaShree
STREAM OPERATIONS - 3
Java 8+

Example 3 - The company has already shortlisted the


top 3 highest-paid employees. Now, we need to fetch the
next 2 highest-paid employees for further evaluation.

Output:

#CodingWalaShree
STREAM OPERATIONS - 3
Java 8+

Subscribe
CodingWalaShree
Examples in this presentation are covered in my YouTube Video on
Stream API Part 5🚀

Note: As I cover more Stream operations in upcoming videos on


CodingWalaShree, I’ll update this presentation and share it on
LinkedIn — so stay tuned! 🚀
To be continued. . .

Follow me on https://www.linkedin.com/in/shrikrishna-
prabhumirashi-717b2356/ for interesting insights.

#CodingWalaShree

You might also like