SlideShare une entreprise Scribd logo
1  sur  51
Agenda
• Java 8
• Lambdas
• Method References
• Default Methods
Lambda
• Stream Operations
• Intermediate vs. Terminal
• Stateless vs. Stateful
• Short-Circuiting
• Collectors
• Parallel Streams
• Benchmark Sequential and Paralllel stream
Stream
Java-style functional programming
CollectionsStreams
Functional
interfaces
Functional
Java
Java™ SE 8 Release Contents
 JSR 335: Lambda Expressions
closures
 JEP 107: Bulk Data Operations for Collections
for-each
filter
map
reduce
http://www.jcp.org/en/jsr/detail?id=337
http://openjdk.java.net/jeps/107
Object
Oriented
ReflectiveStructured Functional
Generic
Concurrent GenericImperative
“…Is a blend of imperative and
object oriented programming
enhanced with functional flavors”
 Lambda expression is like a method –params, body
 Parameters – declared or inferred type
 (int x) -> x +1
 (x) -> x+1
 Lambda body – single expression or block
 Unlike anonymous class, this correspond to encl0sing class
 Any local variable used in lambda body must be declared final or
effectively final
 void m1(int x) { int y = 1; foo(() -> x+y); // Legal: x and y are both
effectively final. }
 A local variable or a method, constructor, lambda, or exception
parameter is effectively final if it is not final but it never occurs as the
left hand operand of an assignment operator (15.26) or as the operand of
an increment or decrement operator
void m6(int x) { foo(() -> x+1); x++; // Illegal: x is not effectively final. }
Lambda Syntax
/* argument list */
(int x, int y) -> { return x*y; }
(x, y) -> { return x*y; }
x -> { return x*2; }
() -> { System.out.println("Do you think this will work?"); }
() -> {throw new RuntimeException();}
/* single expression */
b -> { b.getMissingPages() > threshold ? b.setCondition(BAD)
: b.setCondition(GOOD) }
/* list of statements */
b -> {
Condition c = computeCondition(b.getMissingPages());
b.setCondition(c);
}
FewcommonusagesofLambdaexpression
Anonymous Class
Event Handling
Iterate over List
Parallel processing of Collection elements at API
level
Functional Programming
Streams(Collection) - Map , Reduce, Filter …
Lambda expression vs Anonymous
Classes
 this keyword
 What they are compiled into?
Functional Interfaces(FI)
• Lambdas are backed by interfaces
• Single abstract methods
• Functional Interface = Interface w/ 1 Method
• Names of Interface and Method are irrelevant
• Java API defines FI in java.util.function package
@FunctionalInterface
public interface Calculator
{
int calculate(int x, int y);
}
Calculator multiply = (x, y) -> x * y;
Calculator divide = (x, y) -> x / y;
int product = multiply.calculate(10, 20);
int quotient = divide.calculate(10, 20);
someMethod(multiply, divide);
anotherMethod((x, y) -> x ^ y);
 interface Runnable { void run(); }
// Functional
 interface Foo { boolean equals(Object obj); }
// Not functional; equals is already an implicit member
 interface Bar extends Foo { int compare(String o1, String o2);
}
// Functional; Bar has one abstract non-Object method
 interface Comparator<T> {
boolean equals(Object obj);
int compare(T o1, T o2);
}
// Functional; Comparator has one abstract non-Object method
Functional Interfaces
Function <T, R>
R apply(T t);
Supplier<T
>
T get()
Functional
Interfaces
Consumer
Function
Predicate
Supplier
Consumer<T>
void accept(T t);
Predicate<T>
boolean test(T
t);
Some usages of FI in JavaAPI
 Consumer
Iterable.forEach(Consumer<? super T> action)
 Supplier
ThreadLocal(Supplier<T> supplier)
 Predicate
Conditions like AND, OR, NEGATE, TEST…
ArrayList.removeIf(Predicate<? super E> filter)
public static void filter(List<?> names, Predicate<Object> condition)
{ names.stream().filter((name) ->
(condition.test(name))).forEach((name) -> { System.out.println(name +
" "); }); }
 Function
Comparator
Collections.sort(empList, (Employee e1, Employee e2) ->
e1.id.compareTo(e2.id));
Method References
books.forEach(b -> b.fixSpellingErrors());
books.forEach(Book::fixSpellingErrors); // instance method
books.forEach(b -> BookStore.generateISBN(b));
books.forEach(BookStore::generateISBN); // static method
books.forEach(b -> System.out.println(b.toString()));
books.forEach(System.out::println); // expression
Stream<ISBN> isbns1 = books.map(b -> new ISBN(b));
Stream<ISBN> isbns2 = books.map(ISBN::new); // constructor
Default methods
 Default methods enable new functionality to be
added to the interfaces of libraries and ensure binary
compatibility with code written for older versions of
those interfaces.
@FunctionalInterface
public interface Calculator
{
int calculate(int x, int y);
default int multiply(int x, int y)
{
return x * y;
}
}
• Can be overloaded
• Can be static or instance based
• Introduce multiple inheritance
interface java.lang.Iterable<T> {
abstract Iterator<T> iterator();
default void forEach(Consumer<? super T> consumer) {
for (T t : this) {
consumer.accept(t);
}
}
}
java.lang.Iterable<Object> i = () ->
java.util.Collection.emptyList().iterator();
Operation 1
Operation
2
Operation
3
Operation
4
Stream
Lambda Lambda Lambda Lambda
Streams
 A pipes-and-filters based API for collections
This may be familiar...
ps -ef | grep java | cut -c 1-9 | sort -n | uniq
 A Stream is an abstraction that represents zero or more values (not objects)
 Pipelines
A stream source
Zero or more intermediate operations
a terminal operations
A pipeline can be executed in parallel
 interface java.util.stream.Stream<T>
forEach()
filter()
map()
reduce()
…
 java.util.Collection<T>
Stream<T> stream()
Stream<T> parallelStream()
Streams can be obtained in a number of ways. Some examples include:
• From a Collection via the stream() and parallelStream() methods;
• From an array via Arrays.stream(Object[]);
• From static factory methods on the stream classes, such as
Stream.of(Object[]), IntStream.range(int, int) or Stream.iterate(Object,
UnaryOperator);
• The lines of a file can be obtained from BufferedReader.lines();
• Streams of file paths can be obtained from methods in Files;
• Streams of random numbers can be obtained from Random.ints();
• Numerous other stream-bearing methods in the JDK, including
BitSet.stream(), Pattern.splitAsStream(java.lang.CharSequence), and
JarFile.stream().
Creating and using a Stream
List<Book> myBooks = …;
Stream<Book> books = myBooks.stream();
Stream<Book> goodBooks =
books.filter(b -> b.getStarRating() > 3);
goodBooks.forEach(b -> System.out.println(b.toString()));
Properties of Streams
 Streams do not store elements…
…they are a view on top of a data structure
 Operations provided by Streams...
…are applied to the underlying data source elements
 Stream Operations can take as a parameter…
…Lambda expressions
…Method references
 Manipulating the underlying data source...
…will yield a ConcurrentModificationException
Stream Operations
builder() Returns a builder for a Stream.
filter(Predicate<? super T> predicate) Returns a stream consisting of the
elements of this stream that match the given predicate.
flatMap(Function<? super T,? extends Stream<? extends R>> mapper) Returns a
stream consisting of the results of replacing each element of this stream with
the contents of a mapped stream produced by applying the provided mapping
function to each element.
reduce(BinaryOperator<T> accumulator) Performs a reduction on the elements
of this stream, using an associative accumulation function, and returns an
Optional describing the reduced value, if any.
iterate(T seed, UnaryOperator<T> f) Returns an infinite sequential ordered
Stream produced by iterative application of a function f to an initial element
seed, producing a Stream consisting of seed, f(seed), f(f(seed)), etc.
peek(Consumer<? super T> action) Returns a stream consisting of the elements
of this stream, additionally performing the provided action on each element as
elements are consumed from the resulting stream.
Stream
operations
Build
Filter
Map
Reduce
Iterate
Peek
Intermediate vs. Terminal
 Intermediate: Output is another Stream
filter()
map()
…
 Terminal: Do something else with the Stream
forEach()
reduce()
…
double totalPrice = books.mapToDouble(Book::getPrice)
.reduce(0.0, (p1, p2) -> p1+p2);
Stream Evaluation
 Intermediate Streams are not evaluated…
…until a Terminal Operation is invoked on them
 Intermediate = Lazy
 Terminal = Eager (Consuming)
 This allows Java to…
…do some code optimization during compilation
…avoid buffering intermediate Streams
…handle parallel Streams more easily
Stateless Intermediate Operations
 Operation need nothing other than the current Stream
element to perform its work
 Examples
map()  Maps element to something else
filter()  Apply predicate and keep or drop element
List<Book> myBooks = ...;
double impairments = myBooks.stream()
.filter(b -> b.getCondition().equals(BAD))
.mapToDouble(Book::getPrice)
.reduce(0.0, (p1, p2) -> p1 + p2);
Stateful Intermediate Operations
 Operations that require not only the current stream element
but also additional state
distinct()  Element goes to next stage if it appears the first time
sorted()  Sort elements into natural order
sorted(Comparator)  Sort according to provided Comparator
substream(long)  Discard elements up to provided offset
substream(long, long)  Keep only elements in between offsets
limit(long)  Discard any elements after the provided max. size
myBooks.stream().map(Book::getAuthor).distinct().forEach(System.out::println);
Short-Circuiting Operations
 Processing might stop before the last element of the
Stream is reached
Intermediate
limit(long)
substream(long, long)
Terminal
anyMatch(Predicate)
allMatch(Predicate)
noneMatch(Predicate)
findFirst()
findAny()
Author rp = new Author("Rosamunde Pilcher");
boolean phew = myBooks.stream()
.map(Book::getAuthor)
.noneMatch(isEqual(rp));
System.out.println("Am I safe? " + phew);
Collectors
 <R> R collect(Collector<? super T, A, R> col)
Collect the elements of a Stream into some other data
structure
Powerful and complex tool
Collector is not so easy to implement, but…
 …luckily there are lots of factory methods for everyday
use in java.util.stream.Collectors
toList()
toSet()
toCollection(Supplier)
toMap(Function, Function)
…
Collector Examples
List<Author> authors = myBooks.stream()
.map(Book::getAuthor)
.collect(Collectors.toList());
double averagePages = myBooks.stream()
.collect(Collectors.averagingInt(Book::getPages));
Parallel Streams
• Uses fork-join used under the hood
• Thread pool sized to # cores
• Order can be changed
Parallel Streams
Imperative
Serial
Stream
Parallel
Stream
8,128 0 1 0
33,550,336 190 229 66
8,589,869,056 48648 59646 13383
137,438,691,328 778853 998776 203651
private static boolean isPerfect(long n)
{
return n > 0 &&
LongStream.rangeClosed(1, n / 2). parallel().
filter(i -> n % i == 0).
reduce(0, (l, r) -> l + r) == n;
}
List<Long> perfectNumbers =
LongStream.rangeClosed(1, 8192).parallel().
filter(PerfectNumberFinder::isPerfect).
collect(ArrayList<Long>::new, ArrayList<Long>::add, ArrayList<Long>::addAll);
Parallelization
• Must avoid side-effects and mutating
state
• Problems must fit the associativity
property
• Ex: ((a * b) * c) = (a * (b * c))
• Must be enough parallelizable code
• Performance not always better
• Can’t modify local variables (unlike for
loops)
Streams
Good
• Allow abstraction of details
• Communicate intent clearly
• Concise
• On-demand parallelization
Bad
• Loss of flexibility and control
• Increased code density
• Can be less efficient
• On-demand parallelization
 Lambda expressions
 Remove the Permanent Generation
 Small VM
 Parallel Array Sorting
 Bulk Data Operations for Collections
 Define a standard API for Base64 encoding and
decoding
 New Date & Time API
 Provide stronger Password-Based-Encryption (PBE)
algorithm implementations in the SunJCE provider
Optional
 One interesting new class, used in the Stream API, is
Optional in java.util.
 It is basically an alternative to using null explicitly - it is
returned by some stream operators when it is not
certain that there is a result (e.g. when reducing).
 To check whether it has any contents, isPresent can be
called. If an Option has contents, get will return it.
SoundCard soundcard = ...;
if(soundcard != null){
System.out.println(soundcard);
}
You can use the ifPresent() method, as follows:
Optional<Soundcard> soundcard = ...;
soundcard.ifPresent(System.out::println);
Spliterator
 A spliterator is the parallel analogue of an Iterator; it
describes a (possibly infinite) collection of elements, with
support for sequentially advancing, bulk traversal, and
splitting off some portion of the input into another
spliterator which can be processed in parallel.
 At the lowest level, all streams are driven by a spliterator.
 To support the parallel execution of the pipeline, the data
elements in the original collection must be split over multiple
threads.
 The Spliterator interface, also in java.util, provides this
functionality.
 The method trySplit returns a new Spliterator that manages a
subset of the elements of the original Spliterator. The original
Spliterator then skips elements in the subset that was
delegated. An ideal Spliterator might delegate the
management of half of its elements to a new Spliterator (up
to a certain threshold), so that users can easily break down
the set of data, e.g. for parallelization purposes.
Joining Collector
 Used for concatenation of CharSequences
 Internally implemented using StringBuilder
A lot more efficient than a Map-Reduce with
intermediately concatenated Strings
// not efficient due to recursive String concatenation. And ugly.
String titleList = myBooks.stream().map(Book::getTitle).reduce("", (t1, t2) -> t1+t2);
// Still inefficient. Still ugly (initial line break)
titleList = myBooks.stream().map(Book::getTitle).reduce("", (t1, t2) -> t1+"n"+t2);
// more efficient thanks to StringBuilder. Pretty printed.
titleList = myBooks.stream().map(Book::getTitle).collect(Collectors.joining("n"));
Projects based on Lambda and
streams
 Apache Spark
 Spring-io sagan
 Jlinq (http://www.jinq.org/)
Functional Interfaces
 There will be also new functional interfaces, such as
Predicate<T> and Block<T>
 Default: java.util.function.Consumer<T>
public interface Stream<T> {
void forEach(Consumer<? super T> consumer);
}
public interface Consumer<T> {void accept(T t);}
Consumer<Book> reduceRankForBadAuthors =
(Book b) -> { if (b.getStarRating() < 2) b.getAuthor().addRank(-1); };
books.forEach(reduceRankForBadAuthors);
books.forEach(b -> b.setEstimatedReadingTime(90*b.getPages()));
Terminal = Consuming Operations
 Intermediate Operations can be chained
 Only one Terminal Operation can be invoked
 Best avoid reference variables to Streams entirely by
using Fluent Programming
Construction  (Intermediate)*  Terminal;
books.forEach(b -> System.out.println("Book: " + b.getTitle()));
double totalPrice = books.reduce(0.0, (b1, b2)
-> b1.getPrice() + b2.getPrice());
Exception in thread "main" java.lang.IllegalStateException:
stream has already been operated upon or closed

Contenu connexe

Tendances

Collections Api - Java
Collections Api - JavaCollections Api - Java
Collections Api - JavaDrishti Bhalla
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentationVan Huong
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8icarter09
 
Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.David Gómez García
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections frameworkRiccardo Cardin
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesGanesh Samarthyam
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in javaCPD INDIA
 
Collections In Java
Collections In JavaCollections In Java
Collections In JavaBinoj T E
 
Java collections concept
Java collections conceptJava collections concept
Java collections conceptkumar gaurav
 
Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets Hitesh-Java
 
Java Lambda Expressions.pptx
Java Lambda Expressions.pptxJava Lambda Expressions.pptx
Java Lambda Expressions.pptxSameerAhmed593310
 
Java 8 lambda expressions
Java 8 lambda expressionsJava 8 lambda expressions
Java 8 lambda expressionsLogan Chien
 
Introduction to Java 8
Introduction to Java 8Introduction to Java 8
Introduction to Java 8Knoldus Inc.
 

Tendances (20)

Java 8 streams
Java 8 streamsJava 8 streams
Java 8 streams
 
Collections Api - Java
Collections Api - JavaCollections Api - Java
Collections Api - Java
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8
 
Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections framework
 
Java collection
Java collectionJava collection
Java collection
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional Interfaces
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 
Collections In Java
Collections In JavaCollections In Java
Collections In Java
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
 
Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets
 
Java Lambda Expressions.pptx
Java Lambda Expressions.pptxJava Lambda Expressions.pptx
Java Lambda Expressions.pptx
 
Java 8 lambda expressions
Java 8 lambda expressionsJava 8 lambda expressions
Java 8 lambda expressions
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
 
List in java
List in javaList in java
List in java
 
Introduction to Java 8
Introduction to Java 8Introduction to Java 8
Introduction to Java 8
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
 

En vedette

Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsIntroduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsEmiel Paasschens
 
50 new things you can do with java 8
50 new things you can do with java 850 new things you can do with java 8
50 new things you can do with java 8José Paumard
 
Java 8-streams-collectors-patterns
Java 8-streams-collectors-patternsJava 8-streams-collectors-patterns
Java 8-streams-collectors-patternsJosé Paumard
 
50 nouvelles choses que l'on peut faire avec Java 8
50 nouvelles choses que l'on peut faire avec Java 850 nouvelles choses que l'on peut faire avec Java 8
50 nouvelles choses que l'on peut faire avec Java 8José Paumard
 
Java 8, Streams & Collectors, patterns, performances and parallelization
Java 8, Streams & Collectors, patterns, performances and parallelizationJava 8, Streams & Collectors, patterns, performances and parallelization
Java 8, Streams & Collectors, patterns, performances and parallelizationJosé Paumard
 
Les Streams sont parmi nous
Les Streams sont parmi nousLes Streams sont parmi nous
Les Streams sont parmi nousJosé Paumard
 
Java 8 : Un ch'ti peu de lambda
Java 8 : Un ch'ti peu de lambdaJava 8 : Un ch'ti peu de lambda
Java 8 : Un ch'ti peu de lambdaCh'ti JUG
 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8Martin Toshev
 
Retours sur java 8 devoxx fr 2016
Retours sur java 8 devoxx fr 2016Retours sur java 8 devoxx fr 2016
Retours sur java 8 devoxx fr 2016Jean-Michel Doudoux
 
55 New Features in Java SE 8
55 New Features in Java SE 855 New Features in Java SE 8
55 New Features in Java SE 8Simon Ritter
 

En vedette (14)

Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsIntroduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
 
55 New Features in Java 7
55 New Features in Java 755 New Features in Java 7
55 New Features in Java 7
 
Java 8 Lambda
Java 8 LambdaJava 8 Lambda
Java 8 Lambda
 
Java8
Java8Java8
Java8
 
50 new things you can do with java 8
50 new things you can do with java 850 new things you can do with java 8
50 new things you can do with java 8
 
Java 8-streams-collectors-patterns
Java 8-streams-collectors-patternsJava 8-streams-collectors-patterns
Java 8-streams-collectors-patterns
 
50 nouvelles choses que l'on peut faire avec Java 8
50 nouvelles choses que l'on peut faire avec Java 850 nouvelles choses que l'on peut faire avec Java 8
50 nouvelles choses que l'on peut faire avec Java 8
 
Java 8, Streams & Collectors, patterns, performances and parallelization
Java 8, Streams & Collectors, patterns, performances and parallelizationJava 8, Streams & Collectors, patterns, performances and parallelization
Java 8, Streams & Collectors, patterns, performances and parallelization
 
Java SE 8 best practices
Java SE 8 best practicesJava SE 8 best practices
Java SE 8 best practices
 
Les Streams sont parmi nous
Les Streams sont parmi nousLes Streams sont parmi nous
Les Streams sont parmi nous
 
Java 8 : Un ch'ti peu de lambda
Java 8 : Un ch'ti peu de lambdaJava 8 : Un ch'ti peu de lambda
Java 8 : Un ch'ti peu de lambda
 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8
 
Retours sur java 8 devoxx fr 2016
Retours sur java 8 devoxx fr 2016Retours sur java 8 devoxx fr 2016
Retours sur java 8 devoxx fr 2016
 
55 New Features in Java SE 8
55 New Features in Java SE 855 New Features in Java SE 8
55 New Features in Java SE 8
 

Similaire à Java 8 Lambda and Streams

Functional Java 8 in everyday life
Functional Java 8 in everyday lifeFunctional Java 8 in everyday life
Functional Java 8 in everyday lifeAndrea Iacono
 
CBStreams - Java Streams for ColdFusion (CFML)
CBStreams - Java Streams for ColdFusion (CFML)CBStreams - Java Streams for ColdFusion (CFML)
CBStreams - Java Streams for ColdFusion (CFML)Ortus Solutions, Corp
 
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...Ortus Solutions, Corp
 
Hadoop_Pennonsoft
Hadoop_PennonsoftHadoop_Pennonsoft
Hadoop_PennonsoftPennonSoft
 
Performing Data Science with HBase
Performing Data Science with HBasePerforming Data Science with HBase
Performing Data Science with HBaseWibiData
 
Artigo 81 - spark_tutorial.pdf
Artigo 81 - spark_tutorial.pdfArtigo 81 - spark_tutorial.pdf
Artigo 81 - spark_tutorial.pdfWalmirCouto3
 
Building Kafka Connectors with Kotlin: A Step-by-Step Guide to Creation and D...
Building Kafka Connectors with Kotlin: A Step-by-Step Guide to Creation and D...Building Kafka Connectors with Kotlin: A Step-by-Step Guide to Creation and D...
Building Kafka Connectors with Kotlin: A Step-by-Step Guide to Creation and D...HostedbyConfluent
 
JavaScript Robotics
JavaScript RoboticsJavaScript Robotics
JavaScript RoboticsAnna Gerber
 
Eclipse Day India 2015 - Java 8 Overview
Eclipse Day India 2015 - Java 8 OverviewEclipse Day India 2015 - Java 8 Overview
Eclipse Day India 2015 - Java 8 OverviewEclipse Day India
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side JavascriptJulie Iskander
 
Functional Scala
Functional ScalaFunctional Scala
Functional ScalaStan Lea
 
AI與大數據數據處理 Spark實戰(20171216)
AI與大數據數據處理 Spark實戰(20171216)AI與大數據數據處理 Spark實戰(20171216)
AI與大數據數據處理 Spark實戰(20171216)Paul Chao
 
Java8 training - Class 1
Java8 training  - Class 1Java8 training  - Class 1
Java8 training - Class 1Marut Singh
 
Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 StreamsCharles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streamsjessitron
 
Functional Reactive Programming (FRP): Working with RxJS
Functional Reactive Programming (FRP): Working with RxJSFunctional Reactive Programming (FRP): Working with RxJS
Functional Reactive Programming (FRP): Working with RxJSOswald Campesato
 

Similaire à Java 8 Lambda and Streams (20)

Java 8
Java 8Java 8
Java 8
 
Functional Java 8 in everyday life
Functional Java 8 in everyday lifeFunctional Java 8 in everyday life
Functional Java 8 in everyday life
 
Hadoop - Introduction to mapreduce
Hadoop -  Introduction to mapreduceHadoop -  Introduction to mapreduce
Hadoop - Introduction to mapreduce
 
CBStreams - Java Streams for ColdFusion (CFML)
CBStreams - Java Streams for ColdFusion (CFML)CBStreams - Java Streams for ColdFusion (CFML)
CBStreams - Java Streams for ColdFusion (CFML)
 
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...
 
Hadoop_Pennonsoft
Hadoop_PennonsoftHadoop_Pennonsoft
Hadoop_Pennonsoft
 
Performing Data Science with HBase
Performing Data Science with HBasePerforming Data Science with HBase
Performing Data Science with HBase
 
Artigo 81 - spark_tutorial.pdf
Artigo 81 - spark_tutorial.pdfArtigo 81 - spark_tutorial.pdf
Artigo 81 - spark_tutorial.pdf
 
Building Kafka Connectors with Kotlin: A Step-by-Step Guide to Creation and D...
Building Kafka Connectors with Kotlin: A Step-by-Step Guide to Creation and D...Building Kafka Connectors with Kotlin: A Step-by-Step Guide to Creation and D...
Building Kafka Connectors with Kotlin: A Step-by-Step Guide to Creation and D...
 
Java gets a closure
Java gets a closureJava gets a closure
Java gets a closure
 
JavaScript Robotics
JavaScript RoboticsJavaScript Robotics
JavaScript Robotics
 
Eclipse Day India 2015 - Java 8 Overview
Eclipse Day India 2015 - Java 8 OverviewEclipse Day India 2015 - Java 8 Overview
Eclipse Day India 2015 - Java 8 Overview
 
cb streams - gavin pickin
cb streams - gavin pickincb streams - gavin pickin
cb streams - gavin pickin
 
Lambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter LawreyLambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter Lawrey
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side Javascript
 
Functional Scala
Functional ScalaFunctional Scala
Functional Scala
 
AI與大數據數據處理 Spark實戰(20171216)
AI與大數據數據處理 Spark實戰(20171216)AI與大數據數據處理 Spark實戰(20171216)
AI與大數據數據處理 Spark實戰(20171216)
 
Java8 training - Class 1
Java8 training  - Class 1Java8 training  - Class 1
Java8 training - Class 1
 
Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 StreamsCharles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streams
 
Functional Reactive Programming (FRP): Working with RxJS
Functional Reactive Programming (FRP): Working with RxJSFunctional Reactive Programming (FRP): Working with RxJS
Functional Reactive Programming (FRP): Working with RxJS
 

Plus de Venkata Naga Ravi

Plus de Venkata Naga Ravi (12)

Microservices with Docker
Microservices with Docker Microservices with Docker
Microservices with Docker
 
Processing Large Data with Apache Spark -- HasGeek
Processing Large Data with Apache Spark -- HasGeekProcessing Large Data with Apache Spark -- HasGeek
Processing Large Data with Apache Spark -- HasGeek
 
Quick Trip with Docker
Quick Trip with DockerQuick Trip with Docker
Quick Trip with Docker
 
Glint with Apache Spark
Glint with Apache SparkGlint with Apache Spark
Glint with Apache Spark
 
Flocker
FlockerFlocker
Flocker
 
Big Data Benchmarking
Big Data BenchmarkingBig Data Benchmarking
Big Data Benchmarking
 
Go Lang
Go LangGo Lang
Go Lang
 
Kubernetes
KubernetesKubernetes
Kubernetes
 
NoSQL & HBase overview
NoSQL & HBase overviewNoSQL & HBase overview
NoSQL & HBase overview
 
Software Defined Network - SDN
Software Defined Network - SDNSoftware Defined Network - SDN
Software Defined Network - SDN
 
Virtual Container - Docker
Virtual Container - Docker Virtual Container - Docker
Virtual Container - Docker
 
In Memory Analytics with Apache Spark
In Memory Analytics with Apache SparkIn Memory Analytics with Apache Spark
In Memory Analytics with Apache Spark
 

Dernier

Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxAndreas Kunz
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 

Dernier (20)

Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 

Java 8 Lambda and Streams

  • 1.
  • 2. Agenda • Java 8 • Lambdas • Method References • Default Methods Lambda • Stream Operations • Intermediate vs. Terminal • Stateless vs. Stateful • Short-Circuiting • Collectors • Parallel Streams • Benchmark Sequential and Paralllel stream Stream
  • 4. Java™ SE 8 Release Contents  JSR 335: Lambda Expressions closures  JEP 107: Bulk Data Operations for Collections for-each filter map reduce http://www.jcp.org/en/jsr/detail?id=337 http://openjdk.java.net/jeps/107
  • 5. Object Oriented ReflectiveStructured Functional Generic Concurrent GenericImperative “…Is a blend of imperative and object oriented programming enhanced with functional flavors”
  • 6.
  • 7.  Lambda expression is like a method –params, body  Parameters – declared or inferred type  (int x) -> x +1  (x) -> x+1  Lambda body – single expression or block  Unlike anonymous class, this correspond to encl0sing class  Any local variable used in lambda body must be declared final or effectively final  void m1(int x) { int y = 1; foo(() -> x+y); // Legal: x and y are both effectively final. }  A local variable or a method, constructor, lambda, or exception parameter is effectively final if it is not final but it never occurs as the left hand operand of an assignment operator (15.26) or as the operand of an increment or decrement operator void m6(int x) { foo(() -> x+1); x++; // Illegal: x is not effectively final. }
  • 8. Lambda Syntax /* argument list */ (int x, int y) -> { return x*y; } (x, y) -> { return x*y; } x -> { return x*2; } () -> { System.out.println("Do you think this will work?"); } () -> {throw new RuntimeException();} /* single expression */ b -> { b.getMissingPages() > threshold ? b.setCondition(BAD) : b.setCondition(GOOD) } /* list of statements */ b -> { Condition c = computeCondition(b.getMissingPages()); b.setCondition(c); }
  • 9. FewcommonusagesofLambdaexpression Anonymous Class Event Handling Iterate over List Parallel processing of Collection elements at API level Functional Programming Streams(Collection) - Map , Reduce, Filter …
  • 10. Lambda expression vs Anonymous Classes  this keyword  What they are compiled into?
  • 11. Functional Interfaces(FI) • Lambdas are backed by interfaces • Single abstract methods • Functional Interface = Interface w/ 1 Method • Names of Interface and Method are irrelevant • Java API defines FI in java.util.function package @FunctionalInterface public interface Calculator { int calculate(int x, int y); } Calculator multiply = (x, y) -> x * y; Calculator divide = (x, y) -> x / y; int product = multiply.calculate(10, 20); int quotient = divide.calculate(10, 20); someMethod(multiply, divide); anotherMethod((x, y) -> x ^ y);
  • 12.  interface Runnable { void run(); } // Functional  interface Foo { boolean equals(Object obj); } // Not functional; equals is already an implicit member  interface Bar extends Foo { int compare(String o1, String o2); } // Functional; Bar has one abstract non-Object method  interface Comparator<T> { boolean equals(Object obj); int compare(T o1, T o2); } // Functional; Comparator has one abstract non-Object method
  • 13. Functional Interfaces Function <T, R> R apply(T t); Supplier<T > T get() Functional Interfaces Consumer Function Predicate Supplier Consumer<T> void accept(T t); Predicate<T> boolean test(T t);
  • 14. Some usages of FI in JavaAPI  Consumer Iterable.forEach(Consumer<? super T> action)  Supplier ThreadLocal(Supplier<T> supplier)  Predicate Conditions like AND, OR, NEGATE, TEST… ArrayList.removeIf(Predicate<? super E> filter) public static void filter(List<?> names, Predicate<Object> condition) { names.stream().filter((name) -> (condition.test(name))).forEach((name) -> { System.out.println(name + " "); }); }  Function Comparator Collections.sort(empList, (Employee e1, Employee e2) -> e1.id.compareTo(e2.id));
  • 15.
  • 16. Method References books.forEach(b -> b.fixSpellingErrors()); books.forEach(Book::fixSpellingErrors); // instance method books.forEach(b -> BookStore.generateISBN(b)); books.forEach(BookStore::generateISBN); // static method books.forEach(b -> System.out.println(b.toString())); books.forEach(System.out::println); // expression Stream<ISBN> isbns1 = books.map(b -> new ISBN(b)); Stream<ISBN> isbns2 = books.map(ISBN::new); // constructor
  • 17.
  • 18. Default methods  Default methods enable new functionality to be added to the interfaces of libraries and ensure binary compatibility with code written for older versions of those interfaces. @FunctionalInterface public interface Calculator { int calculate(int x, int y); default int multiply(int x, int y) { return x * y; } } • Can be overloaded • Can be static or instance based • Introduce multiple inheritance interface java.lang.Iterable<T> { abstract Iterator<T> iterator(); default void forEach(Consumer<? super T> consumer) { for (T t : this) { consumer.accept(t); } } } java.lang.Iterable<Object> i = () -> java.util.Collection.emptyList().iterator();
  • 20. Streams  A pipes-and-filters based API for collections This may be familiar... ps -ef | grep java | cut -c 1-9 | sort -n | uniq  A Stream is an abstraction that represents zero or more values (not objects)  Pipelines A stream source Zero or more intermediate operations a terminal operations A pipeline can be executed in parallel  interface java.util.stream.Stream<T> forEach() filter() map() reduce() …  java.util.Collection<T> Stream<T> stream() Stream<T> parallelStream()
  • 21. Streams can be obtained in a number of ways. Some examples include: • From a Collection via the stream() and parallelStream() methods; • From an array via Arrays.stream(Object[]); • From static factory methods on the stream classes, such as Stream.of(Object[]), IntStream.range(int, int) or Stream.iterate(Object, UnaryOperator); • The lines of a file can be obtained from BufferedReader.lines(); • Streams of file paths can be obtained from methods in Files; • Streams of random numbers can be obtained from Random.ints(); • Numerous other stream-bearing methods in the JDK, including BitSet.stream(), Pattern.splitAsStream(java.lang.CharSequence), and JarFile.stream().
  • 22. Creating and using a Stream List<Book> myBooks = …; Stream<Book> books = myBooks.stream(); Stream<Book> goodBooks = books.filter(b -> b.getStarRating() > 3); goodBooks.forEach(b -> System.out.println(b.toString()));
  • 23. Properties of Streams  Streams do not store elements… …they are a view on top of a data structure  Operations provided by Streams... …are applied to the underlying data source elements  Stream Operations can take as a parameter… …Lambda expressions …Method references  Manipulating the underlying data source... …will yield a ConcurrentModificationException
  • 24.
  • 25. Stream Operations builder() Returns a builder for a Stream. filter(Predicate<? super T> predicate) Returns a stream consisting of the elements of this stream that match the given predicate. flatMap(Function<? super T,? extends Stream<? extends R>> mapper) Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. reduce(BinaryOperator<T> accumulator) Performs a reduction on the elements of this stream, using an associative accumulation function, and returns an Optional describing the reduced value, if any. iterate(T seed, UnaryOperator<T> f) Returns an infinite sequential ordered Stream produced by iterative application of a function f to an initial element seed, producing a Stream consisting of seed, f(seed), f(f(seed)), etc. peek(Consumer<? super T> action) Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream. Stream operations Build Filter Map Reduce Iterate Peek
  • 26.
  • 27. Intermediate vs. Terminal  Intermediate: Output is another Stream filter() map() …  Terminal: Do something else with the Stream forEach() reduce() … double totalPrice = books.mapToDouble(Book::getPrice) .reduce(0.0, (p1, p2) -> p1+p2);
  • 28. Stream Evaluation  Intermediate Streams are not evaluated… …until a Terminal Operation is invoked on them  Intermediate = Lazy  Terminal = Eager (Consuming)  This allows Java to… …do some code optimization during compilation …avoid buffering intermediate Streams …handle parallel Streams more easily
  • 29.
  • 30. Stateless Intermediate Operations  Operation need nothing other than the current Stream element to perform its work  Examples map()  Maps element to something else filter()  Apply predicate and keep or drop element List<Book> myBooks = ...; double impairments = myBooks.stream() .filter(b -> b.getCondition().equals(BAD)) .mapToDouble(Book::getPrice) .reduce(0.0, (p1, p2) -> p1 + p2);
  • 31. Stateful Intermediate Operations  Operations that require not only the current stream element but also additional state distinct()  Element goes to next stage if it appears the first time sorted()  Sort elements into natural order sorted(Comparator)  Sort according to provided Comparator substream(long)  Discard elements up to provided offset substream(long, long)  Keep only elements in between offsets limit(long)  Discard any elements after the provided max. size myBooks.stream().map(Book::getAuthor).distinct().forEach(System.out::println);
  • 32.
  • 33. Short-Circuiting Operations  Processing might stop before the last element of the Stream is reached Intermediate limit(long) substream(long, long) Terminal anyMatch(Predicate) allMatch(Predicate) noneMatch(Predicate) findFirst() findAny() Author rp = new Author("Rosamunde Pilcher"); boolean phew = myBooks.stream() .map(Book::getAuthor) .noneMatch(isEqual(rp)); System.out.println("Am I safe? " + phew);
  • 34.
  • 35. Collectors  <R> R collect(Collector<? super T, A, R> col) Collect the elements of a Stream into some other data structure Powerful and complex tool Collector is not so easy to implement, but…  …luckily there are lots of factory methods for everyday use in java.util.stream.Collectors toList() toSet() toCollection(Supplier) toMap(Function, Function) …
  • 36. Collector Examples List<Author> authors = myBooks.stream() .map(Book::getAuthor) .collect(Collectors.toList()); double averagePages = myBooks.stream() .collect(Collectors.averagingInt(Book::getPages));
  • 37.
  • 38. Parallel Streams • Uses fork-join used under the hood • Thread pool sized to # cores • Order can be changed
  • 39. Parallel Streams Imperative Serial Stream Parallel Stream 8,128 0 1 0 33,550,336 190 229 66 8,589,869,056 48648 59646 13383 137,438,691,328 778853 998776 203651 private static boolean isPerfect(long n) { return n > 0 && LongStream.rangeClosed(1, n / 2). parallel(). filter(i -> n % i == 0). reduce(0, (l, r) -> l + r) == n; } List<Long> perfectNumbers = LongStream.rangeClosed(1, 8192).parallel(). filter(PerfectNumberFinder::isPerfect). collect(ArrayList<Long>::new, ArrayList<Long>::add, ArrayList<Long>::addAll);
  • 40. Parallelization • Must avoid side-effects and mutating state • Problems must fit the associativity property • Ex: ((a * b) * c) = (a * (b * c)) • Must be enough parallelizable code • Performance not always better • Can’t modify local variables (unlike for loops)
  • 41. Streams Good • Allow abstraction of details • Communicate intent clearly • Concise • On-demand parallelization Bad • Loss of flexibility and control • Increased code density • Can be less efficient • On-demand parallelization
  • 42.
  • 43.
  • 44.  Lambda expressions  Remove the Permanent Generation  Small VM  Parallel Array Sorting  Bulk Data Operations for Collections  Define a standard API for Base64 encoding and decoding  New Date & Time API  Provide stronger Password-Based-Encryption (PBE) algorithm implementations in the SunJCE provider
  • 45. Optional  One interesting new class, used in the Stream API, is Optional in java.util.  It is basically an alternative to using null explicitly - it is returned by some stream operators when it is not certain that there is a result (e.g. when reducing).  To check whether it has any contents, isPresent can be called. If an Option has contents, get will return it. SoundCard soundcard = ...; if(soundcard != null){ System.out.println(soundcard); } You can use the ifPresent() method, as follows: Optional<Soundcard> soundcard = ...; soundcard.ifPresent(System.out::println);
  • 46. Spliterator  A spliterator is the parallel analogue of an Iterator; it describes a (possibly infinite) collection of elements, with support for sequentially advancing, bulk traversal, and splitting off some portion of the input into another spliterator which can be processed in parallel.  At the lowest level, all streams are driven by a spliterator.  To support the parallel execution of the pipeline, the data elements in the original collection must be split over multiple threads.  The Spliterator interface, also in java.util, provides this functionality.  The method trySplit returns a new Spliterator that manages a subset of the elements of the original Spliterator. The original Spliterator then skips elements in the subset that was delegated. An ideal Spliterator might delegate the management of half of its elements to a new Spliterator (up to a certain threshold), so that users can easily break down the set of data, e.g. for parallelization purposes.
  • 47. Joining Collector  Used for concatenation of CharSequences  Internally implemented using StringBuilder A lot more efficient than a Map-Reduce with intermediately concatenated Strings // not efficient due to recursive String concatenation. And ugly. String titleList = myBooks.stream().map(Book::getTitle).reduce("", (t1, t2) -> t1+t2); // Still inefficient. Still ugly (initial line break) titleList = myBooks.stream().map(Book::getTitle).reduce("", (t1, t2) -> t1+"n"+t2); // more efficient thanks to StringBuilder. Pretty printed. titleList = myBooks.stream().map(Book::getTitle).collect(Collectors.joining("n"));
  • 48. Projects based on Lambda and streams  Apache Spark  Spring-io sagan  Jlinq (http://www.jinq.org/)
  • 49.
  • 50. Functional Interfaces  There will be also new functional interfaces, such as Predicate<T> and Block<T>  Default: java.util.function.Consumer<T> public interface Stream<T> { void forEach(Consumer<? super T> consumer); } public interface Consumer<T> {void accept(T t);} Consumer<Book> reduceRankForBadAuthors = (Book b) -> { if (b.getStarRating() < 2) b.getAuthor().addRank(-1); }; books.forEach(reduceRankForBadAuthors); books.forEach(b -> b.setEstimatedReadingTime(90*b.getPages()));
  • 51. Terminal = Consuming Operations  Intermediate Operations can be chained  Only one Terminal Operation can be invoked  Best avoid reference variables to Streams entirely by using Fluent Programming Construction  (Intermediate)*  Terminal; books.forEach(b -> System.out.println("Book: " + b.getTitle())); double totalPrice = books.reduce(0.0, (b1, b2) -> b1.getPrice() + b2.getPrice()); Exception in thread "main" java.lang.IllegalStateException: stream has already been operated upon or closed