Java 8
Lambdas and much more
Alin Pandichi
@alinpandichi
About me
(Not only Java) software developer
Professional experience of 5 years
BJUG and AgileWorks community member
Twitter: @alinpandichi
We’ll talk about
Lambdas
Default methods
Streams
Method references
Java 8
Expected on 18 March 2014
Lambdas: one of the most anticipated features
Remember 'Plan B for Java 7'?
Play with it
https://github.com/AdoptOpenJDK/lambda-tutorial
https://jdk8.java.net/lambda/
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
() -> {}
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; };
public interface Adder {
public int adder(int x, int y);
}
Lambda
Adder adder = (x, y) -> { return x + y; };
@FunctionalInterface
public interface Adder {
public int adder(int x, int y);
}
Lambda
Adder adder = (x, y) -> { return x + y; };
Adder adder = new Adder() {
@Override
public int add(int x, int y) {
return x + y;
}
};
Iterable.forEach
public interface Iterable<T> {
Iterator<T> iterator();
}
Iterable.forEach
public interface Iterable<T> {
Iterator<T> iterator();
void forEach(Consumer<? super T> action);
}
Iterable.forEach
public interface Iterable<T> {
Iterator<T> iterator();
void forEach(Consumer<? super T> action);
}
Could break 99% of existing codebases!
Iterable.forEach
public interface Iterable<T> {
Iterator<T> iterator();
default void forEach(Consumer<? super T> action) {
}
}
Iterable.forEach
public interface Iterable<T> {
Iterator<T> iterator();
default void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}
}
Before Java 8...
Interfaces contain only
method declarations and no
implementations
With Java 8...
Interfaces contain only
method declarations and no
implementations
Default methods demo...
Default methods
Also called:
Defender methods
Virtual 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 inheritance
of type - been there, done that!
Default methods
Could be abused, for multiple inheritance!
Multiple inheritance
of type - been there, done that!
of behaviour - you are here!
Default methods
Could be abused, for multiple inheritance!
Multiple inheritance
of 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 {
@Override
public void doIt() { Foo.super.doIt(); }
}
Streams API demo...
Streams API
Nothing to do with I/O Streams
Streams API
Nothing to do with I/O Streams
A pipes-and-filters based API for collections
Streams API
Nothing to do with I/O Streams
A 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 Streams
A 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();
Method references
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 object
containingObject::instanceMethodName
Method references
to a static method
ContainingClass::staticMethodName
to an instance method of a particular object
containingObject::instanceMethodName
to a constructor
ClassName::new
See also...
● JSR 310: Date and Time API
No more java.util.Calendar!!!
● Nashorn, the new Javascript engine
var HelloUser = Java.type("com.credera.example.HelloUser");
var helloUser = new HelloUser("John");
print(helloUser.getMessage())

$ jjs -cp /com/credera/example/HelloUser TestNashorn.js
Recap
Lambdas
Default methods
Streams
Method references
Play with it
https://github.com/AdoptOpenJDK/lambda-tutorial
https://jdk8.java.net/lambda/
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/

Java 8 - Lambdas and much more