java 8 - lambdas and much more

47
Lambdas and much more Java 8 Alin Pandichi @alinpandichi

Upload: alinpandichi

Post on 10-May-2015

565 views

Category:

Technology


2 download

DESCRIPTION

Java 8 - Lambdas, default methods, streams API, method references

TRANSCRIPT

Page 1: Java 8 - Lambdas and much more

Lambdas and much more

Java 8

Alin Pandichi@alinpandichi

Page 2: Java 8 - Lambdas and much more

(Not only Java) software developer

Professional experience of 5 years

BJUG and AgileWorks community member

Twitter: @alinpandichi

About me

Page 3: Java 8 - Lambdas and much more

Lambdas

Default methods

Streams

Method references

We’ll talk about

Page 4: Java 8 - Lambdas and much more

Expected on 18 March 2014

Lambdas: one of the most anticipated features

Remember 'Plan B for Java 7'?

Java 8

Page 5: Java 8 - Lambdas and much more

https://github.com/AdoptOpenJDK/lambda-tutorial

https://jdk8.java.net/lambda/

Play with it

IDE support:http://www.jetbrains.com/idea/download/http://wiki.eclipse.org/JDT/Eclipse_Java_8_Support_(BETA)http://bits.netbeans.org/dev/nightly/latest/

Page 6: Java 8 - Lambdas and much more

Lambda demo...

Page 7: Java 8 - Lambdas and much more

External iteration vs. Internal iteration

List<Shape> shapes = /* ... */;

for (Shape shape :shapes) {shape.setColor(newColor);

}

shapes.forEach(shape -> shape.setColor(newColor));

Page 8: Java 8 - Lambdas and much more

() -> {}

Lambda

Page 9: Java 8 - Lambdas and much more

() -> {}

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

Lambda

Page 10: Java 8 - Lambdas and much more

() -> {}

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

s -> System.out.println(s)

Lambda

Page 11: Java 8 - Lambdas and much more

(x, y) -> { return x + y; }

Lambda

Page 12: Java 8 - Lambdas and much more

(x, y) -> { return x + y; }

(x, y) -> x + y

Lambda

Page 13: Java 8 - Lambdas and much more

(x, y) -> { return x + y; }

Lambda

Page 14: Java 8 - Lambdas and much more

Adder adder = (x, y) -> { return x + y; };

Lambda

Page 15: Java 8 - Lambdas and much more

Adder adder = (x, y) -> { return x + y; };

Lambda

public interface Adder {public int adder(int x, int y);

}

Page 16: Java 8 - Lambdas and much more

Adder adder = (x, y) -> { return x + y; };

Lambda

public interface Adder {public int adder(int x, int y);

}

@FunctionalInterface

Page 17: Java 8 - Lambdas and much more

Adder adder = (x, y) -> { return x + y; };

Lambda

Adder adder = new Adder() {@Overridepublic int add(int x, int y) {

return x + y;}

};

Page 18: Java 8 - Lambdas and much more

Iterable.forEachpublic interface Iterable<T> {

Iterator<T> iterator();

}

Page 19: Java 8 - Lambdas and much more

Iterable.forEachpublic interface Iterable<T> {

Iterator<T> iterator();void forEach(Consumer<? super T> action);

}

Page 20: Java 8 - Lambdas and much more

Iterable.forEachpublic interface Iterable<T> {

Iterator<T> iterator();void forEach(Consumer<? super T> action);

}

Could break 99% of existing codebases!

Page 21: Java 8 - Lambdas and much more

Iterable.forEachpublic interface Iterable<T> {

Iterator<T> iterator();default void forEach(Consumer<? super T> action) {}

}

Page 22: Java 8 - Lambdas and much more

Iterable.forEachpublic interface Iterable<T> {

Iterator<T> iterator();default void forEach(Consumer<? super T> action) {

Objects.requireNonNull(action); for (T t : this) { action.accept(t); }}

}

Page 23: Java 8 - Lambdas and much more

Interfaces contain only method declarations and no implementations

Before Java 8...

Page 24: Java 8 - Lambdas and much more

Interfaces contain only method declarations and no implementations

With Java 8...

Page 25: Java 8 - Lambdas and much more

Default methods demo...

Page 26: Java 8 - Lambdas and much more

Also called:Defender methodsVirtual extension methods

Default methods

Page 27: Java 8 - Lambdas and much more

Helped extending/improving the existing API interfaces...

… without breaking the existing code...

… by providing default implementations

Default methods

Page 28: Java 8 - Lambdas and much more

Could be abused, for multiple inheritance!

Default methods

Page 29: Java 8 - Lambdas and much more

Could be abused, for multiple inheritance!

Multiple inheritanceof type - been there, done that!

Default methods

Page 30: Java 8 - Lambdas and much more

Could be abused, for multiple inheritance!

Multiple inheritanceof type - been there, done that!of behaviour - you are here!

Default methods

Page 31: Java 8 - Lambdas and much more

Could be abused, for multiple inheritance!

Multiple inheritanceof type - been there, done that!of behaviour - you are here!of state - not yet, hopefully NOT EVER!

Default methods

Page 32: Java 8 - Lambdas and much more

interface Foo {default void doIt() { /* foo impl */ }

}interface Bar {

default void doIt() { /* bar impl */ }}class FooBar implements Foo, Bar {}

Will not compile

Default methods

Page 33: Java 8 - Lambdas and much more

interface Foo {default void doIt() { /* foo impl */ }

}interface Bar {

default void doIt() { /* bar impl */ }}class FooBar implements Foo, Bar {

@Overridepublic void doIt() { Foo.super.doIt(); }

}

Default methods

Page 34: Java 8 - Lambdas and much more

Streams API demo...

Page 35: Java 8 - Lambdas and much more

Nothing to do with I/O Streams

Streams API

Page 36: Java 8 - Lambdas and much more

Nothing to do with I/O StreamsA pipes-and-filters based API for collections

Streams API

Page 37: Java 8 - Lambdas and much more

Nothing to do with I/O StreamsA pipes-and-filters based API for collections

This may be familiar...ps -ef | grep java | cut -c 1-9 | sort -n | uniq

Streams API

Page 38: Java 8 - Lambdas and much more

Nothing to do with I/O StreamsA pipes-and-filters based API for collections

This may be familiar...ps -ef | grep java | cut -c 1-9 | sort -n | uniq

In a similar manner...blocks.stream()

.filter(b -> b.getColor() == Color.RED)

.mapToInt(b -> b.getWeight()).sum();

Streams API

Page 39: Java 8 - Lambdas and much more

documents.map(d -> d.getTitle()).collect(toList());

Method references

Page 40: Java 8 - Lambdas and much more

documents.map(d -> d.getTitle()).collect(toList());

Does nothing but call an existing method...

Method references

Page 41: Java 8 - Lambdas and much more

documents.map(d -> d.getTitle()).collect(toList());

documents.map(Document::getTitle).collect(toList());

Method references

Page 42: Java 8 - Lambdas and much more

to a static method ContainingClass::staticMethodName

Method references

Page 43: Java 8 - Lambdas and much more

to a static method ContainingClass::staticMethodName

to an instance method of a particular objectcontainingObject::instanceMethodName

Method references

Page 44: Java 8 - Lambdas and much more

to a static method ContainingClass::staticMethodName

to an instance method of a particular objectcontainingObject::instanceMethodName

to a constructorClassName::new

Method references

Page 45: Java 8 - Lambdas and much more

● JSR 310: Date and Time APINo more java.util.Calendar!!!

● Nashorn, the new Javascript enginevar HelloUser = Java.type("com.credera.example.HelloUser");

var helloUser = new HelloUser("John");print(helloUser.getMessage())

$ jjs -cp /com/credera/example/HelloUser TestNashorn.js

See also...

Page 46: Java 8 - Lambdas and much more

Lambdas

Default methods

Streams

Method references

Recap

Page 47: Java 8 - Lambdas and much more

https://github.com/AdoptOpenJDK/lambda-tutorial

https://jdk8.java.net/lambda/

Play with it

IDE support:http://www.jetbrains.com/idea/download/http://wiki.eclipse.org/JDT/Eclipse_Java_8_Support_(BETA)http://bits.netbeans.org/dev/nightly/latest/