© Henri Tremblay 2015
Henri Tremblay
Senior Software Engineer
Terracotta, a Software AG company
Learn Java 8: Lambdas and Functional
Programming
@henri_tremblay
2
Henri Tremblay
3
Henri Tremblay
4
Henri Tremblay
• More or less made possible class mocking and proxying
• Coined the term “partial mocking”
5
Henri Tremblay
• More or less made possible class mocking and proxying
• Coined the term “partial mocking”
6
7
Java < 8
8
Live Coding
9
Questions
10
Java 5
(2004)
11
JAVA 5 GAVE THE GENERIC
TYPES TO THE WORLD
(and also annotations, concurrent collections, enum types, for
each, static imports and so on and so on)
12
Type witness
Having
<T> T anyObject()
you can force the type with
MyClass.<List<String>> anyObject()
because
(List<String>) MyClass.anyObject()
gives a warning
13
Java 6
(2006, last from Sun)
14
JAVA 6 BROUGHT... PRETTY
MUCH NOTHING
(a bunch of performance improvements under the hood, better xml
parsing and the first scripting api)
15
Java 7
(2011, first from Oracle)
16
JAVA 7 BROUGHT A LOT OF
SYNTACTIC SUGAR
(plus invokeDynamic, ForkJoin, better file IO)
17
Switch for strings
switch(s) {
case "hello":
return "world";
case "bonjour":
return "le monde";
}
18
Diamond operator
List<String> list = new ArrayList<>();
19
Binary integer literals and underscores
int i = 0b1110001111;
int i = 1_000_000;
20
Multiple catches
try {
// ... do stuff
}
catch (IOException | SerializationException e) {
log.error("My error", e);
}
Instead of
try {
// ... do stuff
} catch (IOException e) {
log.error("My error", e);
} catch (SerializationException e) {
log.error("My error", e);
}
21
Auto Closeable
Before
InputStream in = new FileInputStream("allo.txt");
try {
// … do stuff
} finally {
try { in.close(); } catch(IOException e) {}
}
After
try(InputStream in = new FileInputStream("allo.txt")) {
// … do stuff
}
22
File IO API
List<String> lines =
Files.readAllLines(
Paths.get("path", "to", "my", "file.txt"));
// also: file watcher, symbolic links, file locks,
// copy… Look in java.nio.file
23
Java 8
(2014)
24
JAVA 8: LAMBDA!
(and also a new date API, default methods, metaspace, Nashorn,
JavaFX and CompletableFuture)
26
Date / Time API
Core ideas:
Immutable
A time is a time, a date is a date. Not always both (like java.util.Date)
Not everyone uses the same calendar (the same Chronology)
LocalDate, LocalTime, LocalDateTime  Local. No time zone
OffsetDateTime, OffsetTime  Time with an offset from
Greenwich
ZonedDateTime  LocalDateTime with a time zone
Duration, Period  Time span
Instant  Timestamp
Formatting  Easy and thread-safe formatting
27
Date / Time API (example)
LocalDateTime now = LocalDateTime.now();
String thatSpecialDay = now
.withDayOfMonth(1)
.atZone(ZoneId.of("Europe/Paris"))
.plus(Duration.ofDays(5))
.format(DateTimeFormatter.ISO_ZONED_DATE_TIME);
System.out.println(thatSpecialDay);
Output
2016-09-06T17:45:22.488+01:00[Europe/Paris]
28
29
Lambda: 11th letter of the Greek alphabet
30
(also written as λ-calculus) is a formal system in
mathematical logic for expressing computation based
on function abstraction and application using variable
binding and substitution. It is a universal model of
computation that can be used to simulate any single-
taped Turing machine and was first introduced by
mathematician Alonzo Church in the 1930s as part of
an investigation into the foundations of mathematics.
Lambda calculus
31
This is a function:
This is a lambda:
Lambda calculus
32
c = sqrt(add(pow(a, 2), pow(b, 2)))
Functional programming
33
45
The End
46
Who has learned
something today?
?
47
Brian Goetz – State of the lambda
http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-
final.html
http://cr.openjdk.java.net/~briangoetz/lambda/lambda-
translation.html
Ninja Squad – Lambda Kata
https://github.com/Ninja-Squad/ninjackaton-lambda
Maurice Naftalin’s lambda facts and books
http://www.lambdafaq.org/
Mastering Lamdbas: Java Programming in a Multicore World
Links
48
Questions? http://montreal-jug.org
?
http://easymock.org
http://objenesis.org
http:/ehcache.org
Henri Tremblay
http://blog.tremblay.pro
@henri_tremblay

DevNexus 2018: Learn Java 8, lambdas and functional programming

  • 1.
    © Henri Tremblay2015 Henri Tremblay Senior Software Engineer Terracotta, a Software AG company Learn Java 8: Lambdas and Functional Programming @henri_tremblay
  • 2.
  • 3.
  • 4.
    4 Henri Tremblay • Moreor less made possible class mocking and proxying • Coined the term “partial mocking”
  • 5.
    5 Henri Tremblay • Moreor less made possible class mocking and proxying • Coined the term “partial mocking”
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
    11 JAVA 5 GAVETHE GENERIC TYPES TO THE WORLD (and also annotations, concurrent collections, enum types, for each, static imports and so on and so on)
  • 12.
    12 Type witness Having <T> TanyObject() you can force the type with MyClass.<List<String>> anyObject() because (List<String>) MyClass.anyObject() gives a warning
  • 13.
  • 14.
    14 JAVA 6 BROUGHT...PRETTY MUCH NOTHING (a bunch of performance improvements under the hood, better xml parsing and the first scripting api)
  • 15.
  • 16.
    16 JAVA 7 BROUGHTA LOT OF SYNTACTIC SUGAR (plus invokeDynamic, ForkJoin, better file IO)
  • 17.
    17 Switch for strings switch(s){ case "hello": return "world"; case "bonjour": return "le monde"; }
  • 18.
  • 19.
    19 Binary integer literalsand underscores int i = 0b1110001111; int i = 1_000_000;
  • 20.
    20 Multiple catches try { //... do stuff } catch (IOException | SerializationException e) { log.error("My error", e); } Instead of try { // ... do stuff } catch (IOException e) { log.error("My error", e); } catch (SerializationException e) { log.error("My error", e); }
  • 21.
    21 Auto Closeable Before InputStream in= new FileInputStream("allo.txt"); try { // … do stuff } finally { try { in.close(); } catch(IOException e) {} } After try(InputStream in = new FileInputStream("allo.txt")) { // … do stuff }
  • 22.
    22 File IO API List<String>lines = Files.readAllLines( Paths.get("path", "to", "my", "file.txt")); // also: file watcher, symbolic links, file locks, // copy… Look in java.nio.file
  • 23.
  • 24.
    24 JAVA 8: LAMBDA! (andalso a new date API, default methods, metaspace, Nashorn, JavaFX and CompletableFuture)
  • 25.
    26 Date / TimeAPI Core ideas: Immutable A time is a time, a date is a date. Not always both (like java.util.Date) Not everyone uses the same calendar (the same Chronology) LocalDate, LocalTime, LocalDateTime  Local. No time zone OffsetDateTime, OffsetTime  Time with an offset from Greenwich ZonedDateTime  LocalDateTime with a time zone Duration, Period  Time span Instant  Timestamp Formatting  Easy and thread-safe formatting
  • 26.
    27 Date / TimeAPI (example) LocalDateTime now = LocalDateTime.now(); String thatSpecialDay = now .withDayOfMonth(1) .atZone(ZoneId.of("Europe/Paris")) .plus(Duration.ofDays(5)) .format(DateTimeFormatter.ISO_ZONED_DATE_TIME); System.out.println(thatSpecialDay); Output 2016-09-06T17:45:22.488+01:00[Europe/Paris]
  • 27.
  • 28.
    29 Lambda: 11th letterof the Greek alphabet
  • 29.
    30 (also written asλ-calculus) is a formal system in mathematical logic for expressing computation based on function abstraction and application using variable binding and substitution. It is a universal model of computation that can be used to simulate any single- taped Turing machine and was first introduced by mathematician Alonzo Church in the 1930s as part of an investigation into the foundations of mathematics. Lambda calculus
  • 30.
    31 This is afunction: This is a lambda: Lambda calculus
  • 31.
    32 c = sqrt(add(pow(a,2), pow(b, 2))) Functional programming
  • 32.
  • 33.
  • 34.
  • 35.
    47 Brian Goetz –State of the lambda http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state- final.html http://cr.openjdk.java.net/~briangoetz/lambda/lambda- translation.html Ninja Squad – Lambda Kata https://github.com/Ninja-Squad/ninjackaton-lambda Maurice Naftalin’s lambda facts and books http://www.lambdafaq.org/ Mastering Lamdbas: Java Programming in a Multicore World Links
  • 36.