Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this...

31
Contrasting Java 8 Streams with Other Technologies & Java Libraries Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Professor of Computer Science Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA

Transcript of Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this...

Page 1: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

Contrasting Java 8 Streams with

Other Technologies & Java Libraries

Douglas C. [email protected]

www.dre.vanderbilt.edu/~schmidt

Professor of Computer Science

Institute for Software

Integrated Systems

Vanderbilt University

Nashville, Tennessee, USA

Page 2: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

2

Learning Objectives in this Lesson• Understand how Java 8 streams compare with other technologies

Page 3: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

3

Learning Objectives in this Lesson• Understand how Java 8 streams compare with other technologies

• Understand how Java 8 streams compare with other Java libraries

Page 4: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

4

Contrasting Java 8 Streams with Other

Technologies

Page 5: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

5

• A Java 8 stream is an implementation of the POSA1 Pipes & Filters pattern

See hillside.net/plop/2011/papers/B-10-Hanmer.pdf

Divide an app’s tasks into multiple self-contained data processing steps & connect these steps via intermediate

data buffers to form a data processing pipeline

Overview of Java 8 Streams

Page 6: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

6

• There are other common implementations of Pipes & Filters

Overview of Java 8 Streams

Page 7: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

7

• There are other common implementations of Pipes & Filters, e.g.

• A pipeline in UNIX command-lineshells, e.g.

See en.wikipedia.org/wiki/Pipeline_(Unix)

Overview of Java 8 Streams

find /usr/bin | #produce

sed 's:.*/::' | #strip directory part

grep -i '^z' | #select certain items

sort | #sort items

xargs -d '\n' #print as single line

Unlike a Java 8 stream, a filter in a UNIX pipeline processes all contents it receives

Page 8: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

8

• There are other common implementations of Pipes & Filters, e.g.

• A pipeline in UNIX command-lineshells

• System V STREAMS

See en.wikipedia.org/wiki/STREAMS

Overview of Java 8 Streams

Page 9: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

9

Contrasting Java 8 Streams with Other

Java Libraries

Page 10: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

10

• Java I/O streams are different from Java 8 streams!

Contrasting Java I/O Streams & Java 8 Streams

See docs.oracle.com/javase/tutorial/essential/io/streams.html

Aggregate operation (Function f)

Input x

Output f(x)

Output g(f(x))

Output h(g(f(x)))

Aggregate operation (Function g)

Aggregate operation (Function h)

Page 11: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

11

• Java I/O streams are different from Java 8 streams!

• They are often used together in Java programs

Contrasting Java I/O Streams & Java 8 Streams

Aggregate operation (Function f)

Input x

Output f(x)

Output g(f(x))

Output h(g(f(x)))

Aggregate operation (Function g)

Aggregate operation (Function h)

Page 12: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

12See www.oracle.com/technetwork/articles/java/ma14-java-se-8-streams-2177646.html

Contrasting Collections & Streams• Java collections are also different from Java 8 streams!

Aggregate operation (Function f)

Input x

Output f(x)

Output g(f(x))

Output h(g(f(x)))

Aggregate operation (Function g)

Aggregate operation (Function h)

Page 13: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

13

• A collection is an in-memory data structure that can store, retrieve, & manipulate groups of elements

Contrasting Collections & Streams

See docs.oracle.com/javase/tutorial/collections/intro

java.util.ArrayList size: 5

elementData

x:0.0

Point

y:0.0

x:0.0

Point

y:0.0

x:0.0

Point

y:0.0

x:0.0

Point

y:0.0

x:0.0

Point

y:0.0

0 1 2 3 4

Page 14: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

14

• A collection is an in-memory data structure that can store, retrieve, & manipulate groups of elements

• It is analogous to a DVD

Contrasting Collections & Streams

All content exists statically

Page 15: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

15

• A stream is a fixed data structure that processes elements on-demand

Contrasting Collections & Streams

See tutorials.jenkov.com/java-collections/streams.html

Aggregate operation (Function f)

Input x

Output f(x)

Output g(f(x))

Output h(g(f(x)))

Aggregate operation (Function g)

Aggregate operation (Function h)

A stream can manipulate elements obtained from a collection without

explicitly iterating over them

Page 16: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

16

• A stream is a fixed data structure that processes elements on-demand

• A Java stream is analogous to a flow of bytes in a streaming video

Contrasting Collections & Streams

Content is dynamically received & processed

Page 17: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

17

• Various factory methods can convert collections to streams & vice versa

Contrasting Collections & Streams

Aggregate operation (Function f)

Input x

Output f(x)

Output g(f(x))

Output h(g(f(x)))

Aggregate operation (Function g)

Aggregate operation (Function h)

stream()

parallelStream()

collect()

Page 18: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

18

• Various factory methods can convert collections to streams & vice versa

Contrasting Collections & Streams

Aggregate operation (Function f)

Input x

Output f(x)

Output g(f(x))

Output h(g(f(x)))

Aggregate operation (Function g)

Aggregate operation (Function h)

stream()

parallelStream()

collect()

Page 19: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

19

• Various factory methods can convert collections to streams & vice versa

Contrasting Collections & Streams

Aggregate operation (Function f)

Input x

Output f(x)

Output g(f(x))

Output h(g(f(x)))

Aggregate operation (Function g)

Aggregate operation (Function h)

stream()

parallelStream()

collect()

Page 20: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

20

• A simple example of manipulating a Java collection

String[] urlArray = {

"http://www.cse.wustl.edu.edu/~schmidt/ka.png",

"http://www.cse.wustl.edu.edu/~schmidt/robot.png",

"http://www.cse.wustl.edu.edu/~schmidt/kitten.png"};

List<String> urls = Arrays.asList(urlArray);

for (int i = 0; i < urls.size(); ++i)

urls.set(i,

urls.get(i).replace("cse.wustl","dre.vanderbilt"));

Contrasting Collections & Streams

Page 21: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

21

• A simple example of manipulating a Java collection

String[] urlArray = {

"http://www.cse.wustl.edu.edu/~schmidt/ka.png",

"http://www.cse.wustl.edu.edu/~schmidt/robot.png",

"http://www.cse.wustl.edu.edu/~schmidt/kitten.png"};

List<String> urls = Arrays.asList(urlArray);

for (int i = 0; i < urls.size(); ++i)

urls.set(i,

urls.get(i).replace("cse.wustl","dre.vanderbilt"));

Contrasting Collections & Streams

Create a list from an array

Page 22: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

22

• A simple example of manipulating a Java collection

String[] urlArray = {

"http://www.cse.wustl.edu.edu/~schmidt/ka.png",

"http://www.cse.wustl.edu.edu/~schmidt/robot.png",

"http://www.cse.wustl.edu.edu/~schmidt/kitten.png"};

List<String> urls = Arrays.asList(urlArray);

for (int i = 0; i < urls.size(); ++i)

urls.set(i,

urls.get(i).replace("cse.wustl","dre.vanderbilt"));

Explicitly iterate through a list & modify each value

Contrasting Collections & Streams

Page 23: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

23

Contrasting Collections & Streams• A simple example of manipulating a Java stream

String[] urlArray = {

"http://www.cse.wustl.edu.edu/~schmidt/ka.png",

"http://www.cse.wustl.edu.edu/~schmidt/robot.png",

"http://www.cse.wustl.edu.edu/~schmidt/kitten.png"};

List<String> urls = Stream

.of(urlArray)

.map(s ->

s.replace("cse.wustl", "dre.vanderbilt"))

.collect(toList());

Page 24: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

24

Implicitly iterate through a pipeline of elements from a collection source, transform each value, & create a collection result

Contrasting Collections & Streams• A simple example of manipulating a Java stream

String[] urlArray = {

"http://www.cse.wustl.edu.edu/~schmidt/ka.png",

"http://www.cse.wustl.edu.edu/~schmidt/robot.png",

"http://www.cse.wustl.edu.edu/~schmidt/kitten.png"};

List<String> urls = Stream

.of(urlArray)

.map(s ->

s.replace("cse.wustl", "dre.vanderbilt"))

.collect(toList());

Page 25: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

25

Implicitly iterate through a pipeline of elements from a collection source, transform each value, & create a collection result

Contrasting Collections & Streams• A simple example of manipulating a Java stream

String[] urlArray = {

"http://www.cse.wustl.edu.edu/~schmidt/ka.png",

"http://www.cse.wustl.edu.edu/~schmidt/robot.png",

"http://www.cse.wustl.edu.edu/~schmidt/kitten.png"};

List<String> urls = Stream

.of(urlArray)

.map(s ->

s.replace("cse.wustl", "dre.vanderbilt"))

.collect(toList());

Page 26: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

26

Implicitly iterate through a pipeline of elements from a collection source, transform each value, & create a collection result

Contrasting Collections & Streams• A simple example of manipulating a Java stream

String[] urlArray = {

"http://www.cse.wustl.edu.edu/~schmidt/ka.png",

"http://www.cse.wustl.edu.edu/~schmidt/robot.png",

"http://www.cse.wustl.edu.edu/~schmidt/kitten.png"};

List<String> urls = Stream

.of(urlArray)

.map(s ->

s.replace("cse.wustl", "dre.vanderbilt"))

.collect(toList());

Page 27: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

27

Implicitly iterate through a pipeline of elements from a collection source, transform each value, & create a collection result

Contrasting Collections & Streams• A simple example of manipulating a Java stream

String[] urlArray = {

"http://www.cse.wustl.edu.edu/~schmidt/ka.png",

"http://www.cse.wustl.edu.edu/~schmidt/robot.png",

"http://www.cse.wustl.edu.edu/~schmidt/kitten.png"};

List<String> urls = Stream

.of(urlArray)

.map(s ->

s.replace("cse.wustl", "dre.vanderbilt"))

.collect(toList());

Page 28: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

28

Contrasting Collections & Streams• A simple example of manipulating a Java stream

String[] urlArray = {

"http://www.cse.wustl.edu.edu/~schmidt/ka.png",

"http://www.cse.wustl.edu.edu/~schmidt/robot.png",

"http://www.cse.wustl.edu.edu/~schmidt/kitten.png"};

List<String> urls = Stream

.of(urlArray)

.map(s ->

s.replace("cse.wustl", "dre.vanderbilt"))

.collect(toList());

Like iterators, elements in a stream can only be visited once during its lifetime

Page 29: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

29

Contrasting Collections & Streams

Key to the power of Java 8 streams is their ability to chain transformations

• A simple example of manipulating a Java stream

String[] urlArray = {

"http://www.cse.wustl.edu.edu/~schmidt/ka.png",

"http://www.cse.wustl.edu.edu/~schmidt/robot.png",

"http://www.cse.wustl.edu.edu/~schmidt/kitten.png"};

List<URL> urls = Stream

.of(urlArray)

.map(s ->

s.replace("cse.wustl", "dre.vanderbilt"))

.map(rethrowFunction(URL::new))

.collect(toList());

Page 30: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

30

Contrasting Collections & Streams• A simple example of manipulating a Java stream

String[] urlArray = {

"http://www.cse.wustl.edu.edu/~schmidt/ka.png",

"http://www.cse.wustl.edu.edu/~schmidt/robot.png",

"http://www.cse.wustl.edu.edu/~schmidt/kitten.png"};

List<URL> urls = Stream

.of(urlArray)

.map(s ->

s.replace("cse.wustl", "dre.vanderbilt"))

.map(rethrowFunction(URL::new))

.collect(toList());rethrowFunction() converts checked

exception into runtime exception

See stackoverflow.com/a/27661504/3312330

Page 31: Contrasting Java 8 Streams with Other Technologies & Java ... · 3 Learning Objectives in this Lesson •Understand how Java 8 streams compare with other technologies •Understand

31

End of Contrasting Java 8 Streams with Other Java Libraries