working through streamsbangalorejug.org/wp-content/uploads/2017/07/working-through-strea… ·...

26

Upload: others

Post on 22-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Working through Streamsbangalorejug.org/wp-content/uploads/2017/07/Working-through-Strea… · Streams Under-hood - Source • Source of the stream is a Spliterator • Spliterator
Page 2: Working through Streamsbangalorejug.org/wp-content/uploads/2017/07/Working-through-Strea… · Streams Under-hood - Source • Source of the stream is a Spliterator • Spliterator

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Working through Streams Implementation, Debugging

and many more …

Vaibhav Choudhary (@vaibhav_c) Java Platforms Team https://blogs.oracle.com/vaibhav

Page 3: Working through Streamsbangalorejug.org/wp-content/uploads/2017/07/Working-through-Strea… · Streams Under-hood - Source • Source of the stream is a Spliterator • Spliterator

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Safe Harbor Statement

The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

3

Page 4: Working through Streamsbangalorejug.org/wp-content/uploads/2017/07/Working-through-Strea… · Streams Under-hood - Source • Source of the stream is a Spliterator • Spliterator

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Agenda of the day …

Introduction

Basics of Streams and ParallelStreams

Small talk on internals (From prev. Talks)

Debugging Streams API

QA

1

2

3

4

5

4

Page 5: Working through Streamsbangalorejug.org/wp-content/uploads/2017/07/Working-through-Strea… · Streams Under-hood - Source • Source of the stream is a Spliterator • Spliterator

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Basics of Streams and ParallelStreams

5

Page 6: Working through Streamsbangalorejug.org/wp-content/uploads/2017/07/Working-through-Strea… · Streams Under-hood - Source • Source of the stream is a Spliterator • Spliterator

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Introduction• Java 8 came with the biggest change ever in terms of Streams and

Lambda. • Advocates Functional Programming • Focus on “what to do” rather than “how to do” • Can leverage the beauty of cores with ParallelStreams. • IDE’s are supporting it and started providing enough information

around it. • Some IDE’s like (eclipse) support to convert existing code into

Lambda code.

6

Page 8: Working through Streamsbangalorejug.org/wp-content/uploads/2017/07/Working-through-Strea… · Streams Under-hood - Source • Source of the stream is a Spliterator • Spliterator

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Small talk on internals (From prev. Talks)

8

Page 9: Working through Streamsbangalorejug.org/wp-content/uploads/2017/07/Working-through-Strea… · Streams Under-hood - Source • Source of the stream is a Spliterator • Spliterator

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Understanding Streams (From prev. Talks)• Streams exploits the most powerful computation principal -

Composition • Before entering into the laziness of Streams, first understand the

basic API’s and it’s internals. • All streams share a common structure – A stream source • Intermediate operations – Single terminal operation

9

Page 10: Working through Streamsbangalorejug.org/wp-content/uploads/2017/07/Working-through-Strea… · Streams Under-hood - Source • Source of the stream is a Spliterator • Spliterator

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Streams Under-hood - Source • Source of the stream is a Spliterator • Spliterator = Iterator + Split (if possible) • Not extended from Iterator. Iterator as in defensive and

duplicative. • hasNext() and next() - count is problem.

10

boolean tryAdvance(Consumer<? super T> action); // single element op void forEachRemaining(Consumer<? super T> action); // bulk op

Using Lambdas, Spliterator has 2 methods to access element :-

Page 11: Working through Streamsbangalorejug.org/wp-content/uploads/2017/07/Working-through-Strea… · Streams Under-hood - Source • Source of the stream is a Spliterator • Spliterator

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Executing a Stream pipeline - Intermediate/Terminal operations

• When terminal operation is initiated, the stream picks the execution plan. • Intermediate operations :- – stateless : filter(), map() ..

– stateful : sort(), limit(), distinct()

– If stateless operation, it can compute in single pass.

– If stateful, pipelines are divided into sections and computer in multiple pass.

• Terminal Operations :- – short-circuiting : allMatch(), findFirst() [ tryAdvance() ] – non-short-circuiting : reduce(), collect(), forEach() [ forEachRemaining() ]

11

Page 12: Working through Streamsbangalorejug.org/wp-content/uploads/2017/07/Working-through-Strea… · Streams Under-hood - Source • Source of the stream is a Spliterator • Spliterator

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Debugging Streams API

12

Page 13: Working through Streamsbangalorejug.org/wp-content/uploads/2017/07/Working-through-Strea… · Streams Under-hood - Source • Source of the stream is a Spliterator • Spliterator

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Streams simplified …• List of element -> find c letter guys -> change to upper case -> sort it -> print it

13

List<String> myList = Arrays.asList("apple", "mango", "orange", "cabbage", "capsicum"); myList .stream() .filter(s -> s.startsWith("c")) .map(String::toUpperCase) .sorted() .forEach(System.out::println);

Page 14: Working through Streamsbangalorejug.org/wp-content/uploads/2017/07/Working-through-Strea… · Streams Under-hood - Source • Source of the stream is a Spliterator • Spliterator

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Let’s peek it…• Need to know the floating values in the pipe

14

List<String> myList = Arrays.asList("apple", "mango", "orange", "cabbage", "capsicum"); myList .stream() .filter(s -> s.startsWith(“c")) .peek(s -> System.out.println(s)) .map(String::toUpperCase) .sorted() .forEach(System.out::println);

Definition for peek: Stream<T> peek(Consumer<? super T> action) JavaDoc specify that we should use peek for debugging purpose.

Page 15: Working through Streamsbangalorejug.org/wp-content/uploads/2017/07/Working-through-Strea… · Streams Under-hood - Source • Source of the stream is a Spliterator • Spliterator

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Let’s peek it…• Need to know the floating values in the parallel pipe

15

List<String> myList = Arrays.asList("apple", "mango", "orange", "cabbage", "capsicum"); myList .stream().parallel() .filter(s -> s.startsWith(“c")) .peek(s -> System.out.println(Thread.currentThread())) .map(String::toUpperCase) .sorted() .forEach(System.out::println);

Parallel stream - You can clearly see how many threads has been created for you by FJ Framework

Page 16: Working through Streamsbangalorejug.org/wp-content/uploads/2017/07/Working-through-Strea… · Streams Under-hood - Source • Source of the stream is a Spliterator • Spliterator

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Intellij Stream Debugger View - A plugin

16

Stream.of("Apple","Mango","Banana","Capsicum", "Cabbage", "Cauliflower") .filter(s -> s.startsWith("C")) .map(s -> s.toLowerCase()) .sorted() .forEach(s -> System.out.println(s));

Page 17: Working through Streamsbangalorejug.org/wp-content/uploads/2017/07/Working-through-Strea… · Streams Under-hood - Source • Source of the stream is a Spliterator • Spliterator

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Common Mistakes while using Streams

17

Page 18: Working through Streamsbangalorejug.org/wp-content/uploads/2017/07/Working-through-Strea… · Streams Under-hood - Source • Source of the stream is a Spliterator • Spliterator

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Example 1

18

IntStream stream = IntStream.of(1, 2); stream.forEach(System.out::println);

stream.forEach(System.out::println);

Guess the output ?

Page 19: Working through Streamsbangalorejug.org/wp-content/uploads/2017/07/Working-through-Strea… · Streams Under-hood - Source • Source of the stream is a Spliterator • Spliterator

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Example 2

19

IntStream.iterate(0, i -> ( i + 1 ) % 2) .distinct() .limit(10) .forEach(System.out::println);

Guess the output ?

Page 20: Working through Streamsbangalorejug.org/wp-content/uploads/2017/07/Working-through-Strea… · Streams Under-hood - Source • Source of the stream is a Spliterator • Spliterator

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Example 2

20

IntStream.iterate(0, i -> ( i + 1 ) % 2) .parallel() .distinct() .limit(10) .forEach(System.out::println);

Guess the output ?

Page 21: Working through Streamsbangalorejug.org/wp-content/uploads/2017/07/Working-through-Strea… · Streams Under-hood - Source • Source of the stream is a Spliterator • Spliterator

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Example 3

21

ArrayList<Integer> integers = new ArrayList<>(Arrays.asList(1,2,3));

for (Integer integer : integers) { integers.remove(2); }

Guess the output ?

Page 22: Working through Streamsbangalorejug.org/wp-content/uploads/2017/07/Working-through-Strea… · Streams Under-hood - Source • Source of the stream is a Spliterator • Spliterator

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Example 4

22

final List<Integer> ints = new ArrayList<>(); ints.add(1); ints.add(2); ints.add(3); ints.add(4); ints.stream() .map(a -> addSomething( a, ints )) .collect(Collectors.toList());

}

private static int addSomething(int current, List<Integer> ints) { ints.add(1); return current; }

Guess the output ?

Page 23: Working through Streamsbangalorejug.org/wp-content/uploads/2017/07/Working-through-Strea… · Streams Under-hood - Source • Source of the stream is a Spliterator • Spliterator

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Example 5

23

IntStream.iterate(0, i -> i + 1) .limit(10) // LIMIT .skip(5) // OFFSET .forEach(System.out::println);

Guess the output ?

Page 24: Working through Streamsbangalorejug.org/wp-content/uploads/2017/07/Working-through-Strea… · Streams Under-hood - Source • Source of the stream is a Spliterator • Spliterator

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Example 6

24

IntStream.range(1, 5) .peek(System.out::println) .peek(i -> { if (i == 5) throw new RuntimeException("bang"); });

Guess the output ?

Page 25: Working through Streamsbangalorejug.org/wp-content/uploads/2017/07/Working-through-Strea… · Streams Under-hood - Source • Source of the stream is a Spliterator • Spliterator

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Example 7

25

IntStream.of(1, 2, 3, 4).forEach( element -> System.out.println(element + 1));

Guess the output ?

Page 26: Working through Streamsbangalorejug.org/wp-content/uploads/2017/07/Working-through-Strea… · Streams Under-hood - Source • Source of the stream is a Spliterator • Spliterator

Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |

Must visit References• Java Stream Debugger plugin - https://plugins.jetbrains.com/

plugin/9696-java-stream-debugger • Top 10 Streams Mistakes - https://blog.jooq.org/2014/06/13/

java-8-friday-10-subtle-mistakes-when-using-the-streams-api/ • An inside view of Streams - http://bangalorejug.org/wp-content/

uploads/2017/06/An-inside-view-of-Streams-And-Lazy-Evaluation.pdf • Streams vs ParallelStreams - http://files.meetup.com/3189882/

streamsvsparallelstreams.pdf

26