java se 8: lambda expressions and the stream api€¢ single-method interfaces are used extensively...

35

Upload: dangphuc

Post on 30-May-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract
Page 2: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract
Page 3: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

Java SE 8: Lambda ExpressionsAnd The Stream API

Simon RitterHead of Java Technology EvangelismJava Product Management

Java Day Tokyo 2015April 8, 2015

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

Page 4: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

Copyright © 2015, 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.

4

Page 5: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

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

Lambdas In Java

Page 6: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

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

Lambda Expressions In JDK8

• Old style, anonymous inner classes

• New style, using a Lambda expression

6

Simplified Parameterised Behaviour

new Thread(new Runnable {public void run() {

doSomeStuff();}

}).start();

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

Page 7: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

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

Lambda Expressions

• 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

Some Details

double highestScore = students.

filter(Student s -> s.getGradYear() == 2011).

map(Student s -> s.getScore())

max();

What

How

Page 8: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

Copyright © 2015, 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(); }

Page 9: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

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

Local Variable Capture

• Lambda expressions can refer to effectively final local variables from the enclosing 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);

}

Page 10: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

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

What Does ‘this’ Mean For Lambdas?

• ‘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

Page 11: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

Copyright © 2015, 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

• Fully statically typed (no dynamic typing sneaking in)

–More typing with less typing

List<String> list = getList();Collections.sort(list, (String x, String y) -> x.length() - y.length());

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

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

Page 12: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

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

Method References

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

• Can also be used for constructor references

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

FileFilter x = File::canRead;

ArrayList<String> list = ArrayList<>::new;

Page 13: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

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

Library Evolution

Page 14: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

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

Library Evolution Goal

• Requirement: aggregate operations on collections–New methods required on Collections to facilitate this

• This is problematic– Can’t add new methods to interfaces without modifying all implementations

– Can’t necessarily find or control all implementations

int heaviestBlueBlock = blocks

.filter(b -> b.getColor() == BLUE)

.map(Block::getWeight)

.reduce(0, Integer::max);

Page 15: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

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

Solution: Default Methods

• Specified in the interface

• From the caller’s perspective, just an ordinary interface method

• Provides a default implementation• Default only used when implementation classes do not provide a body for the

extension method

• Implementation classes can provide a better version, or not

interface Collection<E> {

default Stream<E> stream() {

return StreamSupport.stream(spliterator());

}

}

Page 16: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

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

Virtual Extension Methods

• Err, isn’t this implementing multiple inheritance for Java?

• Yes, but Java already has multiple inheritance of types

• This adds multiple inheritance of behavior too

• But not state, which is where most of the trouble is

• Can still be a source of complexity • Class implements two interfaces, both of which have default methods

• Same signature

• How does the compiler differentiate?

• Static methods also allowed in interfaces in Java SE 8

Stop right there!

Page 17: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

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

Functional Interface Definition

• Single Abstract Method (SAM) type

• A functional interface is an interface that has one abstract method

– Represents a single function contract

– Doesn’t mean it only has one method

• @FunctionalInterface annotation– Helps ensure the functional interface contract is honoured

– Compiler error if not a SAM

Page 18: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

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

Lambdas In Full Flow:Streams

Page 19: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

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

Stream Overview

• Abstraction for specifying aggregate computations

– Not a data structure

– Can be infinite

• Simplifies the description of aggregate computations– Exposes opportunities for optimisation

– Fusing, laziness and parallelism

At The High Level

Page 20: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

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

Stream Overview

• 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

Pipeline

int total = transactions.stream().filter(t -> t.getBuyer().getCity().equals(“London”)).mapToInt(Transaction::getPrice).sum();

Source

Intermediate operation

Terminal operation

Page 21: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

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

Stream Sources

• 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

Many Ways To Create

Page 22: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

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

Stream Terminal Operations

• The pipeline is only evaluated when the terminal operation is called

– All operations can execute sequentially or in parallel

– Intermediate operations can be merged• Avoiding multiple redundant passes on data

• Short-circuit operations (e.g. findFirst)

• Lazy evaluation

– Stream characteristics help identify optimisations• DISTINT stream passed to distinct() is a no-op

Page 23: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

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

Maps and FlatMapsMap Values in a Stream

Map

FlatMap

Input Stream

Input Stream

1-to-1 mapping

1-to-many mapping

Output Stream

Output Stream

Page 24: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

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

Optional Class

• 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

Helping To Eliminate the NullPointerException

Page 25: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

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

Example 1Convert words in list to upper case

List<String> output = wordList.stream().map(String::toUpperCase).collect(Collectors.toList());

Page 26: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

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

Example 1Convert words in list to upper case (in parallel)

List<String> output = wordList.parallelStream().map(String::toUpperCase).collect(Collectors.toList());

Page 27: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

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

Example 2

• BufferedReader has new method

– Stream<String> lines()

Count lines in a file

long count = bufferedReader.lines().count();

Page 28: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

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

Example 3Join lines 3-4 into a single string

String output = bufferedReader.lines().skip(2).limit(2).collect(Collectors.joining());

Page 29: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

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

Example 4Collect all words in a file into a list

List<String> output = reader.lines().flatMap(line -> Stream.of(line.split(REGEXP))).filter(word -> word.length() > 0).collect(Collectors.toList());

Page 30: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

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

Example 5List of unique words in lowercase, sorted by length

List<String> output = reader.lines().flatMap(line -> Stream.of(line.split(REGEXP))).filter(word -> word.length() > 0).map(String::toLowerCase).distinct().sorted((x, y) -> x.length() - y.length()).collect(Collectors.toList());

Page 31: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

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

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

Page 32: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 32

Page 33: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 33

Page 34: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract
Page 35: Java SE 8: Lambda Expressions And The Stream API€¢ Single-method interfaces are used extensively in Java –Definition: a functional interface is an interface with one abstract