java 8 - lambdas and much more

Post on 10-May-2015

565 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

Lambdas and much more

Java 8

Alin Pandichi@alinpandichi

(Not only Java) software developer

Professional experience of 5 years

BJUG and AgileWorks community member

Twitter: @alinpandichi

About me

Lambdas

Default methods

Streams

Method references

We’ll talk about

Expected on 18 March 2014

Lambdas: one of the most anticipated features

Remember 'Plan B for Java 7'?

Java 8

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/

Lambda demo...

External iteration vs. Internal iteration

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

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

}

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

() -> {}

Lambda

() -> {}

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

Lambda

() -> {}

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

s -> System.out.println(s)

Lambda

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

Lambda

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

(x, y) -> x + y

Lambda

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

Lambda

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

Lambda

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

Lambda

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

}

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

Lambda

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

}

@FunctionalInterface

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

Lambda

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

return x + y;}

};

Iterable.forEachpublic interface Iterable<T> {

Iterator<T> iterator();

}

Iterable.forEachpublic interface Iterable<T> {

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

}

Iterable.forEachpublic interface Iterable<T> {

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

}

Could break 99% of existing codebases!

Iterable.forEachpublic interface Iterable<T> {

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

}

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

}

Interfaces contain only method declarations and no implementations

Before Java 8...

Interfaces contain only method declarations and no implementations

With Java 8...

Default methods demo...

Also called:Defender methodsVirtual extension methods

Default methods

Helped extending/improving the existing API interfaces...

… without breaking the existing code...

… by providing default implementations

Default methods

Could be abused, for multiple inheritance!

Default methods

Could be abused, for multiple inheritance!

Multiple inheritanceof type - been there, done that!

Default methods

Could be abused, for multiple inheritance!

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

Default methods

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

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

}interface Bar {

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

Will not compile

Default methods

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

Streams API demo...

Nothing to do with I/O Streams

Streams API

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

Streams API

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

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

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

Method references

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

Does nothing but call an existing method...

Method references

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

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

Method references

to a static method ContainingClass::staticMethodName

Method references

to a static method ContainingClass::staticMethodName

to an instance method of a particular objectcontainingObject::instanceMethodName

Method references

to a static method ContainingClass::staticMethodName

to an instance method of a particular objectcontainingObject::instanceMethodName

to a constructorClassName::new

Method references

● 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...

Lambdas

Default methods

Streams

Method references

Recap

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/

top related