INTRODUCTION TO JAVA LAMBDAS,
STREAMS AND FUNCTIONAL
PROGRAMMING
Chonlameth ARPNIKANONDT, Ph.D.
Our MAIN text >>>
We also rely on other sources for
additional information.
2
3
4 Source: https://ondro.inginea.eu/index.php/new-features-in-java-versions-since-java-8/
5 Source: https://ondro.inginea.eu/index.php/new-features-in-java-versions-since-java-8/
6 Source: https://ondro.inginea.eu/index.php/new-features-in-java-versions-since-java-8/
7 Source: https://ondro.inginea.eu/index.php/new-features-in-java-versions-since-java-8/
8 Source: https://ondro.inginea.eu/index.php/new-features-in-java-versions-since-java-8/
More pointers
• https://en.wikipedia.org/wiki/Java_version_history
• https://advancedweb.hu/a-categorized-list-of-all-java-and-jvm-features-since-jdk-8-to-16/
• https://www.marcobehler.com/guides/a-guide-to-java-versions-and-features#_java_features_8_16
9
Traditional vs Modern Java
• Traditional
• Object-oriented
• Imperative
• Modern
• Functional
• Inherently multicore-aware
• Which version marks the beginning of “Modern Java”?
10
11 Source: https://en.wikipedia.org/wiki/Java_version_history/
12
Before
After
13
After
• Reads:
• “Sort inventory comparing apple weight”
• More concise code that reads much closer to the problem statement
• Functional-style programming
• Method reference
14
Multicore Issues
• Earlier versions of Java (traditional) see a single CPU (core)
• Parallelism/concurrency is achieved via threads
• Problems?
15
Multicore Issues
• Earlier versions of Java (traditional) see a single CPU (core)
• Parallelism/concurrency is achieved via threads (and synchronized ☺)
• Problems?
• Most, if not all, modern CPU are multicore
• Inherently non-optimal computing performance (does it matter?)
• With effort and not user-friendly and error-prone, to work with threads
16
Java 8 and beyond
• From the previous discussion, Java 8 revision (one of the most profound changes in all Java history)
gives rise to features including:
• The Streams API
• New API that supports many parallel operations to process data
• Express what you want in a higher-level manner—similar to database query languages—and the
implementation chooses the best low-level execution mechanism
• Concise techniques for passing code to methods (method references, lambdas) with behavioral
parameterization
• Default methods in interfaces
• In fact, addition of the Streams API in Java 8 could be seen as the direct cause of the other two
features mentioned above…
17
Functional-style programming
• The Java 8 feature of passing code to methods (and being able to return it and incorporate it into
data structures) also provides access to a range of additional techniques that are commonly
referred to as functional-style programming.
• Such code, called functions in the functional programming community, can be passed around and
combined in a way to produce powerful programming idioms
• Methods and lambdas (anonymous functions) in Java have evolved to become the first-class
citizens….
18
Streams API
• Java 8 adds a Streams API (note the uppercase S) in java.util.stream
• Stream<T> is a sequence of items of type T.
• The Streams API has many methods that can be chained to form a complex pipeline just like
Unix commands were chained.
• The key motivation for this is that you can now program in Java at a higher level of abstraction,
structuring your thoughts of turning a stream of this into a stream of that (similar to how you think
when writing database queries) rather than one item at a time.
• Another advantage is that Java 8 can transparently run your pipeline of Stream operations on
several CPU cores on disjoint parts of the input—this is parallelism almost for free instead of hard
work using Threads
19
INTRODUCTION TO LAMBDA
EXPRESSION
Chonlameth ARPNIKANONDT, Ph.D.
Our MAIN text >>>
We also rely on other sources for
additional information.
21
Java 8 as a big evolution…
The programming concepts the drove the design of Java 8
• Stream processing
• Passing code to methods with behavior parameterization
• Parallelism and shared mutable data
22
Functions in Java
• Methods and lambdas as first-class citizen
23
Passing code
24
As a result…
25
From passing methods to lambdas
26
Streams (before)
27
Streams (after)
28
Parallelism “almost for free”
29
Summary
30
METHOD PARAMETERIZATION
31
1. Filtering Green Apples
32
2. Parameterizing the Color
33
What if…
34
3. Filtering with every attribute one can think of
35
Here comes… Behavior Parameterization
36
4. Filtering by abstract criteria
37
38
39
40
Verbose!
41
5. Using an anonymous class
42
43
6. Using a Lambda expression
44
7. Abstracting over a List type
45