MyFirstStream: you can build it!

Preview:

Citation preview

MyFirstStreamby @abelgplaza

MADRID · NOV 18-19 · 2016

https://github.com/agplaza/my-first-stream

MyFirstStreamby abelgarciaplaza

MADRID · NOV 18-19 · 2016

https://github.com/agplaza/my-first-stream

MADRID · NOV 18-19 · 2016

DISCLAIMER

MADRID · NOV 18-19 · 2016

DISCLAIMER

● No developers will be harmed in the coding of

this workshop.

MADRID · NOV 18-19 · 2016

DISCLAIMER

● No developers will be harmed in the coding of

this workshop.

● There is no intention for coding complexity.

MADRID · NOV 18-19 · 2016

DISCLAIMER

● No developers will be harmed in the coding of

this workshop.

● There is no intention for coding complexity.

● Non-parallel processing was assumed while

coding the materials of this workshop.

MADRID · NOV 18-19 · 2016

DISCLAIMER

● No developers will be harmed in the coding of

this workshop.

● There is no intention for coding complexity.

● Non-parallel processing was assumed while

coding the materials of this workshop.

● Any similarity with actual JDK 8 code is purely

coincidental.

MADRID · NOV 18-19 · 2016

Stream

MADRID · NOV 18-19 · 2016

Stream

A sequence of operations...

MADRID · NOV 18-19 · 2016

Stream

A sequence of operations to apply to the whole...

MADRID · NOV 18-19 · 2016

Stream

A sequence of operations to apply to the whole of a potentially infinite series of elements...

MADRID · NOV 18-19 · 2016

Stream

A sequence of operations to apply to the whole of a potentially infinite series of elements that is lazily evaluated...

MADRID · NOV 18-19 · 2016

Stream

A sequence of operations to apply to the whole of a potentially infinite series of elements that is lazily evaluated and internally iterated...

MADRID · NOV 18-19 · 2016

Stream

A sequence of operations to apply to the whole of a potentially infinite series of elements that is lazily evaluated and internally iterated only once.

MADRID · NOV 18-19 · 2016

StreamStream.of(2, 3, 1) .filter(n -> n < 3) .map(n -> n.toString()) .sorted() .collect(toList());

MADRID · NOV 18-19 · 2016

StreamStream.of(2, 3, 1) .filter(n -> n < 3) .map(n -> n.toString()) .sorted() .collect(toList());

231

MADRID · NOV 18-19 · 2016

StreamStream.of(2, 3, 1) .filter(n -> n < 3) .map(n -> n.toString()) .sorted() .collect(toList());

2

1

MADRID · NOV 18-19 · 2016

StreamStream.of(2, 3, 1) .filter(n -> n < 3) .map(n -> n.toString()) .sorted() .collect(toList());

“2”

“1”

MADRID · NOV 18-19 · 2016

StreamStream.of(2, 3, 1) .filter(n -> n < 3) .map(n -> n.toString()) .sorted() .collect(toList());

“1”

“2”

MADRID · NOV 18-19 · 2016

StreamStream.of(2, 3, 1) .filter(n -> n < 3) .map(n -> n.toString()) .sorted() .collect(toList());

“1” “2”

MADRID · NOV 18-19 · 2016

Spliterator<T>

MADRID · NOV 18-19 · 2016

Spliterator<T>

boolean tryAdvance(Consumer<T> action);

MADRID · NOV 18-19 · 2016

Spliterator<T>

boolean tryAdvance(Consumer<T> action);

internal iteration

MADRID · NOV 18-19 · 2016

Spliterator<T>

boolean hasElement = iterator.hasNext();

if (hasElement) {

T element = iterator.next();

action.accept(element);

}

return hasElement;

MADRID · NOV 18-19 · 2016

Spliterator<T>

boolean tryAdvance(Consumer<T> action);

MADRID · NOV 18-19 · 2016

Spliterator<T>

boolean tryAdvance(Consumer<T> action);

Spliterator<T> trySplit();

MADRID · NOV 18-19 · 2016

Spliterator<T>

boolean tryAdvance(Consumer<T> action);

Spliterator<T> trySplit();

MADRID · NOV 18-19 · 2016

Sink<T>

MADRID · NOV 18-19 · 2016

Sink<T> extends Consumer<T>

void accept(T element);

MADRID · NOV 18-19 · 2016

Sink<T> extends Consumer<T>

void accept(T element);

void end();

MADRID · NOV 18-19 · 2016

Sink<T> extends Consumer<T>

void accept(T element);

void end();

MADRID · NOV 18-19 · 2016

1 2

MADRID · NOV 18-19 · 2016

1

MADRID · NOV 18-19 · 2016

Build 1

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1);

Build 1

MADRID · NOV 18-19 · 2016

source

Stream.of(2, 3, 1);

Build 1

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1);

Build

spliterator

1

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1);

Build

2 3 1

1

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1);

P

Build

2 3 1

1

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1);

P

Build

T

2 3 1

1

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1);

?

Build

T

2 3 1

1

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1);

?

Build

i

2 3 1

1

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3);

?

Build

i

2 3 1

1

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3);

filterP T?

Build

i

2 3 1

1

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3);

filteri T?

Build

i

2 3 1

1

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3);

filteri i?

Build

i

2 3 1

1

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3);

filteri i?

Build

i

2 3 1

1

MADRID · NOV 18-19 · 2016

Build

filteri i? i

2 3 1

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString());

1

MADRID · NOV 18-19 · 2016

Build

filter mapi i P T? i

2 3 1

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString());

1

MADRID · NOV 18-19 · 2016

Build

filter mapi i i T? i

2 3 1

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString());

1

MADRID · NOV 18-19 · 2016

Build

filter mapi i i S? i

2 3 1

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString());

1

MADRID · NOV 18-19 · 2016

Build

filter mapi i i S? i

2 3 1

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString());

1

MADRID · NOV 18-19 · 2016

Build

filter mapi i i S? i

2 3 1

1Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted();

MADRID · NOV 18-19 · 2016

Build 1

filter map sortedi i i S P T? i

2 3 1

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted();

MADRID · NOV 18-19 · 2016

Build 1

filter map sortedi i i S S T? i

2 3 1

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted();

MADRID · NOV 18-19 · 2016

Build 1Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted();

filter map sortedi i i S S S? i

2 3 1

MADRID · NOV 18-19 · 2016

Build 1

filter map sortedi i i S S S? i

2 3 1

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted();

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

collect

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

collectT

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

collectS

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

collectS

MADRID · NOV 18-19 · 2016

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

MADRID · NOV 18-19 · 2016

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

MADRID · NOV 18-19 · 2016

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

MADRID · NOV 18-19 · 2016

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

MADRID · NOV 18-19 · 2016

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

MADRID · NOV 18-19 · 2016

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

wrapped

MADRID · NOV 18-19 · 2016

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

wrappedStream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

MADRID · NOV 18-19 · 2016

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

wrappedStream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

MADRID · NOV 18-19 · 2016

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

wrappedStream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

MADRID · NOV 18-19 · 2016

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

wrappedStream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

MADRID · NOV 18-19 · 2016

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

wrappedStream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

MADRID · NOV 18-19 · 2016

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2wrappedStream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

collect

sorted

map

filter

MADRID · NOV 18-19 · 2016

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2wrappedStream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

collect

sorted

map

filter

next

MADRID · NOV 18-19 · 2016

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2wrappedStream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

collect

sorted

map

filter

next

previous

MADRID · NOV 18-19 · 2016

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2wrappedStream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

collect

sorted

map

filter

MADRID · NOV 18-19 · 2016

Evaluate

2 3 1

2

MADRID · NOV 18-19 · 2016

Evaluate

2 3 1

2

wrappedSink.accept(2)

MADRID · NOV 18-19 · 2016

Evaluate

2 3 1

2

wrappedSink.accept(3)

MADRID · NOV 18-19 · 2016

Evaluate

2 3 1

2

wrappedSink.accept(1)

MADRID · NOV 18-19 · 2016

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

MADRID · NOV 18-19 · 2016

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

2 3 1i

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

2 3 1

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

3 1

2

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i2

3 1i

?

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i2

3 1i

OK

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

2

3 1i

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

3 1i

2

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

3 1i

2

i

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

3 1i

“2”

i

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

3 1i

“2”

i

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

3 1i

i

“2”

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

3 1i

i

“2”

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

“2”

3 1

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

“2”

1

3

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

“2”

3

i 1i

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

“2”

3

i 1i

?

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

“2”

3

i 1i

KO

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

“2”

i 1i

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

“2”

i 1i

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

“2”

1

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

“2”

1

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

“2”

1

ii

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

“2”

1

ii

?

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

“2”

1

ii

OK

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

“2”

ii

1

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

“2”

ii

1

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

“2”

ii

“1”

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

i

“2”

ii

“1”

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

i

“2”

ii

“1”

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

“2” “1”

ii

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

“2” “1”

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

“2” “1”

empty!

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

“2” “1”

ii

end()

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

“2” “1”

ii

end()

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

“2” “1”

ii

end()

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

“1” “2”

ii

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

S

i

“1” “2”

ii

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

i

“1” “2”

ii

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

i

“2”

ii

“1”

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

i

ii

“1” “2”

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

i

ii

“1” “2”

S

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

filter map sortedi i i S S S?

Evaluate

i

2 3 1

2

S

i

ii

“1” “2”

S

end()

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

Evaluate 2

“1” “2”

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

Evaluate 2

“1” “2”

List<String> result =

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3)

.map(n -> n.toString())

.sorted()

.collect(toList());

Evaluate 2

“1” “2”

List<String> result =

MADRID · NOV 18-19 · 2016

let’s codehttps://github.com/agplaza/my-first-stream

MADRID · NOV 18-19 · 2016

Methods

MADRID · NOV 18-19 · 2016

Methods

spliterator

iterator

iteration

MADRID · NOV 18-19 · 2016

Methods

spliterator

iterator

peek

distinct

sorted

onClose

filter

map

flatMap

limit

skip

iteration intermediate

MADRID · NOV 18-19 · 2016

Methods

spliterator

iterator

min

max

count

anyMatch

allMatch

noneMatch

findFirst

peek

distinct

sorted

onClose

filter

map

flatMap

limit

skip

findAny

forEach

reduce

collect

toArray

close

iteration intermediate terminal

MADRID · NOV 18-19 · 2016

Methods

spliterator

iterator

min

max

count

anyMatch

allMatch

noneMatch

findFirst

peek

distinct

sorted

onClose

filter

map

flatMap

limit

skip

findAny

forEach

reduce

collect

toArray

close

iteration intermediate terminal

MADRID · NOV 18-19 · 2016

Methods

count

findFirst

peek

distinct

filter

map

flatMap

findAny

forEach

toArray

iteration intermediate terminal

MADRID · NOV 18-19 · 2016

Methods

count

distinct

filter

map

toArray

iteration intermediate terminal

MADRID · NOV 18-19 · 2016

Example

MADRID · NOV 18-19 · 2016

ExampleStream.of(2, 3, 1) .filter(n -> n < 3) .map(n -> n.toString()) .sorted() .collect(toList());

MADRID · NOV 18-19 · 2016

ExampleStream.of(2, 3, 1) .filter(n -> n < 3) .map(n -> n.toString()) .sorted() .collect(toList());

MADRID · NOV 18-19 · 2016

ExampleStream.of(2, 3, 1) .filter(n -> n < 3) .map(n -> n.toString()) .distinct() .collect(toList());

MADRID · NOV 18-19 · 2016

ExampleStream.of(2, 3, 1) .filter(n -> n < 3) .map(n -> n.toString()) .distinct() .collect(toList());

MADRID · NOV 18-19 · 2016

ExampleStream.of(2, 3, 2, 1) .filter(n -> n < 3) .map(n -> n.toString()) .distinct() .collect(toList());

MADRID · NOV 18-19 · 2016

ExampleStream.of(2, 3, 2, 1) .filter(n -> n < 3) .map(n -> n.toString()) .distinct() .collect(toList());

MADRID · NOV 18-19 · 2016

ExampleStream.of(2, 3, 2, 1) .filter(n -> n < 3) .map(n -> n.toString()) .distinct() .toArray();

MADRID · NOV 18-19 · 2016

ExampleStream.of(2, 3, 2, 1) .filter(n -> n < 3) .map(n -> n.toString()) .distinct() .toArray();

MADRID · NOV 18-19 · 2016

Classes

MADRID · NOV 18-19 · 2016

Stream<T>Classes

MADRID · NOV 18-19 · 2016

Stream<T>Classes

SourceStream<T>

MADRID · NOV 18-19 · 2016

Stream<T>

Spliterator<T>

Classes

SourceStream<T>

MADRID · NOV 18-19 · 2016

Stream<T>

IteratorSpliterator<T>

Spliterator<T>

Classes

SourceStream<T>

MADRID · NOV 18-19 · 2016

Stream<T>

IteratorSpliterator<T>

Spliterator<T>

Classes

SourceStream<T> FilterStream<T>

MADRID · NOV 18-19 · 2016

AbstractStream<P, T>

Stream<T>

IteratorSpliterator<T>

Spliterator<T>

Classes

SourceStream<T> FilterStream<T>

MADRID · NOV 18-19 · 2016

AbstractStream<P, T>

ReferenceStream<P, T>

Stream<T>

IteratorSpliterator<T>

Spliterator<T>

Classes

SourceStream<T> FilterStream<T>

MADRID · NOV 18-19 · 2016

AbstractStream<P, T>

ReferenceStream<P, T>

Sink<T> Stream<T>

IteratorSpliterator<T>

Spliterator<T>

Classes

SourceStream<T> FilterStream<T>

MADRID · NOV 18-19 · 2016

AbstractStream<P, T>

ReferenceStream<P, T>

Sink<T> Stream<T>

IteratorSpliterator<T>

Spliterator<T>

Classes

SourceStream<T> FilterStream<T>

FilterSink<T>

MADRID · NOV 18-19 · 2016

AbstractStream<P, T>

ReferenceStream<P, T>

Sink<T> Stream<T>

IteratorSpliterator<T>

Spliterator<T>

Classes

SourceStream<T> FilterStream<T>

FilterSink<T>

MADRID · NOV 18-19 · 2016

AbstractStream<P, T>

ReferenceStream<P, T>

Sink<T>

TerminalSink<T, R>

Stream<T>

IteratorSpliterator<T>

Spliterator<T>

Classes

SourceStream<T> FilterStream<T>

FilterSink<T>

MADRID · NOV 18-19 · 2016

AbstractStream<P, T>

ReferenceStream<P, T>

Sink<T>

TerminalSink<T, R>

Stream<T>

IteratorSpliterator<T>

Spliterator<T>

Classes

SourceStream<T> FilterStream<T>

FilterSink<T>

MADRID · NOV 18-19 · 2016

AbstractStream<P, T>

ReferenceStream<P, T>

Sink<T>

TerminalSink<T, R>

Stream<T>

CountSink<T>

IteratorSpliterator<T>

Spliterator<T>

Classes

SourceStream<T> FilterStream<T>

FilterSink<T>

MADRID · NOV 18-19 · 2016

AbstractStream<P, T>

ReferenceStream<P, T>

Sink<T>

TerminalSink<T, R>

Stream<T>

CountSink<T>

IteratorSpliterator<T>

Spliterator<T>Unchecked

Classes

SourceStream<T> FilterStream<T>

FilterSink<T>

MADRID · NOV 18-19 · 2016

Terminal operation

MADRID · NOV 18-19 · 2016

TerminalSink<T, R>

Terminal operation

CountSink<T>

1

MADRID · NOV 18-19 · 2016

ReferenceStream<P, T> TerminalSink<T, R>

Terminal operation

CountSink<T>

1<<creates>>

MADRID · NOV 18-19 · 2016

ReferenceStream<P, T> TerminalSink<T, R>

Terminal operation

CountSink<T>

1<<creates>>

public long count() {

return evaluate(new CountSink<>());

}

2

MADRID · NOV 18-19 · 2016

Intermediate operation

MADRID · NOV 18-19 · 2016

Sink<T>

Intermediate operation

FilterSink<T>

1

MADRID · NOV 18-19 · 2016

ReferenceStream<P, T> Sink<T>

Intermediate operation

FilterStream<T> FilterSink<T>

2 1

MADRID · NOV 18-19 · 2016

ReferenceStream<P, T> Sink<T>

Intermediate operation

FilterStream<T> FilterSink<T>

2 1<<creates>>

MADRID · NOV 18-19 · 2016

ReferenceStream<P, T> Sink<T>

Intermediate operation

FilterStream<T> FilterSink<T>

2 1<<creates>>

public Sink<T> wrapSink(sink) {

return new FilterSink<>(sink);

}

3

MADRID · NOV 18-19 · 2016

ReferenceStream<P, T> Sink<T>

Intermediate operation

FilterStream<T> FilterSink<T>

2 1

<<creates>>

<<creates>>

public Sink<T> wrapSink(sink) {

return new FilterSink<>(sink);

}

3

MADRID · NOV 18-19 · 2016

ReferenceStream<P, T> Sink<T>

Intermediate operation

FilterStream<T> FilterSink<T>

2 1

<<creates>>

<<creates>>

public Stream<T> filter(...) {

return new FilterStream<>(this);

}

public Sink<T> wrapSink(sink) {

return new FilterSink<>(sink);

}

34

MADRID · NOV 18-19 · 2016

Exercises

MADRID · NOV 18-19 · 2016

Exercises

1. count()

MADRID · NOV 18-19 · 2016

Exercises

1. count()

2. toArray()

MADRID · NOV 18-19 · 2016

Exercises

1. count()

2. toArray()

3. filter()

MADRID · NOV 18-19 · 2016

Exercises

1. count()

2. toArray()

3. filter()

4. map()

MADRID · NOV 18-19 · 2016

Exercises

1. count()

2. toArray()

3. filter()

4. map()

5. distinct()

MADRID · NOV 18-19 · 2016

Methods

spliterator

iterator

min

max

count

anyMatch

allMatch

noneMatch

findFirst

peek

distinct

sorted

onClose

filter

map

flatMap

limit

skip

findAny

forEach

reduce

collect

toArray

close

iteration intermediate terminal

MADRID · NOV 18-19 · 2016

AbstractStream<P, T>

ReferenceStream<P, T>

Sink<T>

TerminalSink<T, R>

Stream<T>

CountSink<T>

IteratorSpliterator<T>

Spliterator<T>Unchecked

Classes

SourceStream<T> FilterStream<T>

FilterSink<T>

MADRID · NOV 18-19 · 2016

ReferenceStream<P, T> TerminalSink<T, R>

Terminal operation

CountSink<T>

1<<creates>>

public long count() {

return evaluate(new CountSink<>());

}

2

MADRID · NOV 18-19 · 2016

ReferenceStream<P, T> Sink<T>

Intermediate operation

FilterStream<T> FilterSink<T>

2 1

<<creates>>

<<creates>>

public Stream<T> filter(...) {

return new FilterStream<>(this);

}

public Sink<T> wrapSink(sink) {

return new FilterSink<>(sink);

}

34

MADRID · NOV 18-19 · 2016

The Endspecial thanks to my workmates for their great feedback

Recommended