java 8 - project lambda
TRANSCRIPT
Revolution of Java Java 8 – Project Lambda
About Me
• Java Developer
• Technical Teacher
• Blogger @kodcu.com
Java 8 Innovations
• Lambda Expressions
• Type Inference
• Metod References
• Collection II
• Defender Methods
Functional Interface ?
• An Interface includes only one abstract method called as FunctionalInterface
• Lambda expressions are converted to a FI’s implementation in Background
Functional Interface Examps.
• java.lang.Runnable
• java.util.concurrent.Callable
• java.nio.file.PathMatcher
• java.lang.reflect.InvocationHandler
• java.beans.PropertyChangeListener
• java.awt.event.ActionListener
Lambda Expressions
• Lambda -> (also called Closures), is an expression that reduces vertical boilerplate code.
• Concise and Clear code development practice
• Interfaces that consist of one more than abstract method, cannot be counterpart of any Lambda expressions. Tip: Exceptions (Defender methods & Object class’ methods)
Focus to Input & Output
Runnable r1=new Runnable() {
@Override
public void run() {
System.out.println(“Hello Uranus.");
}
};
Input
Output
Example
Lambda Types
• 2 type
– Single expression // no need “return”
• (Integer s) -> ( s*2 );
• |or| (Integer s) -> s*2 ;
– Statement Block
• (Integer s) -> { return s*2; };
Type Inference
(String s) -> { System.out.println(s); };
// equals
(s) -> { System.out.println(s); };
// equals
s -> { System.out.println(s); };
Type Inference
(Integer a, Integer b) -> a + b;
// equals
(a,b) -> (a+b);
Metod References
Runnable r1=new Runnable() {
@Override
public void run() {
System.out.println(“Hello Uranus.");
}
};
Input
Output
Metod References
Collection II
• Collection II is an attachment to existing Collection Framework.
• Supports Jquery like chained methods and Underscore.js like Functional Programming Experience.
Collection II
• Existing Collection Framework does not support Concurrent operations in chunked datas.
• Cause, existing one applies External Iteration.
Stream <T> Interface
• Sequential and Consumable data structure.
• The data cannot be fetch again after consumed.
• A Stream object can be obtained any data structure in Collection Framework.
• Fo.Exm: LinkedList, ArrayList, HashSet.
java.util.function Functional Interfaces
• Those special Interfaces support general data operations.
Consumer<T> -> Applying
• Consumer gets an input and runs an action.
• Does not return any result, only do an action.
public void accept(T t);
Predicate<T> -> Filtering
• Predicate gets a conditional code and produce -> true || false
• In concise, makes filtering.
public boolean test(T t);
Function<T, R> -> (Consume & Produce);
• Gets any type of input object and Produce an output object.
public R apply(T t);
Supplier<T>
• Do not get any argument object, only returns instantiation of any object type.
public T get();
Bi?
BiPredicate<L, R> Compares two inputted object.
BiConsumer<T, U> Consumes two type of objects, and produce no result
BiFunction<T, U, R> Gets two input object (T , U) and produce a result (R)
.
.
.
Parallel Stream
• Any parallelizable operations can run concurrently with Parallel stream object.
• Parallel Stream object can be obtain with parallelStream() method from any Streamable data structure.
Lazily & Eagerly Ops.
• Some Stream operations can be run Lazly as some Eagerly.
• .filter().map().allMatch();
• .filter().filter().sorted().forEach();
Lazy
Lazy
Eager
Eager
Lazily & Eagerly
• Lazy operations are evaluated together with first encountered Eager operation.
• The goal is evaluate chanied operations in one step.
Defender Methods
• Before Java 8, Interfaces can contain only abstract methods.
• With Java 8, any Interface can contain one or more concrete methods that named default/defender method.
• Tip: Default methods do not break nature of FonctionalInterfaces
Defender Methods
• Defender Methods, Default Methods, Virtual extension methods
• Makes possible usage of concrete methods.
• Makes more elastic of Java Inheritance model.
Defender Methods
• Defender methods can be re-implemented or inherited by other Interfaces.
Q/A
?
??
???
????
?????
????
???
??
?