jdk 8: stream stylethe following is intended to outline our general product direction. it is...

78

Upload: others

Post on 14-Aug-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

JDK 8: Stream Style

Sergey Kuksenko

[email protected], @kuksenk0

Page 2: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

The following is intended to outline our general product direction. Itis intended for information purposes only, and may not beincorporated into any contract. It is not a commitment to deliver anymaterial, code, or functionality, and should not be relied upon inmaking purchasing decisions. The development, release, and timingof any features or functionality described for Oracle’s productsremains at the sole discretion of Oracle.

Slide 2/48.

Page 3: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Motivation

Slide 3/48.

Page 4: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Motivation

Everything is well. Why do we need any Streams?

public void printGroups(List <People > people) {

Set <Group > groups = new HashSet <>();

for (Person p : people) {

if (p.getAge () >= 65)

groups.add(p.getGroup ());

}

List <Group > sorted = new ArrayList <>(groups );

Collections.sort(sorted , new Comparator <Group >() {

public int compare(Group a, Group b) {

return Integer.compare(a.getSize(), b.getSize ())

}

});

for (Group g : sorted)

System.out.println(g.getName ());

}

Slide 4/48.

Page 5: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Motivation

Everything is well. Why do we need any Streams?

public void printGroups(List <People > people) {

Set <Group > groups = new HashSet <>();

for (Person p : people) {

if (p.getAge () >= 65)

groups.add(p.getGroup ());

}

List <Group > sorted = new ArrayList <>(groups );

Collections.sort(sorted , new Comparator <Group >() {

public int compare(Group a, Group b) {

return Integer.compare(a.getSize(), b.getSize ())

}

});

for (Group g : sorted)

System.out.println(g.getName ());

}

Slide 4/48.

Page 6: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Motivation

It would be awesome to omit miles and miles of duplicated code.

public void printGroups(List <People > people) {

people.stream ()

.filter(p -> p.getAge () > 65)

.map(p -> p.getGroup ())

.distinct ()

.sorted(comparing(g -> g.getSize ()))

.map(g -> g.getName ())

.forEach(n -> System.out.println(n));

}

Slide 5/48.

Page 7: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Motivation

It would be awesome to do less work, and do it later (laziness).

public void printGroups(List <People > people) {

people.stream ()

.filter(p -> p.getAge () > 65)

.map(p -> p.getGroup ())

.distinct ()

.sorted(comparing(g -> g.getSize ()))

.map(g -> g.getName ())

.forEach(n -> System.out.println(n));//⇐ACTIONS!!!

}

Slide 6/48.

Page 8: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Motivation

Parallelism?

Collection <Item > data;

...

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

processItem(data.get(i));

}

Slide 7/48.

Page 9: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Motivation

Parallelism?

Collection <Item > data;

...

for (Item item : data) {

processItem(item);

}

Slide 8/48.

Page 10: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Motivation

Parallelism?

Collection <Item > data;

...

#pragma omgomp parallel

for (Item item : data) {

processItem(item);

}

Slide 9/48.

Page 11: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Motivation

Parallelism?

Collection <Item > data;

...

#pragma omp parallel

for (Item item : data) {

processItem(item);

}

Slide 10/48.

Page 12: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Motivation

Parallelism?

Collection <Item > data;

...

parallel_for (Item item : data) {

processItem(item);

}

Slide 11/48.

Page 13: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Motivation

Parallelism!

Collection <Item > data;

...

data.parallelStream ()

.forEach(item -> processItem(item ));

}

Slide 12/48.

Page 14: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Design

Slide 13/48.

Page 15: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Design

Most of the code fits the same simple pattern:

𝑠𝑜𝑢𝑟𝑐𝑒

→ 𝑜𝑝 → 𝑜𝑝 → 𝑜𝑝 →

«sources»: collections, iterators, channels, ...

«operations»: filter, map, reduce, ...

«sinks»: collections, locals, ...

Slide 14/48.

Page 16: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Design

Most of the code fits the same simple pattern:

𝑠𝑜𝑢𝑟𝑐𝑒 → 𝑜𝑝

→ 𝑜𝑝 → 𝑜𝑝 →

«sources»: collections, iterators, channels, ...

«operations»: filter, map, reduce, ...

«sinks»: collections, locals, ...

Slide 14/48.

Page 17: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Design

Most of the code fits the same simple pattern:

𝑠𝑜𝑢𝑟𝑐𝑒 → 𝑜𝑝 → 𝑜𝑝

→ 𝑜𝑝 →

«sources»: collections, iterators, channels, ...

«operations»: filter, map, reduce, ...

«sinks»: collections, locals, ...

Slide 14/48.

Page 18: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Design

Most of the code fits the same simple pattern:

𝑠𝑜𝑢𝑟𝑐𝑒 → 𝑜𝑝 → 𝑜𝑝 → 𝑜𝑝

«sources»: collections, iterators, channels, ...

«operations»: filter, map, reduce, ...

«sinks»: collections, locals, ...

Slide 14/48.

Page 19: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Design

Most of the code fits the same simple pattern:

𝑠𝑜𝑢𝑟𝑐𝑒 → 𝑜𝑝 → 𝑜𝑝 → 𝑜𝑝 →

«sources»: collections, iterators, channels, ...

«operations»: filter, map, reduce, ...

«sinks»: collections, locals, ...

Slide 14/48.

Page 20: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Design

Most of the code fits the same simple pattern:

𝑠𝑜𝑢𝑟𝑐𝑒 → 𝑜𝑝 → 𝑜𝑝 → 𝑜𝑝 → 𝑔𝑎𝑛𝑔𝑛𝑎𝑚𝑠𝑡𝑦𝑙𝑒

«sources»: collections, iterators, channels, ...

«operations»: filter, map, reduce, ...

«sinks»: collections, locals, ...

Slide 14/48.

Page 21: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Design

Most of the code fits the same simple pattern:

𝑠𝑜𝑢𝑟𝑐𝑒 → 𝑜𝑝 → 𝑜𝑝 → 𝑜𝑝 → 𝑠𝑖𝑛𝑘

«sources»: collections, iterators, channels, ...

«operations»: filter, map, reduce, ...

«sinks»: collections, locals, ...

Slide 14/48.

Page 22: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Design

Most of the code fits the same simple pattern:

𝑠𝑜𝑢𝑟𝑐𝑒 → 𝑜𝑝 → 𝑜𝑝 → 𝑜𝑝 → 𝑠𝑖𝑛𝑘

«sources»: collections, iterators, channels, ...

«operations»: filter, map, reduce, ...

«sinks»: collections, locals, ...

Slide 14/48.

Page 23: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Sources

Standard classes?not-yet-created classes?3𝑟𝑑 party classes?

Collection?should we put everything into collection?

Iterable?“Iterator Hell” (inherently sequential)interface pollution

Stream!new (just invented) class with required semanticinject the only stream() method into existing classes

Slide 15/48.

Page 24: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Sources

Standard classes?not-yet-created classes?3𝑟𝑑 party classes?

Collection?should we put everything into collection?

Iterable?“Iterator Hell” (inherently sequential)interface pollution

Stream!new (just invented) class with required semanticinject the only stream() method into existing classes

Slide 15/48.

Page 25: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Sources

Standard classes?not-yet-created classes?3𝑟𝑑 party classes?

Collection?should we put everything into collection?

Iterable?“Iterator Hell” (inherently sequential)interface pollution

Stream!new (just invented) class with required semanticinject the only stream() method into existing classes

Slide 15/48.

Page 26: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Sources

Standard classes?not-yet-created classes?3𝑟𝑑 party classes?

Collection?should we put everything into collection?

Iterable?“Iterator Hell” (inherently sequential)interface pollution

Stream!new (just invented) class with required semanticinject the only stream() method into existing classes

Slide 15/48.

Page 27: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Stream

Slide 16/48.

Page 28: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Stream

”A multiplicity of values“

May be unordered

Not a collection (no storage)

Operations are deferred as long as possible

May be infinite

Source is unmodifiable

Can be used only once

Sequential or parallel

Primitive specializations: IntStream, LongStream,

DoubleStream

Slide 17/48.

Page 29: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Stream pipeline

𝑎 𝑠𝑜𝑢𝑟𝑐𝑒: Source → Stream

𝑖𝑛𝑡𝑒𝑟𝑚𝑒𝑑𝑖𝑎𝑡𝑒 𝑜𝑝𝑒𝑟𝑎𝑡𝑖𝑜𝑛𝑠: Stream → Stream

𝑎 𝑡𝑒𝑟𝑚𝑖𝑛𝑎𝑙 𝑜𝑝𝑒𝑟𝑎𝑡𝑖𝑜𝑛: Stream → PROFIT!

public void printGroups(List <People > people) {

people.stream()

.filter(p -> p.getAge () > 65)

.map(p -> p.getGroup ())

.distinct()

.sorted(comparing(g -> g.getSize ()))

.map(g -> g.getName ())

.forEach(n -> System.out.println(n));

}

Slide 18/48.

Page 30: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Stream pipeline

𝑎 𝑠𝑜𝑢𝑟𝑐𝑒: Source → Stream

𝑖𝑛𝑡𝑒𝑟𝑚𝑒𝑑𝑖𝑎𝑡𝑒 𝑜𝑝𝑒𝑟𝑎𝑡𝑖𝑜𝑛𝑠: Stream → Stream

𝑎 𝑡𝑒𝑟𝑚𝑖𝑛𝑎𝑙 𝑜𝑝𝑒𝑟𝑎𝑡𝑖𝑜𝑛: Stream → PROFIT!

public void printGroups(List <People > people) {

people.stream()

.filter(p -> p.getAge () > 65)

.map(p -> p.getGroup ())

.distinct()

.sorted(comparing(g -> g.getSize ()))

.map(g -> g.getName ())

.forEach(n -> System.out.println(n));

}

Slide 18/48.

Page 31: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Stream pipeline

𝑎 𝑠𝑜𝑢𝑟𝑐𝑒: Source → Stream

𝑖𝑛𝑡𝑒𝑟𝑚𝑒𝑑𝑖𝑎𝑡𝑒 𝑜𝑝𝑒𝑟𝑎𝑡𝑖𝑜𝑛𝑠: Stream → Stream

𝑎 𝑡𝑒𝑟𝑚𝑖𝑛𝑎𝑙 𝑜𝑝𝑒𝑟𝑎𝑡𝑖𝑜𝑛: Stream → PROFIT!

public void printGroups(List <People > people) {

people.stream()

.filter(p -> p.getAge () > 65)

.map(p -> p.getGroup ())

.distinct()

.sorted(comparing(g -> g.getSize ()))

.map(g -> g.getName ())

.forEach(n -> System.out.println(n));

}

Slide 18/48.

Page 32: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Stream pipeline

𝑎 𝑠𝑜𝑢𝑟𝑐𝑒: Source → Stream

𝑖𝑛𝑡𝑒𝑟𝑚𝑒𝑑𝑖𝑎𝑡𝑒 𝑜𝑝𝑒𝑟𝑎𝑡𝑖𝑜𝑛𝑠: Stream → Stream

𝑎 𝑡𝑒𝑟𝑚𝑖𝑛𝑎𝑙 𝑜𝑝𝑒𝑟𝑎𝑡𝑖𝑜𝑛: Stream → PROFIT!

public void printGroups(List <People > people) {

Stream <People > s1=people.stream ();

Stream <People > s2=s1.filter(p -> p.getAge () > 65);

Stream <Group > s3=s2.map(p -> p.getGroup ());

Stream <Group > s4=s3.distinct ();

Stream <Group > s5=s4.sorted(comparing(g->g.getSize ()));

Stream <String > s6=s5.map(g -> g.getName ());

s6.forEach(n -> System.out.println(n));

}

Slide 19/48.

Page 33: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Stream Sources

Slide 20/48.

Page 34: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Stream Sources: collections

ArrayList <T> list;

Stream <T> s = list.stream (); // sized , ordered

HashSet <T> set;

Stream <T> s = set.stream (); // sized , distinct

TreeSet <T> set;

Stream <T> s = set.stream (); // sized , distinct ,

// ordered , sorted

Slide 21/48.

Page 35: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Stream Sources: collections

ArrayList <T> list;

Stream <T> s = list.stream (); // sized , ordered

HashSet <T> set;

Stream <T> s = set.stream (); // sized , distinct

TreeSet <T> set;

Stream <T> s = set.stream (); // sized , distinct ,

// ordered , sorted

Slide 21/48.

Page 36: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Stream Sources: collections

ArrayList <T> list;

Stream <T> s = list.stream (); // sized , ordered

HashSet <T> set;

Stream <T> s = set.stream (); // sized , distinct

TreeSet <T> set;

Stream <T> s = set.stream (); // sized , distinct ,

// ordered , sorted

Slide 21/48.

Page 37: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Stream Sources: factories, builders

T[] arr;

Stream <T> s = Arrays.stream(arr);

Stream <T> s = Stream.of(v0, v1, v2);

Stream <T> s = Stream.builder ().add(v0).add(v1).build ();

IntStream s = IntStream.range(0, 100);

Slide 22/48.

Page 38: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Stream Sources: factories, builders

T[] arr;

Stream <T> s = Arrays.stream(arr);

Stream <T> s = Stream.of(v0, v1, v2);

Stream <T> s = Stream.builder ().add(v0).add(v1).build ();

IntStream s = IntStream.range(0, 100);

Slide 22/48.

Page 39: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Stream Sources: factories, builders

T[] arr;

Stream <T> s = Arrays.stream(arr);

Stream <T> s = Stream.of(v0, v1, v2);

Stream <T> s = Stream.builder ().add(v0).add(v1).build ();

IntStream s = IntStream.range(0, 100);

Slide 22/48.

Page 40: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Stream Sources: factories, builders

T[] arr;

Stream <T> s = Arrays.stream(arr);

Stream <T> s = Stream.of(v0, v1, v2);

Stream <T> s = Stream.builder ().add(v0).add(v1).build ();

IntStream s = IntStream.range(0, 100);

Slide 22/48.

Page 41: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Stream Sources: generators

AtomicInteger count = new AtomicInteger (0);

Stream <Integer > s =

Stream.generate(count:: incrementAndGet );

IntStream s = Stream.iterate(0, i -> i+1);

Slide 23/48.

Page 42: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Stream Sources: generators

AtomicInteger count = new AtomicInteger (0);

Stream <Integer > s =

Stream.generate(count:: incrementAndGet );

IntStream s = Stream.iterate(0, i -> i+1);

Slide 23/48.

Page 43: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Stream Sources: others

Stream <String > s = bufferedReader.lines ();

Stream <String > s = Pattern.compile(myRegEx)

.splitAsStream(myStr);

DoubleStream s = new SplittableRandom (). doubles ();

Slide 24/48.

Page 44: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Stream Sources: others

Stream <String > s = bufferedReader.lines ();

Stream <String > s = Pattern.compile(myRegEx)

.splitAsStream(myStr);

DoubleStream s = new SplittableRandom (). doubles ();

Slide 24/48.

Page 45: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Stream Sources: others

Stream <String > s = bufferedReader.lines ();

Stream <String > s = Pattern.compile(myRegEx)

.splitAsStream(myStr);

DoubleStream s = new SplittableRandom (). doubles ();

Slide 24/48.

Page 46: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Intermediate Operations

Slide 25/48.

Page 47: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Intermediate Operations

Stream <S> s;

Stream <S> s.filter(Predicate <S>);

Stream <T> s.map(Function <S, T>);

Stream <T> s.flatMap(Function <S, Stream <T>>);

Stream <S> s.peek(Consumer <S>);

Stream <S> s.sorted ();

Stream <S> s.distinct ();

Stream <S> s.unordered ();

Stream <S> s.limit(long);

Stream <S> s.substream(long);

Stream <S> s.substream(long , long);

Stream <S> s.parallel ();

Stream <S> s.sequential ();

Slide 26/48.

Page 48: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Intermediate Operations

Stream <S> s;

Stream <S> s.filter(Predicate <S>);

Stream <T> s.map(Function <S, T>);

Stream <T> s.flatMap(Function <S, Stream <T>>);

Stream <S> s.peek(Consumer <S>);

Stream <S> s.sorted ();

Stream <S> s.distinct ();

Stream <S> s.unordered ();

Stream <S> s.limit(long);

Stream <S> s.substream(long);

Stream <S> s.substream(long , long);

Stream <S> s.parallel ();

Stream <S> s.sequential ();

Slide 26/48.

Page 49: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Terminal Operations a.k.a. PROFIT

Slide 27/48.

Page 50: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Terminal Operations

Terminal operations yield final result

Parallel or sequential execution

Terminal operations ’flavors’:iteration: forEach, iteratorsearching: findFirst, findAnymatching: allMatch, anyMatch, noneMatchaggregation:

𝑟𝑒𝑑𝑢𝑐𝑒𝑟𝑠𝑐𝑜𝑙𝑙𝑒𝑐𝑡𝑜𝑟𝑠

Slide 28/48.

Page 51: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Short-circuting

Do not consume the entire stream, drop it on the floor asnecessary

May operate infinite streams

e.g.: find*, *Match

int v = Stream.iterate(1, i -> i+1)

.filter( i % 2 == 0)

.findFirst (). get();

Slide 29/48.

Page 52: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Iteration

Process each stream element:IntStream.range(0, 100)

.forEach(System.out::println);

Convert to old style iterator1:Iterator<Integer> =

IntStream.range(0, 100).iterator();

1for compatibilitySlide 30/48.

Page 53: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Example

How to get sum of Stream<Integer>?

public int getSum(Stream <Integer > s){

int sum;

s.forEach( i -> sum += i );

return sum;

}

Slide 31/48.

Page 54: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Example

How to get sum of Stream<Integer>?

public int getSum(Stream <Integer > s){

int sum;

s.forEach( i -> sum += i );

return sum;

}

Slide 31/48.

Page 55: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Example

How to get sum of Stream<Integer>?

public int getSum(Stream <Integer > s){

int sum;

s.forEach( i -> sum += i ); // Compile error

return sum;

}

Slide 32/48.

Page 56: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Example

How to get sum of Stream<Integer>?

public int getSum(Stream <Integer > s){

int[] sum = new int [1];

s.forEach( i -> sum[0] += i );

return sum [0];

}

Slide 33/48.

Page 57: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Example

Result?

getSum(IntStream.range(0, 100). map(i -> 1))

100

getSum(IntStream.range(0, 100). map(i -> 1). parallel ())

79, 63, 100, ...

Slide 34/48.

Page 58: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Example

Result?

getSum(IntStream.range(0, 100). map(i -> 1))

100

getSum(IntStream.range(0, 100). map(i -> 1). parallel ())

79, 63, 100, ...

Slide 34/48.

Page 59: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Example

Result?

getSum(IntStream.range(0, 100). map(i -> 1))

100

getSum(IntStream.range(0, 100). map(i -> 1). parallel ())

79, 63, 100, ...

Slide 34/48.

Page 60: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Example

Result?

getSum(IntStream.range(0, 100). map(i -> 1))

100

getSum(IntStream.range(0, 100). map(i -> 1). parallel ())

79, 63, 100, ...

Slide 34/48.

Page 61: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Reduction

Take a stream and make a scalar value:int s = stream.reduce(0, (x, y) -> x + y);

Some operations return Optional<T>:Optional<Integer> o =

stream.reduce((x, y) -> x + y);

Integer i = stream.reduce(0, (x, y) -> x + y);

Slide 35/48.

Page 62: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Reduction

Take a stream and make a scalar value:int s = stream.reduce(0, (x, y) -> x + y);

Some operations return Optional<T>:Optional<Integer> o =

stream.reduce((x, y) -> x + y);

Integer i = stream.reduce(0, (x, y) -> x + y);

Slide 35/48.

Page 63: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Reduction

Stream <T> {

...

<U> U reduce(U identity ,

BiFunction <U,T,U> accumulator ,

BinaryOperator <U> combiner)

...

}

Slide 36/48.

Page 64: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Collectors

A.k.a. mutable reduction operationsAccumulate elements into a mutable result container:

List <Integer > list = IntStream.range(0, 100)

.boxed ()

.collect(Collectors.toList ());

int[] ints = IntStream.range(0, 100). toArray ();

Complex collections:

Map <Integer ,Integer > map = IntStream.range(0, 1000)

.boxed (). collect(

Collectors.toConcurrentMap(

k -> k % 42, v -> v, (a, b) -> b

)

);

Slide 37/48.

Page 65: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Collectors

A.k.a. mutable reduction operationsAccumulate elements into a mutable result container:

List <Integer > list = IntStream.range(0, 100)

.boxed ()

.collect(Collectors.toList ());

int[] ints = IntStream.range(0, 100). toArray ();

Complex collections:

Map <Integer ,Integer > map = IntStream.range(0, 1000)

.boxed (). collect(

Collectors.toConcurrentMap(

k -> k % 42, v -> v, (a, b) -> b

)

);Slide 37/48.

Page 66: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Collectors

String [] a = new String []{"a", "b", "c"};

How to get "a, b, c" ?

Arrays.stream(a). collect(Collectors.joining(",␣"));

FYI: java.util.StringJoiner

Slide 38/48.

Page 67: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Collectors

String [] a = new String []{"a", "b", "c"};

How to get "a, b, c" ?

Arrays.stream(a). collect(Collectors.joining(",␣"));

FYI: java.util.StringJoiner

Slide 38/48.

Page 68: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Collectors

String [] a = new String []{"a", "b", "c"};

How to get "a, b, c" ?

Arrays.stream(a). collect(Collectors.joining(",␣"));

FYI: java.util.StringJoiner

Slide 38/48.

Page 69: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Collectors

Stream <T> {

...

<R> R collect(Supplier <R> supplier ,

BiConsumer <R,T> accumulator ,

BiConsumer <R,R> combiner)

...

}

Slide 39/48.

Page 70: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Collectors

Stream <T> s;

List <T> l = s.collect(Collectors.toList ());

l = collect( () -> new ArrayList <>(),

(list , t) -> list.add(t),

(l1 , l2) -> l1.addAll(l2));

Slide 40/48.

Page 71: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Parallelism

Slide 41/48.

Page 72: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Parallelism

Lots of sources are naturally splittable

Lots of operations are well parallelizable

Streams will do it for us

«ForkJoinPool inside»

Have to ask for the parallelism explicitly

int v = list.parallelStream ()

.reduce(Math::max)

.get();

Slide 42/48.

Page 73: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Explicit parallelism

Q: Why not implicit?A: Final speedup depends on:

𝑁 – number of source elements

𝑄 – cost of operation

𝑃 – available HW parallelism

𝐶 – number of concurrent clients

We know 𝑁 .We can estimate 𝑃 .

We can somehow cope with 𝐶.𝑄 is almost not predictable.

Slide 43/48.

Page 74: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Splitterator

Slide 44/48.

Page 75: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Splitterator

interface Splitterator <T> {

...

long estimateSize ();

boolean tryAdvance(Consumer <T> action );

Spliterator <T> trySplit ();

...

}

Slide 45/48.

Page 76: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Useful links

JDK8: http://openjdk.java.net/projects/jdk8/

Binary builds: https://jdk8.java.net/download.html

Mailing list: [email protected]

Slide 46/48.

Page 77: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Thanks!

Slide 47/48.

Page 78: JDK 8: Stream StyleThe following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract

Q & A ?

Slide 48/48.