Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional...

50
@ JosePaumard # FreeLambdas Free your Lambdas Java SE 8

Transcript of Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional...

Page 1: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

Free your Lambdas

Java SE 8

Page 2: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

Agenda

Tutorial session: we will start at the very beginning!

…and explore how to build functional interfaces to design

new APIs

This is about lambdas and functional interfaces

So not much about Streams & Collectors

Page 3: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard

@JosePaumard

https://github.com/JosePaumard

https://www.slideshare.net/jpaumard

https://www.youtube.com/user/JPaumard

Page 4: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

start

Page 5: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them
Page 6: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

Questions?

#FreeLambdas

Page 7: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

A first example

What is this code doing?

Comparator<Person> cmp = new Comparator<Person>() {

@Overridepublic int compare(Person p1, Person p2) {

return p1.getLastName().compareTo(p2.getLastName());}

};

Page 8: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

A first example

What is this code doing?

Comparator<Person> cmp = new Comparator<Person>() {

@Overridepublic int compare(Person p1, Person p2) {

int cmp = p1.getLastName().compareTo(p2.getLastName());if (cmp == 0) {

return p1.getFirstName().compareTo(p2.getFirstName());} else {

return cmp;}

}};

Page 9: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

A first example

What is this code doing?Comparator<Person> cmp = new Comparator<Person>() {

@Overridepublic int compare(Person p1, Person p2) {

int cmp = p1.getLastName().compareTo(p2.getLastName());if (cmp == 0) {

cmp = p1.getLastName().compareTo(p2.getFirstName());if (cmp == 0) {

return p1.getAge() - p2.getAge();} else {

return cmp;}

} else {return cmp;

}}

};

Page 10: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

A first example

What is this code doing?Comparator<Person> cmp = new Comparator<Person>() {

@Overridepublic int compare(Person p1, Person p2) {

int cmp = p1.getLastName().compareTo(p2.getLastName());if (cmp == 0) {

cmp = p1.getFirstName().compareTo(p2.getFirstName());if (cmp == 0) {

return p1.getAge() - p2.getAge();} else {

return cmp;}

} else {return cmp;

}}

};

Page 11: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

A first example

What is this code doing?

Comparator<Person> cmp = Comparator.comparing(Person::getLastName).thenComparing(Person::getFirstName).thenComparing(Person::getAge);

Page 12: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

A closer look at the Comparator

Suppose we want to sort strings of characters

1) We create a comparator:

Comparator<String> comparator = new Comparator<String>() {public int compare(String s1, String s2) {

return Integer.compare(s1.length(), s2.length());}

};

Page 13: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

A closer look at the Comparator

Suppose we want to sort strings of characters

1) We create a comparator:

2) We pass it to the right method:

Comparator<String> comparator = new Comparator<String>() {public int compare(String s1, String s2) {

return Integer.compare(s1.length(), s2.length());}

};

Arrays.sort(strings, comparator); Collections.sort(list, comparator) ;

Page 14: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

A closer look at the Comparator

What did we do?

Page 15: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

A closer look at the Comparator

What did we do?

We passed a piece of code as a parameter to a method

And this method will use this code later

Page 16: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

A closer look at the Comparator

Why did we use an instance of an anoymous class?

Page 17: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

A closer look at the Comparator

Why did we use an instance of an anoymous class?

Because there is no other way in Java 7!

Page 18: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

Another way of writing it

Our comparator:Comparator<String> comparator = new Comparator<String>() {

public int compareTo(String s1, String s2) {

return Integer.compare(s1.length(), s2.length()) ;

}

}

Page 19: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

Another way of writing it

Our comparator:

Becomes:

Comparator<String> comparator = new Comparator<String>() {

public int compareTo(String s1, String s2) {

return Integer.compare(s1.length(), s2.length()) ;

}

}

Comparator<String> comparator =

(String s1, String s2) ->

Integer.compare(s1.length(), s2.length()) ;

Page 20: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

Back to the Comparator interface

The Comparator interface…

public interface Comparator<T> {

public int compare(T t1, T t2) ;}

Page 21: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

Back to the Comparator interface

The Comparator interface…

becomes a functional interface in Java 8

public interface Comparator<T> {

public int compare(T t1, T t2) ;}

Page 22: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

Back to the Comparator interface

The Comparator interface…

becomes a functional interface in Java 8

because it has a single abstract method

public interface Comparator<T> {

public int compare(T t1, T t2) ;}

Page 23: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

Back to the Comparator interface

A functional interface can be annotated

But it is optional

@FunctionalInterfacepublic interface Comparator<T> {

public int compare(T t1, T t2) ;}

Page 24: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them
Page 25: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

So…

Writing a lambda is a matter of finding what interface to

implement

The type of a lambda is always known at compile time

The method to write a lambda is always the same:

- copy / paste the block of parameters

- little ASCII art arrow

- implement the method

Page 26: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

A weird thing…

We wrote this code:

The forEach() method is defined on the Iterable interface

List<String> strings = ...;

strings.forEach(

System.out::println // method reference (bound instance)

) ;

Page 27: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

A weird thing…

We wrote this code:

The forEach() method is defined on the Iterable interface

Do we really want to refactor all the Collection API?

List<String> strings = ...;

strings.forEach(

System.out::println // method reference (bound instance)

) ;

Page 28: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

Default methods

Breaking the backward compatibility is not possible in Java

The way interfaces work has been modified:

public interface Iterable<E> {

// usual methods

default void forEach(Consumer<? super T> action) {

Objects.requireNonNull(action);

for (T t : this) {

action.accept(t);

}

}

}

Page 29: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

Default methods

We can now add methods in interfaces with their

implementation

Those are « normal » methods

It is a new concept of interface, not a new concept of

method

Page 30: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

Default & static methods

We can now add methods in interfaces with their

implementation

Those are « normal » methods

It is a new concept of interface, not a new concept of

method

And static methods are allowed too!

Page 31: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

Back to the functional interface

A functional interface is an interface

with only one abstract method

So default & static methods do not count

Page 32: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them
Page 33: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

So…

Interfaces in Java 8:

- Functional interfaces to write lambda expressions

- Default methods, can be used to compose them

- Static methods, can be used as factory

We have better readability, better robustness…

and better performances!

(Youtube: Lambda a peek under the hood by B. Goetz)

Page 34: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

Package java.util.function

4 categories of functional interfaces:

ConsumerSupplierFunctionPredicate

Page 35: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

Package java.util.function

4 categories of functional interfaces:

Consumer t -> {};Supplier () -> t;Function t -> u;Predicate t -> true;

Page 36: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

Package java.util.function

4 categories of functional interfaces:

Consumer t -> {}; s -> System.out.println(s);Supplier () -> t; () -> new Balloon();Function t -> u; person -> person.getAge();Predicate t -> true; age -> age > 20;

Page 37: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

Package java.util.function

4 categories of functional interfaces:

Runnable () -> {};Consumer t -> {}; s -> System.out.println(s);Supplier () -> t; () -> new Balloon();Function t -> u; person -> person.getAge();Predicate t -> true; age -> age > 20;

Page 38: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

Package java.util.function

4 categories of functional interfaces:

Runnable () -> {}; () -> logger.log(message);Consumer t -> {}; s -> System.out.println(s);Supplier () -> t; () -> new Ballon();Function t -> u; person -> person.getAge();Predicate t -> true; age -> age > 20;

Page 39: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

Back to the Comparator

Let us write a comparator of people

- Using an anonymous class

- Then a lambda

Page 40: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them
Page 41: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

Method reference

There are 4 types of method references

Nom Syntaxe

Static RefType::staticMethod

Bound instance expr::instMethod

Unbound instance RefType::instMethod

Constructor ClassName::new

Page 42: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

Method reference

There are 4 types of method references

Lambda Method Reference

d -> Math.sin(d) Math::sin

s -> System.out.println(s) System.out::println

(s, t) -> s.compareTo(t) String::compareTo

name -> new Person(name) Person::new

Page 43: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

What about patterns?

Let us talk about the GoF

Page 44: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

What about patterns?

Let us talk about the GoF

The factory pattern

Page 45: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them
Page 46: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

What about patterns?

Let us talk about the GoF

The factory pattern

The registry & builder patterns

Page 47: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them
Page 48: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

Conclusion

Lambdas are not just a nice way of writing instances of

anonymous classes

Functional interfaces + default methods + factory methods

provide new ways of implementing well-known patterns for

our applications & APIs

Page 49: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas

Thank you!

Page 50: Free your Lambdas Java SE 8 - RainFocus · 2019-06-27 · Interfaces in Java 8: - Functional interfaces to write lambda expressions - Default methods, can be used to compose them

@JosePaumard#FreeLambdas