These are the slides of the talk we made with Stuart Marks at Devoxx Belgium 2018. This first part covers Lambda Expressions and API design with functional interfaces.
What is the state of lambda expressions in Java 11? Lambda expressions are the major feature of Java 8, having an impact on most of the API, including the Streams and Collections API. We are now living the Java 11 days; new features have been added and new patterns have emerged. This highly technical Deep Dive session will visit all these patterns, the well-known ones and the new ones, in an interactive hybrid of lecture and laboratory. We present a technique and show how it helps solve a problem. We then present another problem, and give you some time to solve it yourself. Finally, we present a solution, and open for questions, comments, and discussion. Bring your laptop set up with JDK 11 and your favorite IDE, and be prepared to think!
5. #LambdaHOL#Devoxx
The LambdaHOL
8 test classes, with 80 exercises to complete
- Lambda, functionnal interfaces
- Comparators
- Streams, Collectors
- Some challenges at the end
- Updated regularly
With solutions!
17. #LambdaHOL#Devoxx
1st example
A consumer that clears a string builder
@Test
public void consumer_1() {
Consumer<List<String>> consumer = null; // TODO
List<String> list =
new ArrayList<>(Arrays.asList("a", "b", "c"));
consumer.accept(list);
assertThat(list).isEmpty();
}
18. #LambdaHOL#Devoxx
2nd example
A consumer that calls two Consumers
@Test
public void consumer_2() {
Consumer<List<String>> c1 = list -> list.add("first");
Consumer<List<String>> c2 = list -> list.add("second");
Consumer<List<String>> consumer; // TODO
List<String> list =
new ArrayList<>(Arrays.asList("a", "b", "c"));
consumer.accept(list);
assertThat(list).containsExactly("a", "b", "c", "first", "second");
}
20. #LambdaHOL#Devoxx
1st example
A predicate that negates another predicate
@Test
public void predicate_1() {
Predicate<String> predicate = s -> s.isEmpty();
Predicate<String> notPredicate; // TODO
assertThat(notPredicate.test("")).isFalse();
assertThat(notPredicate.test("Not empty!")).isTrue();
}
21. #LambdaHOL#Devoxx
2nd example
A predicate that is true if a string is non null
and non empty
@Test
public void predicate_2() {
Predicate<String> p1 = s -> s != null;
Predicate<String> p2 = s -> s.isEmpty();
Predicate<String> p3; // TODO
assertThat(p3.test("")).isFalse();
assertThat(p3.test(null)).isFalse();
assertThat(p3.test("Not empty!")).isTrue();
}
22. #LambdaHOL#Devoxx
3rd example
How to design a xOr() method on Predicate?
@Test
public void predicate_3() {
Predicate<String> p1 = s -> s.length() == 4;
Predicate<String> p2 = s -> s.startsWith("J");
Predicate<String> p3; // TODO
assertThat(p3.test("True")).isTrue();
assertThat(p3.test("Julia")).isTrue();
assertThat(p3.test("Java")).isFalse();
}
24. #LambdaHOL#Devoxx
A first example
What is this code doing?
Comparator<Person> cmp = new Comparator<Person>() {
@Override
public int compare(Person p1, Person p2) {
return p1.getLastName().compareTo(p2.getLastName());
}
};
25. #LambdaHOL#Devoxx
A first example
What is this code doing?
Comparator<Person> cmp = new Comparator<Person>() {
@Override
public 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;
}
}
};
26. #LambdaHOL#Devoxx
A first example
What is this code doing?Comparator<Person> cmp = new Comparator<Person>() {
@Override
public int compare(Person p1, Person p2) {
int cmp = p1.getLastName().compareTo(p2.getLastName());
if (cmp == 0) {
cmp = p1.getFirstName().compareTo(p2.getLastName());
if (cmp == 0) {
return Integer.compare(p1.getAge(), p2.getAge());
} else {
return cmp;
}
} else {
return cmp;
}
}
};
27. #LambdaHOL#Devoxx
A first example
What is this code doing?Comparator<Person> cmp = new Comparator<Person>() {
@Override
public 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 Integer.compare(p1.getAge(), p2.getAge());
} else {
return cmp;
}
} else {
return cmp;
}
}
};
28. #LambdaHOL#Devoxx
A first example
What is this code doing?
Comparator<Person> cmp = Comparator.comparing(Person::getLastName)
.thenComparing(Person::getFirstName)
.thenComparing(Person::getAge);
29. #LambdaHOL#Devoxx
1st example
A comparator that puts null values at the end
Person michael = new Person("Michael", "Jackson", 51);
Person rod = new Person("Rod", "Stewart", 71);
Person paul = new Person("Paul", "McCartney", 74);
Person mick = new Person("Mick", "Jagger", 73);
Person jermaine = new Person("Jermaine", "Jackson", 61);
@Test
public void comparator_1() {
Comparator<Person> cmp = null; // TODO
assertThat(cmp.compare(michael, rod)).isLessThan(0);
assertThat(cmp.compare(paul, paul)).isEqualTo(0);
assertThat(cmp.compare(michael, jermaine)).isGreaterThan(0);
assertThat(cmp.compare(mick, null)).isLessThan(0);
assertThat(cmp.compare(null, mick)).isGreaterThan(0);
}
32. #LambdaHOL#Devoxx
A Currency Converter
… write the following currency converter:
LocalDate date = ...;
CurrencyConverter plnToAud =
CurrencyConverter.of(date)
.from("PLN")
.to("AUD");
float pln = ...;
float aud = plnToAud.convert(pln);
33. #LambdaHOL#Devoxx
A Validator
Given the following bean
public class Person {
private String lastName ;
private int age ;
// getters, setters, constructeurs
}
34. #LambdaHOL#Devoxx
A Validator
Implement the following pattern
If the user is not valid: get() throws an exception, with all
the exceptions of each validator in the suppressed
exceptions
Validator<Person> validator = Validator.
.firstValidate(p -> p.getName() == null, "name is null")
.thenValidate(p -> p.getAge() < 0, "age is negative")
.thenValidate(p -> p.getAge() > 150, "age is greater than
150");
person = validator.validate(person).get();
35. #LambdaHOL#Devoxx
A Validator
If the user is not valid: get() throws an exception, with all
the exceptions of each validator in the suppressed
exceptions
Validator<Person> validator =
Validator
.firstValidate(p -> p.getName() == null, "name is null")
.thenValidate(p -> p.getAge() < 0, "age is negative")
.thenValidate(p -> p.getAge() > 150, "age is greater than 150");
person = validator.validate(person).get();
36. #Devoxx
Don’t miss the 2nd day:
1) More functional
programming!
2) Stream, reduction
3) Collectors
Day break!