Java Functional Programming
Online Training References
Working with streams, lambda expressions, and method references in Java SE8 and beyond
- Course Description
- Course Documents
- Book : Modern Java Recipes
- Blog : https://kousenit.org/
- Video : Java 8 and 9 Fundamentals: Modern Java Development with Lambdas, Streams, and Introducing Java 9’s JShell and the Java Platform Module System (JPMS)
- Video : Advanced Java Development
- Java Doc 11
Lambda Expression
- Assigned to Single Abstract Method (SAM) interface (functional interface) (ex: Runnable, file)
- Parameter types inferred from context
- Method Reference
Functional Interface
- see java.util.function package
- Functional Interfaces (Not Required but useful)
- Use Pure Function
- Consumer -> single arg, no result (not a Pure function)
void accept(T t)
Ex : print or log
Also IntConsumer, LongConsumer, BiConsumer<T, U>
Ex :1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17import java.util.function.Consumer;
import java.util.logging.Logger;
import java.util.stream.Stream;
public class ConsumerCompositionDemo {
public static void main(String[] args) {
Consumer<String> cPrinter = System.out::println;
Logger logger = Logger.getLogger(ConsumerCompositionDemo.class.getName());
Consumer<String> cLogger = logger::info;
Consumer<String> composed = cPrinter.andThen(cLogger);
Stream.of("This", "is", "a", "string").forEach(composed);
}
}
Predicate -> returns boolean
boolean test(T t)
Supplier -> no arg, returns single result
T get()
Also BooleanSupplier…
Function -> single arg, returns result
R apply(T t)
Also IntFunction, LongFunction, ToDoubleFunction, ToDoubleBiFunction<T,U>, ToDoubleFunction
ToLongFunction
Method Reference
Method references uses :: notation
System.out::println
equivalent to x -> System.out.println(x)
Math::max
equivalent to (x, y) -> Math.max(x, y)
String::compareToIgnoreCase
equivalent to (x, y) -> x.compareToIgnoreCase(y)
Constructor Reference
Constructor references uses :: notation
Ex:
1 | import java.util.Arrays; |
Interface
Default methods
Use keyword default
If conflict :
- Class vs Interface => Class always wins
- Interface vs Interface => Child overrides parent, otherwise compilation error
If class implements multiple Interface with same method
- Must call InterfaceName.super.method
1 | public class Pegasus implements Horse, Bird { |
Streams
Definition
A sequence of elements
- Does not store the elements
- Does not change the source
- Operations are lazy when possible
- Closed when terminal expression reached
A stream carries values
- from a source
- through a pipeline
Pipeline
A pipeline is
- a source
- zero or more intermediate operations
- a terminal operation
Reduction Operations
Terminal operation that produce one value for a stream
Ex: average, sum, max, min, count, ....