4. Comparator<Person> comparator = new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
return o1.firstname.compareTo(o2.firstname);
}
};
9. Comparator<Person> comparator1 =
(o1, o2) -> throw new Exception("Something went wrong");
THE CONTENT OF THE LAMBDA MUST MATCH THE SIGNATURE
OF THE METHOD IMPLEMENTED
30. public interface Postfixable {
public final static int UNARY_MESSAGE_PRIORITY = 1;
public final static int BINARY_MESSAGE_PRIORITY = 2;
// ...
int getPriority();
public default boolean le(Postfixable other) {
return this.getPriority() <= other.getPriority();
}
}
31. public interface Formula {
double calculate(int a);
default double sqrt(int a) {
return Math.sqrt(positive(a));
}
}
Formula formula = (a) -> sqrt( a * 100);
32. public interface Formula {
double calculate(int a);
default double sqrt(int a) {
return Math.sqrt(positive(a));
}
}
Formula formula = (a) -> sqrt( a * 100);
DOES NOT COMPILE
34. public static class MyClass implements MyInterface1, MyInterface2 {
// ...
}
35. public static class MyClass implements MyInterface1, MyInterface2 {
// ...
} DOES NOT COMPILE
36. public static abstract class MyClass
implements MyInterface1, MyInterface2 {
public abstract foo();
}
UN-IMPLEMENTING THE DEFAULT METHOD SOLVES THE PROBLEM
37. public static class MyClass implements MyInterface1, MyInterface2 {
public void foo() {
MyInterface1.super.foo();
MyInterface2.super.foo();
}
}
38. public static class MyClass1 implements MyInterface1 {
public void foo(){
System.out.println("bonjour");
}
}
public static class MyClass2 extends MyClass1 implements MyInterface2 {
}
39. public static class MyClass1 implements MyInterface1 {
public void foo(){
System.out.println("bonjour");
}
}
public static class MyClass2 extends MyClass1 implements MyInterface2 {
}
IN CASE OF AMBIGUITY A CLASS TAKES PRECEDENCE
41. public class Car {
public static Car create(final Supplier<Car> supplier) {
return supplier.get();
}
public static void collide(final Car car) {
System.out.println("Collided " + car.toString());
}
public void repair() {
System.out.println("Repaired " + this.toString());
}
public void follow(final Car another) {
System.out.println("Following the " + another.toString());
}
}
42. final Car car = Car.create(Car::new);
final List<Car> cars = Arrays.asList(car);
43. final Car car = Car.create(Car::new);
final List<Car> cars = Arrays.asList(car);
cars.forEach(Car::collide);
44. final Car car = Car.create(Car::new);
final List<Car> cars = Arrays.asList(car);
cars.forEach(Car::repair);
45. final Car car = Car.create(Car::new);
final List<Car> cars = Arrays.asList(car);
final Car police = Car.create(Car::new);
cars.forEach(police::follow);
46. public static UtfToCodePoint findUtfToCodePoint(final Charset charset) {
switch (charset) {
case UTF8:
case UTF8BOM:
return Converter::utf8ToCodePoint;
case UTF16BE:
return Converter::utf16beToCodePoint;
case UTF16LE:
return Converter::utf16leToCodePoint;
case UTF32BE:
return Converter::utf32beToCodePoint;
case UTF32LE:
return Converter::utf32leToCodePoint;
default:
throw new UnicodeException("Unknown charset!");
}
}
47. public class Person {
final private String firstName;
final private String lastName;
public Person() {}
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}
public interface PersonFactory {
Person create(String firstName, String lastName);
}
60. public class Collections {
public static <T> void copy(List<? super T> dest, List<? extends T> src) {
for (int i = 0; i < src.size(); i++)
dest.set(i,src.get(i));
}
}
List<A> as = new ArrayList<>();
List<B> bs = new ArrayList<>();
List<C> cs = new ArrayList<>();
Collections.copy(List<A>, List<B>);
Collections.copy(List<A>, List<A>);
Collections.copy(List<A>, List<C>);
Collections.copy(List<B>, List<B>);
Collections.copy(List<B>, List<A>); // KO
Collections.copy(List<B>, List<C>);
Collections.copy(List<C>, List<B>); // KO
Collections.copy(List<C>, List<A>); // KO
Collections.copy(List<C>, List<C>);
public class A {
}
public class B extends A {
}
public class C extends B {
}
61. void forEach(Consumer<? super T> action)
List<Double> temperature = Arrays.asList(20.0, 22.0, 22.5);
temperature.forEach(System.out::println);
62. void sort(Comparator<? super E> c)
List<Double> temperature = Arrays.asList(20.0, 22.0, 22.5);
temperature.sort((a, b) -> a > b ? -1 : 1);
63. boolean removeIf(Predicate<? super E> filter)
List<Double> temperature = Arrays.asList(20.0, 22.0, 22.5);
temperature.removeIf(s -> s > 22);
65. void forEach(BiConsumer<? super K, ? super V> action)
Map<String , Integer> authorBooks = new HashMap<String , Integer>();
authorBooks.put("Robert Ludlum", 27);
authorBooks.put("Clive Cussler", 50);
authorBooks.put("Tom Clancy", 17);
authorBooks.forEach((a, b) -> System.out.println(a + " wrote " + b + " books"));
66. V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)
Map<String , Integer> authorBooks = new HashMap<String , Integer>();
authorBooks.put("Robert Ludlum", 27);
authorBooks.put("Clive Cussler", 50);
authorBooks.put("Tom Clancy", 17);
authorBooks.compute("Clive Cussler", (a, b) -> b + 1);
// If the compute function returns null then the entry for that key is removed
// from the map. If the key is not present then a new entry is added.
67. V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)
Map<String , Integer> authorBooks = new HashMap<String , Integer>();
authorBooks.put("Robert Ludlum", 27);
authorBooks.put("Clive Cussler", 50);
authorBooks.put("Tom Clancy", 17);
authorBooks.computeIfAbsent("Agatha Christie", b -> b.length());
// The entry is added only if the computed value is not null.
68. V computeIfPresent(K key,
BiFunction<? super K, ? super V, ? extends V> remappingFunction)
Map<String , Integer> authorBooks = new HashMap<String , Integer>();
authorBooks.put("Robert Ludlum", 27);
authorBooks.put("Clive Cussler", 50);
authorBooks.put("Tom Clancy", 17);
authorBooks.computeIfPresent("Tom Clancy", (a, b) -> b + 1);
// Note that this function also removes an element if the new value computed from
// the passed lambda expression is null.
69. V getOrDefault(Object key, V defaultValue)
Map<String , Integer> authorBooks = new HashMap<String , Integer>();
authorBooks.put("Robert Ludlum", 27);
authorBooks.put("Clive Cussler", 50);
authorBooks.put("Tom Clancy", 17);
authorBooks.getOrDefault("AuthorA", 0)
70. V merge(K key, V value,
BiFunction<? super V, ? super V, ? extends V> remappingFunction)
Map<String , Integer> authorBooks = new HashMap<String , Integer>();
authorBooks.put("Robert Ludlum", 27);
authorBooks.put("Clive Cussler", 50);
authorBooks.put("Tom Clancy", 17);
authorBooks.merge("AuthorB", 1, (a, b) -> a + b);
System.out.println(authorBooks.get("AuthorB")); // 1
authorBooks.merge("AuthorB", 1, (a, b) -> a + b);
System.out.println(authorBooks.get("AuthorB")); // 2
72. boolean remove(Object key, Object value)
V replace(K key, V newValue)
boolean replace(K key, V oldValue, V newValue)
void replaceAll (BiFunction<? super K, ? super V, ? extends V> function)
94. // The result won't be always the same
Set<Integer> seen = Collections.synchronizedSet(new HashSet<>());
stream.parallel().map(e -> {
if (seen.add(e)) return 0; else return e;
}
);
95. // with a stateless lambda expression the results would
always be the same.
IntStream.range(0,5).parallel().map(x -> x * 2).toArray()
// use reduction instead of mutable accumulators
96.
97. int sum = numbers.stream().reduce(0, Integer::sum);
98. int sumOfWeights = widgets.stream()
.reduce(0,
(sum, b) -> sum + b.getWeight())
Integer::sum);
113. <R,A> R collect(Collector<? super T,A,R> collector)
<R> R collect(Supplier<R> supplier,
BiConsumer<R,? super T> accumulator,
BiConsumer<R,R> combiner)
114.
115. public void closure() {
List<Supplier<Integer>> list = new ArrayList<>();
for (int i = 1; i <= 5; i++) {
list.add(() -> i);
}
}
116. public void closure() {
List<Supplier<Integer>> list = new ArrayList<>();
for (int i = 1; i <= 5; i++) {
list.add(() -> i);
}
}
DOES NOT COMPILE
The value of « i » changes along the way
117. public void closure() {
List<Supplier<Integer>> list = new ArrayList<>();
for (int i = 1; i <= 5; i++) {
final int j = i;
list.add(() -> j);
}
list.forEach((e) -> System.out.print(e.get())); // 12345
}
COMPILE
A new « j » variable is created at each iteration
118. public void closure() {
List<Supplier<Integer>> list = new ArrayList<>();
for (int i = 1; i <= 5; i++) {
final int j = i;
list.add(() -> j);
}
list.forEach((e) -> System.out.print(e.get())); // 12345
}
The « final » keyword is not mandatory as the value of « j » is set once and for all
In this example « j » IS EFFECTIVELY FINAL