introduction-to-lambda-expressions-in-java-8-slides
introduction-to-lambda-expressions-in-java-8-slides
José Paumard
blog.paumard.org
@JosePaumard
What You Will Learn
Lambda expressions
Java FX
Nashorn
Course Overview
Generics
Collection API
Java I/O
Module Outline
Functional interfaces
Module Outline
Functional interfaces
Method references
Constructor references
Module Outline
Functional interfaces
Method references
Constructor references
A simple example
@Override
public boolean accept(File file) {
return file.getName().endsWith(".java");
}
};
@Override
public boolean accept(File file) {
return file.getName().endsWith(".java");
}
};
A First Lambda Expression
@Override
public boolean accept(File file) {
return file.getName().endsWith(".java");
}
};
We take the parameters
@Override
public boolean accept(File file) {
return file.getName().endsWith(".java");
}
};
and then…
@Override
public boolean accept(File file) {
return file.getName().endsWith(".java");
}
};
return this
@Override
public boolean accept(File file) {
return file.getName().endsWith(".java");
}
};
Runnable r = () -> {
for (int i = 0; i < 5; i++) {
System.out.println("Hello world!");
}
};
Several Ways of Writing a Lambda Expression
Example:
run();
};
Functional Interface
Example:
run();
};
Example:
run();
};
someMethod();
/**
* Some more documentation
*/
equals(Object o);
};
Functional Interface
@FunctionalInterface
public interface MyFunctionalInterface {
someMethod();
/**
* Some more documentation
*/
equals(Object o);
};
It is just here for convenience, the compiler can tell me whether the
interface is functional or not
Three Questions About Lambdas
Answer is yes!
Comparator<String> c =
(String s1, String s2) ->
Integer.compare(s1.length(), s2.length());
Can I Put a Lambda Expression in a Variable?
Answer is yes!
Comparator<String> c =
(String s1, String s2) ->
Integer.compare(s1.length(), s2.length());
Answer: yes!
Comparator<String> c =
new Comparator<String>(String s1, String s2) {
Comparator<String> c =
new Comparator<String>(String s1, String s2) {
4 categories:
Supplier
@FunctionalInterface
public interface Supplier<T> {
T get();
}
Package java.util.function
4 categories:
Consumer
@FunctionalInterface
public interface Consumer<T> {
4 categories:
Consumer / BiConsumer
@FunctionalInterface
public interface Consumer<T> {
@FunctionalInterface
public interface BiConsumer<T, U> {
4 categories:
Predicate
@FunctionalInterface
public interface Predicate<T> {
4 categories:
Predicate / BiPredicate
@FunctionalInterface
public interface Predicate<T> {
@FunctionalInterface
public interface BiPredicate<T, U> {
4 categories:
Function
@FunctionalInterface
public interface Function<T, R> {
R apply (T t);
}
Package java.util.function
4 categories:
Function / BiFunction
@FunctionalInterface
public interface Function<T, R> {
R apply (T t);
}
@FunctionalInterface
public interface BiFunction<T, U, R> {
R apply (T t, U u);
}
Package java.util.function
4 categories:
Function / UnaryOperator
@FunctionalInterface
public interface Function<T, R> {
R apply (T t);
}
@FunctionalInterface
public interface UnaryOperator<T> extends Function<T, T> {
}
Package java.util.function
4 categories:
BiFunction / BinaryOperator
@FunctionalInterface
public interface Function<T, U, R> {
R apply (T t, U u);
}
@FunctionalInterface
public interface BinaryOperator<T> extends BiFunction<T, T, T> {
}
More Lambda Expressions Syntax
Becomes:
Comparator<String> c =
(s1, s2) ->
Integer.compare(s1.length(), s2.length());
Method References
Or:
List<Customer> list = ...;
list.forEach(System.out::println);
Can I Process This Data with Lambdas?
We can write:
We can write:
We can write:
for (E e : this) {
consumer.accept(e);
}
}
}
Default Methods
Predicates
Predicates
Predicate<String> p3 = p1.and(p2);
Examples Of New Patterns
Predicates
Predicate<String> p3 = p1.and(p2);
@FunctionalInterface
public interface Predicate<T> {
Predicates
Predicate<String> id = Predicate.isEqual(target);
Examples Of New Patterns
Predicates
Predicate<String> id = Predicate.isEqual(target);
@FunctionalInterface
public interface Predicate<T> {
Iterable.forEach method