183
MyFirstStream by @abelgplaza MADRID · NOV 18-19 · 2016 https://github.com/agplaza/my-first-stream

MyFirstStream: you can build it!

Embed Size (px)

Citation preview

Page 1: MyFirstStream: you can build it!

MyFirstStreamby @abelgplaza

MADRID · NOV 18-19 · 2016

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

Page 2: MyFirstStream: you can build it!

MyFirstStreamby abelgarciaplaza

MADRID · NOV 18-19 · 2016

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

Page 3: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

DISCLAIMER

Page 4: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

DISCLAIMER

● No developers will be harmed in the coding of

this workshop.

Page 5: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

DISCLAIMER

● No developers will be harmed in the coding of

this workshop.

● There is no intention for coding complexity.

Page 6: MyFirstStream: you can build it!

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.

Page 7: MyFirstStream: you can build it!

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.

Page 8: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Stream

Page 9: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Stream

A sequence of operations...

Page 10: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Stream

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

Page 11: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Stream

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

Page 12: MyFirstStream: you can build it!

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...

Page 13: MyFirstStream: you can build it!

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...

Page 14: MyFirstStream: you can build it!

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.

Page 15: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

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

Page 16: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

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

231

Page 17: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

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

2

1

Page 18: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

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

“2”

“1”

Page 19: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

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

“1”

“2”

Page 20: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

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

“1” “2”

Page 21: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Spliterator<T>

Page 22: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Spliterator<T>

boolean tryAdvance(Consumer<T> action);

Page 23: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Spliterator<T>

boolean tryAdvance(Consumer<T> action);

internal iteration

Page 24: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Spliterator<T>

boolean hasElement = iterator.hasNext();

if (hasElement) {

T element = iterator.next();

action.accept(element);

}

return hasElement;

Page 25: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Spliterator<T>

boolean tryAdvance(Consumer<T> action);

Page 26: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Spliterator<T>

boolean tryAdvance(Consumer<T> action);

Spliterator<T> trySplit();

Page 27: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Spliterator<T>

boolean tryAdvance(Consumer<T> action);

Spliterator<T> trySplit();

Page 28: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Sink<T>

Page 29: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Sink<T> extends Consumer<T>

void accept(T element);

Page 30: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Sink<T> extends Consumer<T>

void accept(T element);

void end();

Page 31: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Sink<T> extends Consumer<T>

void accept(T element);

void end();

Page 32: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

1 2

Page 33: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

1

Page 34: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Build 1

Page 35: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1);

Build 1

Page 36: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

source

Stream.of(2, 3, 1);

Build 1

Page 37: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1);

Build

spliterator

1

Page 38: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1);

Build

2 3 1

1

Page 39: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1);

P

Build

2 3 1

1

Page 40: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1);

P

Build

T

2 3 1

1

Page 41: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1);

?

Build

T

2 3 1

1

Page 42: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1);

?

Build

i

2 3 1

1

Page 43: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3);

?

Build

i

2 3 1

1

Page 44: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3);

filterP T?

Build

i

2 3 1

1

Page 45: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3);

filteri T?

Build

i

2 3 1

1

Page 46: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3);

filteri i?

Build

i

2 3 1

1

Page 47: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Stream.of(2, 3, 1)

.filter(n -> n < 3);

filteri i?

Build

i

2 3 1

1

Page 48: MyFirstStream: you can build it!

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

Page 49: MyFirstStream: you can build it!

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

Page 50: MyFirstStream: you can build it!

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

Page 51: MyFirstStream: you can build it!

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

Page 52: MyFirstStream: you can build it!

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

Page 53: MyFirstStream: you can build it!

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();

Page 54: MyFirstStream: you can build it!

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();

Page 55: MyFirstStream: you can build it!

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();

Page 56: MyFirstStream: you can build it!

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

Page 57: MyFirstStream: you can build it!

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();

Page 58: MyFirstStream: you can build it!

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

Page 59: MyFirstStream: you can build it!

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

Page 60: MyFirstStream: you can build it!

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

Page 61: MyFirstStream: you can build it!

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

Page 62: MyFirstStream: you can build it!

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

Page 63: MyFirstStream: you can build it!

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());

Page 64: MyFirstStream: you can build it!

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());

Page 65: MyFirstStream: you can build it!

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());

Page 66: MyFirstStream: you can build it!

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());

Page 67: MyFirstStream: you can build it!

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());

Page 68: MyFirstStream: you can build it!

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());

Page 69: MyFirstStream: you can build it!

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

Page 70: MyFirstStream: you can build it!

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());

Page 71: MyFirstStream: you can build it!

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());

Page 72: MyFirstStream: you can build it!

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());

Page 73: MyFirstStream: you can build it!

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());

Page 74: MyFirstStream: you can build it!

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());

Page 75: MyFirstStream: you can build it!

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

Page 76: MyFirstStream: you can build it!

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

Page 77: MyFirstStream: you can build it!

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

Page 78: MyFirstStream: you can build it!

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

Page 79: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Evaluate

2 3 1

2

Page 80: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Evaluate

2 3 1

2

wrappedSink.accept(2)

Page 81: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Evaluate

2 3 1

2

wrappedSink.accept(3)

Page 82: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Evaluate

2 3 1

2

wrappedSink.accept(1)

Page 83: MyFirstStream: you can build it!

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());

Page 84: MyFirstStream: you can build it!

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());

Page 85: MyFirstStream: you can build it!

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

Page 86: MyFirstStream: you can build it!

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

Page 87: MyFirstStream: you can build it!

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

Page 88: MyFirstStream: you can build it!

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

?

Page 89: MyFirstStream: you can build it!

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

Page 90: MyFirstStream: you can build it!

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

Page 91: MyFirstStream: you can build it!

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

Page 92: MyFirstStream: you can build it!

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

Page 93: MyFirstStream: you can build it!

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

Page 94: MyFirstStream: you can build it!

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

Page 95: MyFirstStream: you can build it!

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”

Page 96: MyFirstStream: you can build it!

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”

Page 97: MyFirstStream: you can build it!

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

Page 98: MyFirstStream: you can build it!

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

Page 99: MyFirstStream: you can build it!

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

Page 100: MyFirstStream: you can build it!

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

?

Page 101: MyFirstStream: you can build it!

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

Page 102: MyFirstStream: you can build it!

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

Page 103: MyFirstStream: you can build it!

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

Page 104: MyFirstStream: you can build it!

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

Page 105: MyFirstStream: you can build it!

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

Page 106: MyFirstStream: you can build it!

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

Page 107: MyFirstStream: you can build it!

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

?

Page 108: MyFirstStream: you can build it!

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

Page 109: MyFirstStream: you can build it!

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

Page 110: MyFirstStream: you can build it!

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

Page 111: MyFirstStream: you can build it!

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”

Page 112: MyFirstStream: you can build it!

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”

Page 113: MyFirstStream: you can build it!

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”

Page 114: MyFirstStream: you can build it!

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

Page 115: MyFirstStream: you can build it!

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”

Page 116: MyFirstStream: you can build it!

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!

Page 117: MyFirstStream: you can build it!

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()

Page 118: MyFirstStream: you can build it!

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()

Page 119: MyFirstStream: you can build it!

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()

Page 120: MyFirstStream: you can build it!

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

Page 121: MyFirstStream: you can build it!

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

Page 122: MyFirstStream: you can build it!

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

Page 123: MyFirstStream: you can build it!

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”

Page 124: MyFirstStream: you can build it!

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”

Page 125: MyFirstStream: you can build it!

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

Page 126: MyFirstStream: you can build it!

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()

Page 127: MyFirstStream: you can build it!

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”

Page 128: MyFirstStream: you can build it!

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 =

Page 129: MyFirstStream: you can build it!

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 =

Page 130: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

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

Page 131: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Methods

Page 132: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Methods

spliterator

iterator

iteration

Page 133: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Methods

spliterator

iterator

peek

distinct

sorted

onClose

filter

map

flatMap

limit

skip

iteration intermediate

Page 134: MyFirstStream: you can build it!

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

Page 135: MyFirstStream: you can build it!

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

Page 136: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Methods

count

findFirst

peek

distinct

filter

map

flatMap

findAny

forEach

toArray

iteration intermediate terminal

Page 137: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Methods

count

distinct

filter

map

toArray

iteration intermediate terminal

Page 138: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Example

Page 139: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

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

Page 140: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

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

Page 141: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

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

Page 142: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

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

Page 143: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

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

Page 144: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

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

Page 145: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

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

Page 146: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

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

Page 147: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Classes

Page 148: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Stream<T>Classes

Page 149: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Stream<T>Classes

SourceStream<T>

Page 150: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Stream<T>

Spliterator<T>

Classes

SourceStream<T>

Page 151: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Stream<T>

IteratorSpliterator<T>

Spliterator<T>

Classes

SourceStream<T>

Page 152: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Stream<T>

IteratorSpliterator<T>

Spliterator<T>

Classes

SourceStream<T> FilterStream<T>

Page 153: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

AbstractStream<P, T>

Stream<T>

IteratorSpliterator<T>

Spliterator<T>

Classes

SourceStream<T> FilterStream<T>

Page 154: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

AbstractStream<P, T>

ReferenceStream<P, T>

Stream<T>

IteratorSpliterator<T>

Spliterator<T>

Classes

SourceStream<T> FilterStream<T>

Page 155: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

AbstractStream<P, T>

ReferenceStream<P, T>

Sink<T> Stream<T>

IteratorSpliterator<T>

Spliterator<T>

Classes

SourceStream<T> FilterStream<T>

Page 156: MyFirstStream: you can build it!

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>

Page 157: MyFirstStream: you can build it!

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>

Page 158: MyFirstStream: you can build it!

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>

Page 159: MyFirstStream: you can build it!

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>

Page 160: MyFirstStream: you can build it!

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>

Page 161: MyFirstStream: you can build it!

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>

Page 162: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Terminal operation

Page 163: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

TerminalSink<T, R>

Terminal operation

CountSink<T>

1

Page 164: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

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

Terminal operation

CountSink<T>

1<<creates>>

Page 165: MyFirstStream: you can build it!

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

Page 166: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Intermediate operation

Page 167: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Sink<T>

Intermediate operation

FilterSink<T>

1

Page 168: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

ReferenceStream<P, T> Sink<T>

Intermediate operation

FilterStream<T> FilterSink<T>

2 1

Page 169: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

ReferenceStream<P, T> Sink<T>

Intermediate operation

FilterStream<T> FilterSink<T>

2 1<<creates>>

Page 170: MyFirstStream: you can build it!

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

Page 171: MyFirstStream: you can build it!

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

Page 172: MyFirstStream: you can build it!

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

Page 173: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Exercises

Page 174: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Exercises

1. count()

Page 175: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Exercises

1. count()

2. toArray()

Page 176: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Exercises

1. count()

2. toArray()

3. filter()

Page 177: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Exercises

1. count()

2. toArray()

3. filter()

4. map()

Page 178: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

Exercises

1. count()

2. toArray()

3. filter()

4. map()

5. distinct()

Page 179: MyFirstStream: you can build it!

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

Page 180: MyFirstStream: you can build it!

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>

Page 181: MyFirstStream: you can build it!

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

Page 182: MyFirstStream: you can build it!

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

Page 183: MyFirstStream: you can build it!

MADRID · NOV 18-19 · 2016

The Endspecial thanks to my workmates for their great feedback