0% found this document useful (0 votes)
2 views10 pages

Intermediate Operations

The document discusses various intermediate operations in streams, including filter(), distinct(), limit(), map(), flatMap(), and sorted(). Each operation has specific functionalities, such as removing duplicates, transforming data, and sorting elements. These operations are stateful and produce a stream as a result, unlike terminal operations.

Uploaded by

gs23133
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)
2 views10 pages

Intermediate Operations

The document discusses various intermediate operations in streams, including filter(), distinct(), limit(), map(), flatMap(), and sorted(). Each operation has specific functionalities, such as removing duplicates, transforming data, and sorting elements. These operations are stateful and produce a stream as a result, unlike terminal operations.

Uploaded by

gs23133
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/ 10

Streams

Intermediate Operations

Copyright © Seán Kennedy


Intermediate Operations
filter()

• Unlike a terminal operation, an intermediate operation


produces a stream as a result.

Copyright © Seán Kennedy


Intermediate Operations
distinct()

• distinct() returns a stream with duplicate values removed.


• equals() is used i.e. case sensitive.

• distinct() is a stateful intermediate operation.


• it behaves like a filter – if it has not seen the object previously, it
passes it on and remembers it; if it has seen it already, it filters it
out.

Copyright © Seán Kennedy


Intermediate Operations
distinct()

Copyright © Seán Kennedy


Intermediate Operations
limit()
• limit() is a short-circuiting stateful intermediate operation.

Copyright © Seán Kennedy


Intermediate Operations
map()
• map() creates a one-to-one mapping between elements in
the stream and elements in the next stage of the stream.

• map() is for transforming data.

Copyright © Seán Kennedy


Intermediate Operations
flatMap()
• flatMap() takes each element in the stream
e.g. Stream<List<String>> and makes any elements it contains
top-level elements in a single stream e.g. Stream<String>.

Copyright © Seán Kennedy


Intermediate Operations
sorted()
• sorted() returns a stream with the elements sorted.

• Just like sorting arrays, Java uses natural ordering unless we


provide a comparator.

• sorted() is a stateful intermediate operation; it needs to see


all of the data before it can sort it.

Copyright © Seán Kennedy


Intermediate Operations
sorted()

10

Copyright © Seán Kennedy


Intermediate Operations
sorted(Comparator)

11

Copyright © Seán Kennedy

You might also like