Sei sulla pagina 1di 61

JDK8 Lambdas and Streams:

Changing The Way You Think


When Developing Java
Simon Ritter
Head of Java Technology Evangelism
Oracle Corp.

Twitter: @speakjava

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Safe Harbor Statement
The following is intended to outline our general product direction. It is intended for
information purposes only, and may not be incorporated into any contract. It is not a
commitment to deliver any material, code, or functionality, and should not be relied upon
in making purchasing decisions. The development, release, and timing of any features or
functionality described for Oracle’s products remains at the sole discretion of Oracle.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 2


Lambda & Streams
Primer

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Lambda Expressions In JDK8
Simplified Parameterised Behaviour

• Old style, anonymous inner classes


new Thread(new Runnable {
public void run() {
doSomeStuff();
}
}).start();

• New style, using a Lambda expression


new Thread(() -> doSomeStuff()).start();

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 4


Lambda Expressions
Some Details
• Lambda expressions represent anonymous functions
– Same structure as a method
• typed argument list, return type, set of thrown exceptions, and a body
– Not associated with a class
• We now have parameterised behaviour, not just values
double highestScore = students
.filter(Student s -> s.getGradYear() == 2011)
What .map(Student s -> s.getScore())
.max();

How
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
Lambda Expression Types
• Single-method interfaces are used extensively in Java
– Definition: a functional interface is an interface with one abstract method
– Functional interfaces are identified structurally
– The type of a lambda expression will be a functional interface
• Lambda expressions provide implementations of the abstract method

interface Comparator<T> { boolean compare(T x, T y); }


interface FileFilter { boolean accept(File x); }
interface Runnable { void run(); }
interface ActionListener { void actionPerformed(…); }
interface Callable<T> { T call(); }

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Local Variable Capture
• Lambda expressions can refer to effectively final local variables from the
surrounding scope
– Effectively final: A variable that meets the requirements for final variables (i.e.,
assigned once), even if not explicitly declared final
– Closures on values, not variables

void expire(File root, long before) {


root.listFiles(File p -> p.lastModified() <= before);
}

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


What Does ‘this’ Mean In A Lambda
• ‘this’ refers to the enclosing object, not the lambda itself
• Think of ‘this’ as a final predefined local
• Remember the Lambda is an anonymous function
– It is not associated with a class
– Therefore there can be no ‘this’ for the Lambda

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Referencing Instance Variables
Which are not final, or effectively final

class DataProcessor {
private int currentValue;

public void process() {


DataSet myData = myFactory.getDataSet();
dataSet.forEach(d -> d.use(currentValue++));
}
}

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Referencing Instance Variables
The compiler helps us out

class DataProcessor {
private int currentValue;

public void process() {


DataSet myData = myFactory.getDataSet();
dataSet.forEach(d -> d.use(this.currentValue++);
}
}

‘this’ (which is effectively final)


inserted by the compiler

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Type Inference
• The compiler can often infer parameter types in a lambda expression
 Inferrence based on the target functional interface’s method signature

static T void sort(List<T> l, Comparator<? super T> c);

List<String> list = getList();


Collections.sort(list, (String x, String y) -> x.length() - y.length());

Collections.sort(list, (x, y) -> x.length() - y.length());

• Fully statically typed (no dynamic typing sneaking in)


– More typing with less typing

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Method References
• Method references let us reuse a method as a lambda expression

FileFilter x = File f -> f.canRead();

FileFilter x = File::canRead;

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Method References
Rules For Construction

Lambda (args) -> ClassName.staticMethod(args)

Method Ref ClassName::staticMethod

Lambda (arg0, rest) -> arg0.instanceMethod(rest)


instanceOf
Method Ref ClassName::instanceMethod

Lambda (args) -> expr.instanceMethod(args)

Method Ref expr::instanceMethod


Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 13
Constructor References

• Same concept as a method reference


– For the constructor

Factory<List<String>> f = () -> return new ArrayList<String>();

Replace with

Factory<List<String>> f = ArrayList<String>::new;

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Useful Methods That Can Use Lambdas
• Iterable.forEach(Consumer c)

List<String> myList = ...


myList.forEach(s -> System.out.println(s));

myList.forEach(System.out::println);

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Useful Methods That Can Use Lambdas
• Collection.removeIf(Predicate p)

List<String> myList = ...


myList.removeIf(s -> s.length() == 0)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Useful Methods That Can Use Lambdas
• List.replaceAll(UnaryOperator o)

List<String> myList = ...


myList.replaceAll(s -> s.toUpperCase());

myList.replaceAll(String::toUpper);

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Useful Methods That Can Use Lambdas
• List.sort(Comparator c)
• Replaces Collections.sort(List l, Comparator c)

List<String> myList = ...


myList.sort((x, y) -> x.length() – y.length());

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Useful Methods That Can Use Lambdas
• Logger.finest(Supplier<String> msgSupplier)
• Overloads Logger.finest(String msg)

logger.finest(produceComplexMessage());
logger.finest(() -> produceComplexMessage());

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Functional Interface Definition
• An interface
• Must have only one abstract method
– In JDK 7 this would mean only one method (like ActionListener)
• JDK 8 introduced default methods
– Adding multiple inheritance of types to Java
– These are, by definition, not abstract (they have an implementation)
• JDK 8 also now allows interfaces to have static methods
– Again, not abstract
• @FunctionalInterface can be used to have the compiler check

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 20


Is This A Functional Interface?
@FunctionalInterface
public interface Runnable {
public abstract void run();
}

Yes. There is only


one abstract method

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 21


Is This A Functional Interface?
@FunctionalInterface
public interface Predicate<T> {
default Predicate<T> and(Predicate<? super T> p) {…};
default Predicate<T> negate() {…};
default Predicate<T> or(Predicate<? super T> p) {…};
static <T> Predicate<T> isEqual(Object target) {…};
boolean test(T t);
}
Yes. There is still only
one abstract method

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 22


Is This A Functional Interface?
@FunctionalInterface
public interface Comparator {
// default and static methods elided
int compare(T o1, T o2);
boolean equals(Object obj); Therefore only one
}
abstract method

The equals(Object)
method is implicit
from the Object class
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 23
Stream Overview
Pipeline
• A stream pipeline consists of three types of things
– A source
– Zero or more intermediate operations
– A terminal operation
• Producing a result or a side-effect
Source
int total = transactions.stream()
.filter(t -> t.getBuyer().getCity().equals(“London”))
.mapToInt(Transaction::getPrice)
.sum();
Intermediate operation
Terminal operation
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
Stream Sources
Many Ways To Create
• From collections and arrays
– Collection.stream()
– Collection.parallelStream()
– Arrays.stream(T array) or Stream.of()
• Static factories
– IntStream.range()
– Files.walk()
• Roll your own
– java.util.Spliterator

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Maps and FlatMaps
Map Values in a Stream

1-to-1 mapping
Input Stream Output Stream
Map

1-to-many mapping
Input Stream Output Stream
FlatMap

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


The Optional Class

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Optional<T>
Reducing NullPointerException Occurrences
String direction = gpsData.getPosition().getLatitude().getDirection();

String direction = “UNKNOWN”;

if (gpsData != null) {
Position p = gpsData.getPosition();

if (p != null) {
Latitude latitude = p.getLatitude();

if (latitude != null)
direction = latitude.getDirection();
}
}
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
Optional Class
Helping To Eliminate the NullPointerException
• Terminal operations like min(), max(), etc do not return a direct result
• Suppose the input Stream is empty?
• Optional<T>
– Container for an object reference (null, or real object)
– Think of it like a Stream of 0 or 1 elements
– use get(), ifPresent() and orElse() to access the stored reference
– Can use in more complex ways: filter(), map(), etc
– gpsMaybe.filter(r -> r.lastReading() < 2).ifPresent(GPSData::display);

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Optional ifPresent()
Do something when set

if (x != null) {
print(x);
}

opt.ifPresent(x -> print(x));


opt.ifPresent(this::print);

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Optional filter()
Reject certain values of the Optional

if (x != null && x.contains("a")) {


print(x);
}

opt.filter(x -> x.contains("a"))


.ifPresent(this::print);

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Optional map()
Transform value if present

if (x != null) {
String t = x.trim();
if (t.length() > 1)
print(t);
}

opt.map(String::trim)
.filter(t -> t.length() > 1)
.ifPresent(this::print);

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Optional flatMap()
Going deeper

public String findSimilar(String s)

Optional<String> tryFindSimilar(String s)

Optional<Optional<String>> bad = opt.map(this::tryFindSimilar);


Optional<String> similar = opt.flatMap(this::tryFindSimilar);

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Update Our GPS Code
class GPSData {
public Optional<Position> getPosition() { ... }
}

class Position {
public Optional<Latitude> getLatitude() { ... }
}

class Latitude {
public String getString() { ... }
}

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Update Our GPS Code
String direction = Optional.ofNullable(gpsData)
.flatMap(GPSData::getPosition)
.flatMap(Position::getLatitude)
.map(Latitude::getDirection)
.orElse(“None”);

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Advanced Lambdas And Streams

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Streams and Concurrency

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Serial And Parallel Streams
• Collection Stream sources
– stream()
– parallelStream()
• Stream can be made parallel or sequential at any point
– parallel()
– sequential()
• The last call wins
– Whole stream is either sequential or parallel

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 38


Parallel Streams
• Implemented underneath using the fork-join framework
• Will default to as many threads for the pool as the OS reports processors
– Which may not be what you want
System.setProperty(
"java.util.concurrent.ForkJoinPool.common.parallelism",
"32767");
• Remember, parallel streams always need more work to process
– But they might finish it more quickly

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 39


When To Use Parallel Streams
Sadly, no simple answer

• Data set size is important, as is the type of data structure


– ArrayList: GOOD
– HashSet, TreeSet: OK
– LinkedList: BAD
• Operations are also important
– Certain operations decompose to parallel tasks better than others
– filter() and map() are excellent
– sorted() and distinct() do not decompose well

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 40


When To Use Parallel Streams
Quantative Considerations

• N = size of the data set


• Q = Cost per element through the Stream pipeline
• N x Q = Total cost of pipeline operations
• The bigger N x Q is the better a parallel stream will perform
• It is easier to know N than Q, but Q can be estimated
• If in doubt, profile

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 41


Streams: Pitfalls For The Unwary

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Functional v. Imperative
• For functional programming you should not modify state
• Java supports closures over values, not closures over variables
• But state is really useful…

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 43


Counting Methods That Return Streams
Still Thinking Imperatively

Set<String> sourceKeySet = streamReturningMethodMap.keySet();

LongAdder sourceCount = new LongAdder();

sourceKeySet.stream()
.forEach(c ->
sourceCount.add(streamReturningMethodMap.get(c).size()));

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 44


Counting Methods That Return Streams
Functional Way

sourceKeySet.stream()
.mapToInt(c -> streamReturningMethodMap.get(c).size())
.sum();

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 45


Printing And Counting Functional Interfaces
Still Thinking Imperatively

LongAdder newMethodCount = new LongAdder();

functionalParameterMethodMap.get(c).stream()
.forEach(m -> {
output.println(m);

if (isNewMethod(c, m))
newMethodCount.increment();
});

return newMethodCount.intValue();

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 46


Printing And Counting Functional Interfaces
More Functional, But Not Pure Functional

int count = functionalParameterMethodMap.get(c).stream()


.mapToInt(m -> {
int newMethod = 0;
output.println(m);

if (isNewMethod(c, m))
newMethod = 1;

return newMethod There is still state being


}) modified in the Lambda
.sum();

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 47


Printing And Counting Functional Interfaces
Even More Functional, But Still Not Pure Functional

int count = functionalParameterMethodMap.get(nameOfClass)


.stream()
.peek(method -> output.println(method))
.mapToInt(m -> isNewMethod(nameOfClass, m) ? 1 : 0)
.sum();

Strictly speaking printing


is a side effect, which is
not purely functional
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 48
The Art Of Reduction
(Or The Need to Think Differently)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


A Simple Problem
• Find the length of the longest line in a file
• Hint: BufferedReader has a new method, lines(), that returns a Stream

BufferedReader reader = ...

reader.lines()
.mapToInt(String::length)
.max()
.getAsInt();

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 50


Another Simple Problem
• Find the length of the longest line in a file

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 51


Naïve Stream Solution

String longest = reader.lines().


sort((x, y) -> y.length() - x.length()).
findFirst().
get();

• That works, so job done, right?


• Not really. Big files will take a long time and a lot of resources
• Must be a better approach

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 52


External Iteration Solution
String longest = "";

while ((String s = reader.readLine()) != null)


if (s.length() > longest.length())
longest = s;

• Simple, but inherently serial


• Not thread safe due to mutable state

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 53


Recursive Approach: The Method
String findLongestString(String s, int index, List<String> l) {
if (index >= l.size())
return s;

if (index == l.size() - 1) {
if (s.length() > l.get(index).length())
return s;
return l.get(index);
}

String s2 = findLongestString(l.get(start), index + 1, l);

if (s.length() > s2.length())


return s;
return s2;
}

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 54


Recursive Approach: Solving The Problem
List<String> lines = new ArrayList<>();

while ((String s = reader.readLine()) != null)


lines.add(s);

String longest = findLongestString("", 0, lines);

• No explicit loop, no mutable state, we’re all good now, right?


• Unfortunately not - larger data sets will generate an OOM exception

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 55


A Better Stream Solution
• The Stream API uses the well known filter-map-reduce pattern
• For this problem we do not need to filter or map, just reduce
Optional<T> reduce(BinaryOperator<T> accumulator)
• The key is to find the right accumulator
– The accumulator takes a partial result and the next element, and returns a new
partial result
– In essence it does the same as our recursive solution
– Without all the stack frames
• BinaryOperator is a subclass of BiFunction, but all types are the same
• R apply(T t, U u) or T apply(T x, T y)
Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 56
A Better Stream Solution
• Use the recursive approach as an accululator for a reduction

String longestLine = reader.lines()


.reduce((x, y) -> {
if (x.length() > y.length())
return x;
return y;
})
.get();

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 57


A Better Stream Solution
• Use the recursive approach as an accululator for a reduction

String longestLine = reader.lines()


.reduce((x, y) -> {
if (x.length() > y.length())
return x;
return y; x in effect maintains state for
}) us, by always holding the
.get();
longest string found so far

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 58


The Simplest Stream Solution
• Use a specialised form of max()
• One that takes a Comparator as a parameter

reader.lines()
.max(comparingInt(String::length))
.get();

• comparingInt() is a static method on Comparator


– Comparator<T> comparingInt(ToIntFunction<? extends T> keyExtractor)

Copyright © 2014, Oracle and/or its affiliates. All rights reserved. 59


Conclusions

• Java needs lambda statements


– Significant improvements in existing libraries are required
• Require a mechanism for interface evolution
– Solution: virtual extension methods
• Bulk operations on Collections
– Much simpler with Lambdas
• Java SE 8 evolves the language, libraries, and VM together

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.


Simon Ritter
Oracle Corporartion

Twitter: @speakjava

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Potrebbero piacerti anche