Hanoi jug java 8 stream api

Post on 17-Jul-2015

1.215 views 1 download

Transcript of Hanoi jug java 8 stream api

Hanoi JUG - 2015

Java 8: Stream API

Vu Dang Ngoc - eXo

Hanoi JUG - 2015

Introduction to Stream API

1. Stream API at glance

a. Aggregate Operations, Terminal Operations

b. Laziness and performance tuning

2. How Stream API change the way we coding?

3. Advantage and disadvantage

4. Further reading

Hanoi JUG - 2015

Stream example

Hanoi JUG - 2015

Stream API at glance

● Pipeline contain a data source, 1 or many intermediate

operation, 1 terminal operation and linked together by

streams.

Hanoi JUG - 2015

Stream source

Hanoi JUG - 2015

Operations

Hanoi JUG - 2015

Intermediate Operation

● Stream-producing: require a stream and

return another stream

● Always lazy

● Stateless and stateful operations

o Stateless (filter, map …): don’t require information of

other items.

o Stateful (distinct, sorted, limit, peek…): require

information of other items.

Hanoi JUG - 2015

Terminal Operation

● End of pipeline

● Push intermediate operator working

● Short-circuiting: stream don’t have to travel

all data.

Hanoi JUG - 2015

Laziness and performance tuning

● Intermediate operator run only when

Terminate operator asking

Hanoi JUG - 2015

Laziness and performance tuning

Hanoi JUG - 2015

Laziness and performance tuning

Hanoi JUG - 2015

Stream is not loop

continue to next

element

You have no power

here!

Hanoi JUG - 2015

How Stream API change the way we coding?

Hanoi JUG - 2015

How Stream API change the way we coding?

Hanoi JUG - 2015

How Stream API change the way we coding?

Hanoi JUG - 2015

How Stream API change the way we coding?

Hanoi JUG - 2015

How Stream API change the way we coding?

● Terminal operator collect provide many way

to transform data

● Collectors class provide built-in collection

feature.

Hanoi JUG - 2015

How Stream API change the way we coding?

Hanoi JUG - 2015

How Stream API change the way we coding?

Hanoi JUG - 2015

How Stream API change the way we coding?

Hanoi JUG - 2015

How Stream API change the way we coding?

Hanoi JUG - 2015

How Stream API change the way we coding?

Hanoi JUG - 2015

Parallel stream

● Using Fork-Join Pool

● Side-effect

● Some parallel stream operations like reduce

and collect need additional computations

● All sub-tasks should finish in a reasonable

time

Hanoi JUG - 2015

Parallel stream

Hanoi JUG - 2015

Parallel stream

Hanoi JUG - 2015

Parallel stream

Hanoi JUG - 2015

Performance compare

Hanoi JUG - 2015

Performance compare

Hanoi JUG - 2015

Performance compare

Hanoi JUG - 2015

Performance compare

Hanoi JUG - 2015

Performance compare

Hanoi JUG - 2015

Performance compare

Hanoi JUG - 2015

Performance compare

Hanoi JUG - 2015

Performance compare

Hanoi JUG - 2015

Performance compare

Hanoi JUG - 2015

Performance compare

Hanoi JUG - 2015

Performance compare

Hanoi JUG - 2015

Parallel stream performance is unstable

Hanoi JUG - 2015

Advantage and Disadvantage

● The Goodo Reduce size of code

o Easy using parallel computing

o Developer can focus how to process data

o Built-in feature of Collectors class

● The Bado No customize operator

o Cannot random access

Hanoi JUG - 2015

Advantage and Disadvantage

● And the Uglyo Stream cannot reuseable

o Parallel stream performance is unstable and

unpredictable

o Stream don’t store data and cannot modify source

collection.

o Can cause infinite stream

Hanoi JUG - 2015

Infinite stream

Hanoi JUG - 2015

Further reading

● Collection Pipes - article of Martin Fowler

about pipe and filter software pattern

● Java Fork-Join Calamity and Java Parallel

Calamity descript flaws of Fork/Join and

parallel stream

● Active vs Passive iterator

● Parallel Streams and Spliterators

Hanoi JUG - 2015

Referent

● http://www.java2novice.com/java-sorting-

algorithms/quick-sort/

● http://javapapers.com/java/java-stream-api/

● http://winterbe.com/posts/2014/07/31/java8-stream-

tutorial-examples/

● http://www.informit.com/articles/article.aspx?p=2198914

● http://blog.credera.com/technology-insights/java/java-8-

part-1-lamdas-streams-functional-interfaces/

● https://docs.oracle.com/javase/8/docs/api/java/util/strea

m/package-summary.html

● http://java.dzone.com/articles/think-twice-using-java-8

● http://blog.jooq.org/2014/06/13/java-8-friday-10-subtle-

mistakes-when-using-the-streams-api/

Hanoi JUG - 2015

Questions & Answers