Slides Cours11 Streams
Slides Cours11 Streams
2023-12-14
Why functional programming?
Code that is easier to reason about (shorter, clarity…)
Functional interfaces
Improved uncoupling of code
Lambda expressions
Better handling of concurrency
Stream API
Why functional programming?
Code that is easier to reason about (shorter, clarity…)
Functional interfaces
Improved uncoupling of code Last week
Lambda expressions
Better handling of concurrency
Stream API
Why functional programming?
Code that is easier to reason about (shorter, clarity…)
Functional interfaces
Improved uncoupling of code Last week
Lambda expressions
Better handling of concurrency
Today
Stream API
Image credits: https://innovationm.co/concept-of-stream-api-java1-8/
A stream is sequence of
elements of the same type
accessed one after the other
Streams can be
constructed out of
Java collections
From miles to kilometers using streams
Streams can be
constructed out of
Java collections
Lambda expressions
are used to declare
the stream pipeline
From miles to kilometers using streams
Streams can be
constructed out of
Java collections
Lambda expressions
are used to declare
the stream pipeline
Intermediate methods
(transform successive streams)
Anatomy of a stream pipeline
Intermediate methods
(transform successive streams)
Streams are frequently created out of one of the standard Java collections
f(seed)
f(f(seed))
f(f(f(seed)))
…
Case 5: Declaring an infinite stream
f(seed)
f(f(seed))
f(seed)
f(f(seed))
Delegation to a
Comparator<T> is possible if
natural order is not suitable
Skipping or limiting streams
Skipping or limiting streams
The java.util.stream.Collectors
class contains predefined collectors that
are sufficient for most use cases
Case 3: Reduction methods
Output:
a: 1
b: 2
a: 2
b: 3
a: 3
b: 4
About interleaving
This is a recursion!
Evaluation
Please help us improve this brand new course!
https://forms.office.com/Pages/ResponsePage.aspx?id=1JCwei76z068fEEntNWC7J5qKCyxXcROjhQa15tvWb9URURTSlZYVVRXRldJS1JIQlpJVDZES1ZMQy4u