java 8 - project lambda

29
Revolution of Java Java 8 Project Lambda

Upload: rahman-usta

Post on 15-Apr-2017

7.824 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Java 8 - Project Lambda

Revolution of Java Java 8 – Project Lambda

Page 2: Java 8 - Project Lambda

About Me

• Java Developer

• Technical Teacher

• Blogger @kodcu.com

Page 3: Java 8 - Project Lambda

Java 8 Innovations

• Lambda Expressions

• Type Inference

• Metod References

• Collection II

• Defender Methods

Page 4: Java 8 - Project Lambda

Functional Interface ?

• An Interface includes only one abstract method called as FunctionalInterface

• Lambda expressions are converted to a FI’s implementation in Background

Page 5: Java 8 - Project Lambda

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

Page 6: Java 8 - Project Lambda

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)

Page 7: Java 8 - Project Lambda

Focus to Input & Output

Runnable r1=new Runnable() {

@Override

public void run() {

System.out.println(“Hello Uranus.");

}

};

Input

Output

Page 8: Java 8 - Project Lambda

Example

Page 9: Java 8 - Project Lambda

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; };

Page 10: Java 8 - Project Lambda

Type Inference

(String s) -> { System.out.println(s); };

// equals

(s) -> { System.out.println(s); };

// equals

s -> { System.out.println(s); };

Page 11: Java 8 - Project Lambda

Type Inference

(Integer a, Integer b) -> a + b;

// equals

(a,b) -> (a+b);

Page 12: Java 8 - Project Lambda

Metod References

Runnable r1=new Runnable() {

@Override

public void run() {

System.out.println(“Hello Uranus.");

}

};

Input

Output

Page 13: Java 8 - Project Lambda

Metod References

Page 14: Java 8 - Project Lambda

Collection II

• Collection II is an attachment to existing Collection Framework.

• Supports Jquery like chained methods and Underscore.js like Functional Programming Experience.

Page 15: Java 8 - Project Lambda

Collection II

• Existing Collection Framework does not support Concurrent operations in chunked datas.

• Cause, existing one applies External Iteration.

Page 16: Java 8 - Project Lambda

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.

Page 17: Java 8 - Project Lambda

java.util.function Functional Interfaces

• Those special Interfaces support general data operations.

Page 18: Java 8 - Project Lambda

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);

Page 19: Java 8 - Project Lambda

Predicate<T> -> Filtering

• Predicate gets a conditional code and produce -> true || false

• In concise, makes filtering.

public boolean test(T t);

Page 20: Java 8 - Project Lambda

Function<T, R> -> (Consume & Produce);

• Gets any type of input object and Produce an output object.

public R apply(T t);

Page 21: Java 8 - Project Lambda

Supplier<T>

• Do not get any argument object, only returns instantiation of any object type.

public T get();

Page 22: Java 8 - Project Lambda

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)

.

.

.

Page 23: Java 8 - Project Lambda

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.

Page 24: Java 8 - Project Lambda

Lazily & Eagerly Ops.

• Some Stream operations can be run Lazly as some Eagerly.

• .filter().map().allMatch();

• .filter().filter().sorted().forEach();

Lazy

Lazy

Eager

Eager

Page 25: Java 8 - Project Lambda

Lazily & Eagerly

• Lazy operations are evaluated together with first encountered Eager operation.

• The goal is evaluate chanied operations in one step.

Page 26: Java 8 - Project Lambda

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

Page 27: Java 8 - Project Lambda

Defender Methods

• Defender Methods, Default Methods, Virtual extension methods

• Makes possible usage of concrete methods.

• Makes more elastic of Java Inheritance model.

Page 28: Java 8 - Project Lambda

Defender Methods

• Defender methods can be re-implemented or inherited by other Interfaces.

Page 29: Java 8 - Project Lambda

Q/A

?

??

???

????

?????

????

???

??

?