Java8 Slides
Java8 Slides
Shekhar Gulati
Xebia
@shekhargulati
1
Couple of requests..
Ask questions :)
2
Agenda
Interface static and default methods
Functional paradigm
Lambda expressions
Stream API
Collectors
Optional
3
Prerequisite
Oracle Java 8 installed on your machine
http://www.oracle.com/technetwork/java/javase/
downloads/index-jsp-138363.html
https://www.jetbrains.com/idea/download/
http://www.eclipse.org/downloads/packages/eclipse-ide-
java-developers/marsr
Git
4
Why languages
evolve?
5
Why languages evolve?
To meet developers expectations
To remain relevant
Security fixes
6
Why you should learn Java 8?
To embrace functional programming paradigm
Lambdas
Stream API
Concurrency utilities
Optional
7
Section 01
Interface API Design
8
Three main components of API Design
Interfaces
Abstract Classes
Classes
9
Let’s write a simple Calculator
10
Static methods
11
Static method example
12
Interfaces can now have
static and default(instance)
methods
13
Why we need this?
14
Demo >>
Evolving API’s
15
Default Methods
16
Example of default method
17
Multiple inheritance
18
Interface Class Hierarchy
19
Multiple Interface Hierarchy
20
Diamond problem
21
Section 02
Functional Paradigm
22
Programming paradigm
23
Imperative vs Functional Paradigm
Examples on
24
next page
Canonical example — Factorial
Scala >>
Functional Programming
26
Functional programming
27
What is a function?
28
Why should we embrace functional
programming?
No side effects
29
Moore’s law ran out!!
30
Functional Languages
Haskell
ML
Erlang
F# - (Hybrid)
Scala - (Hybrid)
Clojure
Java 8 - (Hybrid)
31
Key Functional Programming Concepts
Function
A side effect free computation that always result in same value when passed
with same argument.
Referential transparency
Recursion
Lazy evaluation
32
Introduce Example Domain
Task
TaskType
33
SECTION 03
LAMBDAS
34
Lambda expression
A new feature introduced in Java 8
35
Demo >> lambda
Example 1
36
Lambda expression
37
Lambda syntax
- (a,b) -> a + b
- a -> a*a
- () -> a
38
Type Inference
The act of inferring a type from context is called
Type Inference.
41
Invalid @FunctionalInterface
42
Look at few @FunctionalInterface
Most of the time you don’t have to write your
own @FunctionalInterface
43
Test your lambda
knowledge
>> Exercise1_Lambda.java
44
Where can I use lambda expression?
45
Using local variables >> Effective final
46
Method References
47
Method reference
48
Demo >>
Method Reference
49
Exercise >> Let’s apply what we have learnt so far
50
SECTION 04
STREAM PROCESSING
51
Stream Processing
Stream vs Collection
52
Why?
53
Data processing before Java 8
54
Data processing in Java 8
55
Understanding the code
56
Why Java 8 code is better?
57
Stream
58
Collection vs Stream
Collection Stream
59
Stream API
60
Stream operations
collect, foreach,
61
filtering
62
Filtering operations
Find all the reading tasks sorted by their
creation date
63
map operation
Example3_MapStreams
64
flatmap operation
65
reduce operation
66
Duplicate Elements in a List
Output >>
Duplicate Elements n times in a List
Output >>
Working with numeric streams
IntStream
LongStream
DoubleStream
69
Parallel Stream
70
SECTION 05
COLLECTORS
71
Collector
72
Demo >>
Power of Collector
73
What you can do with collect?
74
Collectors class
grouping
partitioning
75
Reducing to a single value
numeric type
domain object
a collection
77
Partitioning
Map<Boolean, List<Task>>
79
How many of you
have experienced
NullPointerException?
80
Null History
It was designed by Sir Tony Hoare in 1965
creator of Quicksort
82
NullPointerException
83
We end up writing …
84
Biggest problem with code
85
Possible solutions
86
Optional
A container that may or may not contain a value
87
Demo >>
Optional TaskRepository
88
Another Example
89
SECTION 07
DATE AND TIME API
90
What does the below program prints?
91
Existing Date API Problems
Sun Jan 12 00:00:00 IST 1913
92
Existing Date API
Date API was introduced in JDK in 1996
Mutability
No concept of timezone
Boilerplate friendly
93
Calendar API
Still mutable
94
New API — Getting Started
95
New types for humans
97
New Type >> Instant
Nanosecond precision
98
Demo >> Instant
99
Duration and Period
Duration represents quantity or amount of
time in seconds or nano-seconds like 10
seconds
Duration d = Duration.between(dt1,dt2)
Period p = Period.between(ld1,ld2)
100
Demo >>
101
Temporal Adjuster
When you need to do advance date-time
manipulation then you will use it
Examples
102
Demo >>
103
Thanks
https://github.com/shekhargulati/java8-the-
missing-tutorial
https://twitter.com/shekhargulati
104