SlideShare ist ein Scribd-Unternehmen logo
1 von 65
1
Hot Streaming Java
Nick Maiorano
ThoughtFlow Solutions Inc.
Java
Consultant
Author
Trainer
2Hot Streaming Java
About me
3Hot Streaming Java
About me
Goals
4
• Understand the streams API
• Able to use them in everyday Java
• Know when to hold ‘em/fold ‘em
Hot Streaming Java
Objectives
5
• Review the functional paradigm
• Present stream operations
• Examine stream-based algorithms
• Compare imperative algorithms vs.
serial & parallel streams
Hot Streaming Java
Part I
Introduction
6Hot Streaming Java
Streams
7
• Introduced as part of Java 8
• Minimal syntactical changes in Java 8
• Streams are as functional as Java gets
Hot Streaming Java
What’s a stream?
8
“A declarative construct used to express an
algorithm as a series of operations working
on a stream of data”
Hot Streaming Java
9Hot Streaming Java
What’s a stream?
10Hot Streaming Java
What’s a stream?
Streams
11Hot Streaming Java
• Streams are functional constructs:
• Functions as first-class citizens
• Emphasis on immutability/
statelessness
• Avoidance of side-effects
• Declarative programming
Streams
12Hot Streaming Java
• Streams use:
• Lambdas
• Functional interfaces
• Method references
Lambda refresh
13Hot Streaming Java
Lambda expressions
Parameter name -> Lambda expression
i -> System.out.println(i);
i -> i * 2;
Function <T, R>
R apply(T t);
Supplier<T>
T get()
Functional Interfaces
14Hot Streaming Java
Functional
Interfaces
Consumer
Function
Predicate
Supplier
Consumer<T>
void accept(T t);
Predicate<T>
boolean test(T t);
Functional Interfaces
15Hot Streaming Java
BiFunction<Integer, Integer, Integer>
multiply = (x, y) -> x * y;
IntBinaryOperator multiply2 = (x, y) -> x * y;
IntBinaryOperator multiply3 = Math::multiplyExact;
int product = multiply.apply(10, 20);
int product2 = multiply2.applyAsInt(10, 20);
int product3 = multiply3.applyAsInt(10, 20);
16Hot Streaming Java
Streams
Lambda Lambda Lambda Lambda
Operation
1
Operation
2
Operation
3
Operation
4
• Operations fused into pipelines
• Customized with lambdas
• Abstract for/while loop
• Fluent-style programming
17Hot Streaming Java
Streams
List<Integer> integersIn =
Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Integer> evenIntegers = new ArrayList<>();
integersIn.stream().
filter(x -> x % 2 == 0).
forEach(evenIntegers::add);
parallel().
18Hot Streaming Java
Constructing streams
A coherent stream
1 build + 0..n intermediate + 1 terminal
19Hot Streaming Java
Constructing streams
Good stream
list.stream().filter(x-> x ^ 2 == 0).reduce((l, r) -> l + r);
Build operation Intermediate operation Terminal operation
20Hot Streaming Java
Constructing streams
Bad stream!
list.stream().filter(x-> x ^ 2 == 0);
No terminal operation
21Hot Streaming Java
Constructing streams
Badder stream!!
Stream.iterate(0, i -> ++i).anyMatch(i -> i < 0);
Infinite stream
22Hot Streaming Java
Constructing streams
Baddest stream!!!
List<String> words = new ArrayList<>(
Arrays.asList(“This”, “sentence”, “contains”, “five”, “words”));
// This stream has interference – avoid!
words.stream().
forEach(s -> {if (s.equals(“five”)) words.add(“thousand”);});
Lambda interferes with stream, has side-
effects and is not thread-safe
23Hot Streaming Java
Constructing streams
Generic vs. specialized
//Generic
Stream.iterate(1, i -> ++i).
limit(10).reduce((l, r) -> l + r);
//Specialized
IntStream.rangeClosed(1, 10).sum();
Part II
Diving into Streams
24Hot Streaming Java
Stream operations
25Hot Streaming Java
Stream
operations
Build
Filter
Map
Reduce
Iterate
Peek
• Create the stream
• Collections:
List.stream()
• Buffered readers:
BufferedReader.lines()
• Or thin-air:
IntStream.range(1, 10)
Stream operations
26Hot Streaming Java
Stream
operations
Build
Filter
Map
Reduce
Iterate
Peek
• Allows or blocks data
• If-statements
• True:
• let the stream element thru
• False:
• block the element
Stream operations
27Hot Streaming Java
Stream
operations
Build
Filter
Map
Reduce
Iterate
Peek
• Transforms stream data
• Same or different type
• map, mapToInt, mapToLong
• Same or different cardinality
• flatMap, flatMapToInt, flatMapToLong
Stream operations
28Hot Streaming Java
Stream
operations
Build
Filter
Map
Reduce
Iterate
Peek
• Boils down to one thing
• Ordinary reduction:
(((n1 + n2) + n3) + n4) + n5
(((1 + 2) + 3) + 4) + 5
((3 + 3) + 4) + 5
(6 + 4) + 5
10 + 5
15
• Mutable reduction:
• Mutate an object while collecting
Left fold
29Hot Streaming Java
Implement the Linux grep command
using streams to count string matches
30Hot Streaming Java
Stream grep –c
Imperative algorithm
private static long grepDashCImperative(BufferedReader in,
String upperCaseSearchWord) {
String nextLine = in.readLine();
int count = 0;
while (nextLine != null) {
String upperCaseLine = nextLine.toUpperCase();
if (upperCaseLine.contains(upperCaseSearchWord)) {
count++;
}
nextLine = in.readLine();
}
return count;
}
Imperative
style
31Hot Streaming Java
Stream grep –c
Using forEach
private static long grepDashC(BufferedReader in,
String upperCaseSearchWord) {
int count = 0;
in.lines().
map(String::toUpperCase).
filter(s -> s.contains(upperCaseSearchWord)).
forEach(next -> count++);
return count;
}
Stream style
32Hot Streaming Java
Stream grep –c
Using forEach
private static long grepDashC(BufferedReader in,
String upperCaseSearchWord) {
int count = 0;
in.lines().
map(String::toUpperCase).
filter(s -> s.contains(upperCaseSearchWord)).
forEach(next -> count++);
return count;
}
Cannot mutate a local variable in a lambda
33Hot Streaming Java
Stream grep –c
Using reduce
private static long grepDashC(BufferedReader in,
String upperCaseSearchWord) {
// The long way
return in.lines().
map(String::toUpperCase).
filter(s -> s.contains(upperCaseSearchWord)).
mapToLong(count -> 1).
reduce(0, (l, r) -> l + r);
}
Using the
reduce
operation
34Hot Streaming Java
Stream grep –c
Using count()
private static long grepDashC(BufferedReader in,
String upperCaseSearchWord) {
// Using the built-in function count
return in.lines().
map(String::toUpperCase).
filter(s -> s.contains(upperCaseSearchWord)).
count();
}
Using the
count
operation
35Hot Streaming Java
Implement the Linux grep command
using streams to return string matches
36Hot Streaming Java
Stream grep
Foreach-based accumulation
private static String grep(BufferedReader in,
String upperCaseSearchWord) {
StringBuilder accumulator = new StringBuilder();
// Accumulate the strings via foreach
in.lines().
map(String::toUpperCase).
filter(s -> s.contains(upperCaseSearchWord)).
forEach(accumulator::append);
return accumulator.toString();
}
forEach is not a reduction tool
37Hot Streaming Java
Stream grep
Reduction-based accumulation
private static String grep(BufferedReader in,
String upperCaseSearchWord) {
// Accumulate the strings via reduction
return in.lines().
map(String::toUpperCase).
filter(s -> s.contains(upperCaseSearchWord)).
reduce("", (l, r) -> l.concat(r).concat(", "));
}
Reduce is not the right tool for the job:
String is being copied every time
38Hot Streaming Java
Stream grep
Mutable reduction
private static List<String> grep(BufferedReader in,
String upperCaseSearchWord){
// Accumulate the strings via collect
return in.lines().
map(String::toUpperCase).
filter(s -> s.contains(upperCaseSearchWord)).
collect(ArrayList<String>::new,
ArrayList<String>::add,
ArrayList<String>::addAll);
}
Collect is a mutable reduction operation
Collect
Supplier
(Supplier)
Creates the
mutable structure
Accumulator
(BiConsumer)
Adds to the
structure
Combiner
(BiConsumer)
Merges each
partial
accumulation
ArrayList<String>::add
ArrayList<String>::addAll
ArrayList<String>::new
40Hot Streaming Java
Stream grep
Mutable reduction
private static List<String> grep(BufferedReader in,
String upperCaseSearchWord){
// Accumulate the strings via collect
return in.lines().
map(String::toUpperCase).
filter(s -> s.contains(upperCaseSearchWord)).
collect(ArrayList<String>::new,
ArrayList<String>::add,
ArrayList<String>::addAll);
}
Scary syntax can be replaced by…
41Hot Streaming Java
Stream grep
Mutable reduction
private static List<String> grep(BufferedReader in,
String upperCaseSearchWord){
// Accumulate the strings via collect with collectors
return in.lines().
map(String::toUpperCase).
filter(s -> s.contains(upperCaseSearchWord)).
collect(Collectors.toList());
}
Declarative collection
Rolling your own Collector
42Hot Streaming Java
• Implement Collector interface
• Supplier, Accumulator, Combiner, Finisher
• Set characteristics
• Concurrent: accumulation can be concurrent
• Unordered: Collection is not ordered
• Identity finish: Turn on/off finishing
43Hot Streaming Java
Stream grep
Parallel streaming
private static List<String> grep(BufferedReader in,
String upperCaseSearchWord){
// Accumulate the strings via collect with collectors
return in.lines().
map(String::toUpperCase).parallel().
filter(s -> s.contains(upperCaseSearchWord)).
collect(Collectors.toList());
}
44Hot Streaming Java
Serial streaming
45Hot Streaming Java
Parallel streaming
6
5
1
8
2
9
7
4
3
9
7
4
3
8
2
6
5
1
Part III
Fluid Streams
46Hot Streaming Java
Stream thinking
47
• Thinking in streams can be difficult
• Steep learning curve
• Lambdas
• Method references
• Standard functional interfaces
• Generics
• Requires a functional mindset
• Look at algorithms differently
• Not all algorithms translate to streams
Hot Streaming Java
Stream thinking
Streams are best
suited for algorithms
that… Traverse a sequence
of data
Reduces
sequence
to a thing
No read-ahead
no read-behind
No state change
during traversal
Stream thinking
49
• When algorithms don’t fit:
• Re-think the algorithm functionally
• Use stream operations to maintain state
• Or don’t use streams!
Hot Streaming Java
Algorithm suitability
Grep
Quick sort
Conway’s game
of life
51Hot Streaming Java
Compare imperative quick sort
vs.
stream quick sort
52Hot Streaming Java
public static ArrayList<Integer> imperativeQuickSort(ArrayList<Integer> array, int low, int n) {
int lo = low;
int hi = n;
if (lo >= n) return array;
// Step 1: find pivot point
int mid = array.get((lo + hi) / 2);
// Step 2: find & swap values less & greater than pivot
while (lo < hi) {
while (lo < hi && array.get(lo) < mid) {
lo++;
}
while (lo < hi && array.get(hi) > mid) {
hi--;
}
if (lo < hi) {
// Swap values
int temp = array.get(lo);
array.set(lo, array.get(hi));
array.set(hi, temp);
lo++;
hi--;
}
}
if (hi < lo) lo = hi;
// Steps 3: split the array and repeat recursively
imperativeQuickSort(array, low, lo);
imperativeQuickSort(array, lo == low ? lo + 1 : lo, n);
Imperative
quick sort
public static List<Integer> functionalSort(List<Integer> array) {
List<Integer> returnArray = array;
if (array.size() > 1) {
// Step 1
int mid = array.get(array.size() / 2);
// Step 2
Map<Integer, List<Integer>> map = array.stream().parallel().
collect(Collectors.groupingBy(i -> i < mid ? 0 : i == mid ? 1 : 2));
// Step 3
List<Integer> left = functionalSort(map.getOrDefault(0, new ArrayList<>()));
List<Integer> middle = map.getOrDefault(1, new ArrayList<>());
List<Integer> right = functionalSort(map.getOrDefault(2, new ArrayList<>()));
left.addAll(middle);
left.addAll(right);
returnArray = left;
}
return returnArray;
}
53Hot Streaming Java
Functional
quick sort
54Hot Streaming Java
Compare imperative Conway’s Game Of Life
vs.
stream-based Game of life
55Hot Streaming Java
public static boolean[][] getNextGeneration(boolean[][] oldGeneration)
{
boolean[][] newGeneration = new boolean[oldGeneration.length][oldGeneration.length];
for (int y = 0; y < oldGeneration.length; ++y) {
for (int x = 0; x < oldGeneration.length; ++x) {
newGeneration[y][x] = isAlive(oldGeneration, y, x);
}
}
return newGeneration;
}
private static int countLiveNeighborCells(boolean[][] generation, int y, int x)
{
int count = 0;
for (int yIndex = y - 1; yIndex <= y + 1; ++yIndex) {
if (yIndex >= 0 && yIndex < generation.length) {
for (int xIndex = x - 1; xIndex <= x + 1; ++xIndex) {
if (xIndex >= 0 && xIndex < generation.length) {
if (generation[yIndex][xIndex] && !(xIndex == x && yIndex == y)) {
++count;
}
}
}
}
}
return count;
}
private static boolean isAlive(boolean[][] generation, int y, int x)
{
int liveCells = countLiveNeighborCells(generation, y, x);
return (generation[y][x] && liveCells >= 2 && liveCells <= 3) ||
(!generation[y][x] && liveCells == 3);
}
Imperative
56Hot Streaming Java
Conway’s Game of Life
public static boolean[][] getNextGeneration(boolean[][] oldGeneration)
{
boolean[][] newGeneration = new boolean[oldGeneration.length][oldGeneration.length];
for (int y = 0; y < oldGeneration.length; ++y) {
for (int x = 0; x < oldGeneration.length; ++x) {
newGeneration[y][x] = isAlive(oldGeneration, y, x);
}
}
return newGeneration;
}
private static int countLiveNeighborCells(boolean[][] generation, int y, int x)
{
int count = 0;
for (int yIndex = y - 1; yIndex <= y + 1; ++yIndex) {
if (yIndex >= 0 && yIndex < generation.length) {
for (int xIndex = x - 1; xIndex <= x + 1; ++xIndex) {
if (xIndex >= 0 && xIndex < generation.length) {
if (generation[yIndex][xIndex] && !(xIndex == x && yIndex == y)) {
++count;
}
}
}
}
}
return count;
}
private static boolean isAlive(boolean[][] generation, int y, int x)
{
int liveCells = countLiveNeighborCells(generation, y, x);
return (generation[y][x] && liveCells >= 2 && liveCells <= 3) ||
(!generation[y][x] && liveCells == 3);
}
public static boolean[][] getNextGenerationFunctional(boolean[][] oldGeneration)
{
boolean[][] newGeneration = new boolean[oldGeneration.length][oldGeneration.length];
List<Coordinates> list = new ArrayList<>();
IntStream.range(0, oldGeneration.length).
forEach(nextY ->
{
list.addAll(IntStream.range(0, oldGeneration.length).
parallel().
mapToObj(nextX -> new Coordinates(nextX, nextY)).
filter(coordinates -> isAlive(oldGeneration, coordinates.getY(),
coordinates.getX())).
collect(Collectors.toList()));
});
list.parallelStream().
forEach(nextCoordinate ->
newGeneration[nextCoordinate.getY()][nextCoordinate.getX()] = true);
return newGeneration;
}
private static int countLiveNeighborCells(boolean[][] generation, int y, int x)
{
return IntStream.rangeClosed(y - 1, y + 1).
filter(yInner -> yInner >= 0 && yInner < generation.length).
reduce(0, (yLeft, yRight) -> yLeft +
IntStream.rangeClosed(x - 1, x + 1).
filter(xInner -> xInner >= 0 && xInner < generation.length &&
generation[yRight][xInner] && !(xInner == x && yRight == y)).
reduce(0, (xLeft, xRight) -> xLeft + 1));
}
Stream-based
Takeaway
Grep & Quick Sort offered compelling
reasons to implement with Streams
Conway’s Game of Life did not
58Hot Streaming Java
Part IV
Parallel streams
Parallel streams
Parallel streams are
best suited for
algorithms that… Can split data easily
& quickly
Have no
order
constraint
Can reduce
concurrently
Have large
N and/or Q
Hot Streaming Java
60Hot Streaming Java
Parallel streaming
Remember: A parallel stream must do
everything a serial one does and more
61Hot Streaming Java
Parallel streaming
Sometimes, parallel streams are slower
than their serial counterparts…
And also their imperative counterparts
Part V
End-of-stream
62Hot Streaming Java
Streams recap
Hot Streaming Java
5
Think functionally when using streams
4
3
2
1
Use the right reduction tool
Break through the learning curve
Use parallel streaming intelligently
Adapt thinking for streams or don’t use ‘em
64Hot Streaming Java
Questions
65Hot Streaming Java

Weitere ähnliche Inhalte

Was ist angesagt?

Rxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJavaRxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJavaKros Huang
 
Reactive programming with RxJava
Reactive programming with RxJavaReactive programming with RxJava
Reactive programming with RxJavaJobaer Chowdhury
 
Functional Reactive Programming (FRP): Working with RxJS
Functional Reactive Programming (FRP): Working with RxJSFunctional Reactive Programming (FRP): Working with RxJS
Functional Reactive Programming (FRP): Working with RxJSOswald Campesato
 
RxJava applied [JavaDay Kyiv 2016]
RxJava applied [JavaDay Kyiv 2016]RxJava applied [JavaDay Kyiv 2016]
RxJava applied [JavaDay Kyiv 2016]Igor Lozynskyi
 
RxJava 2.0 介紹
RxJava 2.0 介紹RxJava 2.0 介紹
RxJava 2.0 介紹Kros Huang
 
Jug Marche: Meeting June 2014. Java 8 hands on
Jug Marche: Meeting June 2014. Java 8 hands onJug Marche: Meeting June 2014. Java 8 hands on
Jug Marche: Meeting June 2014. Java 8 hands onOnofrio Panzarino
 
Stress test data pipeline
Stress test data pipelineStress test data pipeline
Stress test data pipelineMarina Grechuhin
 
Intro to Java 8 Closures (Dainius Mezanskas)
Intro to Java 8 Closures (Dainius Mezanskas)Intro to Java 8 Closures (Dainius Mezanskas)
Intro to Java 8 Closures (Dainius Mezanskas)Kaunas Java User Group
 
Intro to RxJava/RxAndroid - GDG Munich Android
Intro to RxJava/RxAndroid - GDG Munich AndroidIntro to RxJava/RxAndroid - GDG Munich Android
Intro to RxJava/RxAndroid - GDG Munich AndroidEgor Andreevich
 
(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_iNico Ludwig
 
Practical RxJava for Android
Practical RxJava for AndroidPractical RxJava for Android
Practical RxJava for AndroidTomĂĄĹĄ Kypta
 
HBase RowKey design for Akka Persistence
HBase RowKey design for Akka PersistenceHBase RowKey design for Akka Persistence
HBase RowKey design for Akka PersistenceKonrad Malawski
 
Functional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event SystemsFunctional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event SystemsLeonardo Borges
 
Non Blocking I/O for Everyone with RxJava
Non Blocking I/O for Everyone with RxJavaNon Blocking I/O for Everyone with RxJava
Non Blocking I/O for Everyone with RxJavaFrank Lyaruu
 
Streaming all the things with akka streams
Streaming all the things with akka streams   Streaming all the things with akka streams
Streaming all the things with akka streams Johan AndrĂŠn
 
Reactive stream processing using Akka streams
Reactive stream processing using Akka streams Reactive stream processing using Akka streams
Reactive stream processing using Akka streams Johan AndrĂŠn
 
Java 8 Stream API and RxJava Comparison
Java 8 Stream API and RxJava ComparisonJava 8 Stream API and RxJava Comparison
Java 8 Stream API and RxJava ComparisonJosĂŠ Paumard
 

Was ist angesagt? (20)

Rxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJavaRxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJava
 
Reactive programming with RxJava
Reactive programming with RxJavaReactive programming with RxJava
Reactive programming with RxJava
 
Functional Reactive Programming (FRP): Working with RxJS
Functional Reactive Programming (FRP): Working with RxJSFunctional Reactive Programming (FRP): Working with RxJS
Functional Reactive Programming (FRP): Working with RxJS
 
RxJava Applied
RxJava AppliedRxJava Applied
RxJava Applied
 
RxJava applied [JavaDay Kyiv 2016]
RxJava applied [JavaDay Kyiv 2016]RxJava applied [JavaDay Kyiv 2016]
RxJava applied [JavaDay Kyiv 2016]
 
RxJava 2.0 介紹
RxJava 2.0 介紹RxJava 2.0 介紹
RxJava 2.0 介紹
 
Jug Marche: Meeting June 2014. Java 8 hands on
Jug Marche: Meeting June 2014. Java 8 hands onJug Marche: Meeting June 2014. Java 8 hands on
Jug Marche: Meeting June 2014. Java 8 hands on
 
Stress test data pipeline
Stress test data pipelineStress test data pipeline
Stress test data pipeline
 
Intro to Java 8 Closures (Dainius Mezanskas)
Intro to Java 8 Closures (Dainius Mezanskas)Intro to Java 8 Closures (Dainius Mezanskas)
Intro to Java 8 Closures (Dainius Mezanskas)
 
Intro to RxJava/RxAndroid - GDG Munich Android
Intro to RxJava/RxAndroid - GDG Munich AndroidIntro to RxJava/RxAndroid - GDG Munich Android
Intro to RxJava/RxAndroid - GDG Munich Android
 
(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i
 
Practical RxJava for Android
Practical RxJava for AndroidPractical RxJava for Android
Practical RxJava for Android
 
HBase RowKey design for Akka Persistence
HBase RowKey design for Akka PersistenceHBase RowKey design for Akka Persistence
HBase RowKey design for Akka Persistence
 
Meet scala
Meet scalaMeet scala
Meet scala
 
Functional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event SystemsFunctional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event Systems
 
Non Blocking I/O for Everyone with RxJava
Non Blocking I/O for Everyone with RxJavaNon Blocking I/O for Everyone with RxJava
Non Blocking I/O for Everyone with RxJava
 
Streaming all the things with akka streams
Streaming all the things with akka streams   Streaming all the things with akka streams
Streaming all the things with akka streams
 
Reactive stream processing using Akka streams
Reactive stream processing using Akka streams Reactive stream processing using Akka streams
Reactive stream processing using Akka streams
 
AWS Java SDK @ scale
AWS Java SDK @ scaleAWS Java SDK @ scale
AWS Java SDK @ scale
 
Java 8 Stream API and RxJava Comparison
Java 8 Stream API and RxJava ComparisonJava 8 Stream API and RxJava Comparison
Java 8 Stream API and RxJava Comparison
 

Ähnlich wie Hot Streaming Java

Java 8 Intro - Core Features
Java 8 Intro - Core FeaturesJava 8 Intro - Core Features
Java 8 Intro - Core FeaturesGlobalLogic Ukraine
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentationVan Huong
 
Lambda Functions in Java 8
Lambda Functions in Java 8Lambda Functions in Java 8
Lambda Functions in Java 8Ganesh Samarthyam
 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8Martin Toshev
 
What is new in java 8 concurrency
What is new in java 8 concurrencyWhat is new in java 8 concurrency
What is new in java 8 concurrencykshanth2101
 
Akka streams - Umeå java usergroup
Akka streams - Umeå java usergroupAkka streams - Umeå java usergroup
Akka streams - Umeå java usergroupJohan Andrén
 
Reactive streams processing using Akka Streams
Reactive streams processing using Akka StreamsReactive streams processing using Akka Streams
Reactive streams processing using Akka StreamsJohan AndrĂŠn
 
Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 WorkshopMario Fusco
 
Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 StreamsCharles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streamsjessitron
 
Introduction to java 8 stream api
Introduction to java 8 stream apiIntroduction to java 8 stream api
Introduction to java 8 stream apiVladislav sidlyarevich
 
Java8: what's new and what's hot
Java8: what's new and what's hotJava8: what's new and what's hot
Java8: what's new and what's hotSergii Maliarov
 
Wprowadzenie do technologii Big Data / Intro to Big Data Ecosystem
Wprowadzenie do technologii Big Data / Intro to Big Data EcosystemWprowadzenie do technologii Big Data / Intro to Big Data Ecosystem
Wprowadzenie do technologii Big Data / Intro to Big Data EcosystemSages
 
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)Harmeet Singh(Taara)
 
Xebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_finalXebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_finalUrs Peter
 
Java8.part2
Java8.part2Java8.part2
Java8.part2Ivan Ivanov
 
Lambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter LawreyLambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter LawreyJAXLondon_Conference
 
Real Time Big Data Management
Real Time Big Data ManagementReal Time Big Data Management
Real Time Big Data ManagementAlbert Bifet
 

Ähnlich wie Hot Streaming Java (20)

Java 8 Intro - Core Features
Java 8 Intro - Core FeaturesJava 8 Intro - Core Features
Java 8 Intro - Core Features
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
 
Lambda Functions in Java 8
Lambda Functions in Java 8Lambda Functions in Java 8
Lambda Functions in Java 8
 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8
 
What is new in java 8 concurrency
What is new in java 8 concurrencyWhat is new in java 8 concurrency
What is new in java 8 concurrency
 
Akka streams - Umeå java usergroup
Akka streams - Umeå java usergroupAkka streams - Umeå java usergroup
Akka streams - Umeå java usergroup
 
Reactive streams processing using Akka Streams
Reactive streams processing using Akka StreamsReactive streams processing using Akka Streams
Reactive streams processing using Akka Streams
 
Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
 
Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 StreamsCharles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streams
 
Introduction to java 8 stream api
Introduction to java 8 stream apiIntroduction to java 8 stream api
Introduction to java 8 stream api
 
What is new in Java 8
What is new in Java 8What is new in Java 8
What is new in Java 8
 
Java8
Java8Java8
Java8
 
Java8: what's new and what's hot
Java8: what's new and what's hotJava8: what's new and what's hot
Java8: what's new and what's hot
 
Wprowadzenie do technologii Big Data / Intro to Big Data Ecosystem
Wprowadzenie do technologii Big Data / Intro to Big Data EcosystemWprowadzenie do technologii Big Data / Intro to Big Data Ecosystem
Wprowadzenie do technologii Big Data / Intro to Big Data Ecosystem
 
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
 
Xebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_finalXebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_final
 
Java8.part2
Java8.part2Java8.part2
Java8.part2
 
Lambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter LawreyLambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter Lawrey
 
Real Time Big Data Management
Real Time Big Data ManagementReal Time Big Data Management
Real Time Big Data Management
 
Java 8
Java 8Java 8
Java 8
 

KĂźrzlich hochgeladen

Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfWilly Marroquin (WillyDevNET)
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 

KĂźrzlich hochgeladen (20)

Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 

Hot Streaming Java

  • 1. 1 Hot Streaming Java Nick Maiorano ThoughtFlow Solutions Inc.
  • 4. Goals 4 • Understand the streams API • Able to use them in everyday Java • Know when to hold ‘em/fold ‘em Hot Streaming Java
  • 5. Objectives 5 • Review the functional paradigm • Present stream operations • Examine stream-based algorithms • Compare imperative algorithms vs. serial & parallel streams Hot Streaming Java
  • 7. Streams 7 • Introduced as part of Java 8 • Minimal syntactical changes in Java 8 • Streams are as functional as Java gets Hot Streaming Java
  • 8. What’s a stream? 8 “A declarative construct used to express an algorithm as a series of operations working on a stream of data” Hot Streaming Java
  • 11. Streams 11Hot Streaming Java • Streams are functional constructs: • Functions as first-class citizens • Emphasis on immutability/ statelessness • Avoidance of side-effects • Declarative programming
  • 12. Streams 12Hot Streaming Java • Streams use: • Lambdas • Functional interfaces • Method references
  • 13. Lambda refresh 13Hot Streaming Java Lambda expressions Parameter name -> Lambda expression i -> System.out.println(i); i -> i * 2;
  • 14. Function <T, R> R apply(T t); Supplier<T> T get() Functional Interfaces 14Hot Streaming Java Functional Interfaces Consumer Function Predicate Supplier Consumer<T> void accept(T t); Predicate<T> boolean test(T t);
  • 15. Functional Interfaces 15Hot Streaming Java BiFunction<Integer, Integer, Integer> multiply = (x, y) -> x * y; IntBinaryOperator multiply2 = (x, y) -> x * y; IntBinaryOperator multiply3 = Math::multiplyExact; int product = multiply.apply(10, 20); int product2 = multiply2.applyAsInt(10, 20); int product3 = multiply3.applyAsInt(10, 20);
  • 16. 16Hot Streaming Java Streams Lambda Lambda Lambda Lambda Operation 1 Operation 2 Operation 3 Operation 4 • Operations fused into pipelines • Customized with lambdas • Abstract for/while loop • Fluent-style programming
  • 17. 17Hot Streaming Java Streams List<Integer> integersIn = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); List<Integer> evenIntegers = new ArrayList<>(); integersIn.stream(). filter(x -> x % 2 == 0). forEach(evenIntegers::add); parallel().
  • 18. 18Hot Streaming Java Constructing streams A coherent stream 1 build + 0..n intermediate + 1 terminal
  • 19. 19Hot Streaming Java Constructing streams Good stream list.stream().filter(x-> x ^ 2 == 0).reduce((l, r) -> l + r); Build operation Intermediate operation Terminal operation
  • 20. 20Hot Streaming Java Constructing streams Bad stream! list.stream().filter(x-> x ^ 2 == 0); No terminal operation
  • 21. 21Hot Streaming Java Constructing streams Badder stream!! Stream.iterate(0, i -> ++i).anyMatch(i -> i < 0); Infinite stream
  • 22. 22Hot Streaming Java Constructing streams Baddest stream!!! List<String> words = new ArrayList<>( Arrays.asList(“This”, “sentence”, “contains”, “five”, “words”)); // This stream has interference – avoid! words.stream(). forEach(s -> {if (s.equals(“five”)) words.add(“thousand”);}); Lambda interferes with stream, has side- effects and is not thread-safe
  • 23. 23Hot Streaming Java Constructing streams Generic vs. specialized //Generic Stream.iterate(1, i -> ++i). limit(10).reduce((l, r) -> l + r); //Specialized IntStream.rangeClosed(1, 10).sum();
  • 24. Part II Diving into Streams 24Hot Streaming Java
  • 25. Stream operations 25Hot Streaming Java Stream operations Build Filter Map Reduce Iterate Peek • Create the stream • Collections: List.stream() • Buffered readers: BufferedReader.lines() • Or thin-air: IntStream.range(1, 10)
  • 26. Stream operations 26Hot Streaming Java Stream operations Build Filter Map Reduce Iterate Peek • Allows or blocks data • If-statements • True: • let the stream element thru • False: • block the element
  • 27. Stream operations 27Hot Streaming Java Stream operations Build Filter Map Reduce Iterate Peek • Transforms stream data • Same or different type • map, mapToInt, mapToLong • Same or different cardinality • flatMap, flatMapToInt, flatMapToLong
  • 28. Stream operations 28Hot Streaming Java Stream operations Build Filter Map Reduce Iterate Peek • Boils down to one thing • Ordinary reduction: (((n1 + n2) + n3) + n4) + n5 (((1 + 2) + 3) + 4) + 5 ((3 + 3) + 4) + 5 (6 + 4) + 5 10 + 5 15 • Mutable reduction: • Mutate an object while collecting Left fold
  • 29. 29Hot Streaming Java Implement the Linux grep command using streams to count string matches
  • 30. 30Hot Streaming Java Stream grep –c Imperative algorithm private static long grepDashCImperative(BufferedReader in, String upperCaseSearchWord) { String nextLine = in.readLine(); int count = 0; while (nextLine != null) { String upperCaseLine = nextLine.toUpperCase(); if (upperCaseLine.contains(upperCaseSearchWord)) { count++; } nextLine = in.readLine(); } return count; } Imperative style
  • 31. 31Hot Streaming Java Stream grep –c Using forEach private static long grepDashC(BufferedReader in, String upperCaseSearchWord) { int count = 0; in.lines(). map(String::toUpperCase). filter(s -> s.contains(upperCaseSearchWord)). forEach(next -> count++); return count; } Stream style
  • 32. 32Hot Streaming Java Stream grep –c Using forEach private static long grepDashC(BufferedReader in, String upperCaseSearchWord) { int count = 0; in.lines(). map(String::toUpperCase). filter(s -> s.contains(upperCaseSearchWord)). forEach(next -> count++); return count; } Cannot mutate a local variable in a lambda
  • 33. 33Hot Streaming Java Stream grep –c Using reduce private static long grepDashC(BufferedReader in, String upperCaseSearchWord) { // The long way return in.lines(). map(String::toUpperCase). filter(s -> s.contains(upperCaseSearchWord)). mapToLong(count -> 1). reduce(0, (l, r) -> l + r); } Using the reduce operation
  • 34. 34Hot Streaming Java Stream grep –c Using count() private static long grepDashC(BufferedReader in, String upperCaseSearchWord) { // Using the built-in function count return in.lines(). map(String::toUpperCase). filter(s -> s.contains(upperCaseSearchWord)). count(); } Using the count operation
  • 35. 35Hot Streaming Java Implement the Linux grep command using streams to return string matches
  • 36. 36Hot Streaming Java Stream grep Foreach-based accumulation private static String grep(BufferedReader in, String upperCaseSearchWord) { StringBuilder accumulator = new StringBuilder(); // Accumulate the strings via foreach in.lines(). map(String::toUpperCase). filter(s -> s.contains(upperCaseSearchWord)). forEach(accumulator::append); return accumulator.toString(); } forEach is not a reduction tool
  • 37. 37Hot Streaming Java Stream grep Reduction-based accumulation private static String grep(BufferedReader in, String upperCaseSearchWord) { // Accumulate the strings via reduction return in.lines(). map(String::toUpperCase). filter(s -> s.contains(upperCaseSearchWord)). reduce("", (l, r) -> l.concat(r).concat(", ")); } Reduce is not the right tool for the job: String is being copied every time
  • 38. 38Hot Streaming Java Stream grep Mutable reduction private static List<String> grep(BufferedReader in, String upperCaseSearchWord){ // Accumulate the strings via collect return in.lines(). map(String::toUpperCase). filter(s -> s.contains(upperCaseSearchWord)). collect(ArrayList<String>::new, ArrayList<String>::add, ArrayList<String>::addAll); } Collect is a mutable reduction operation
  • 39. Collect Supplier (Supplier) Creates the mutable structure Accumulator (BiConsumer) Adds to the structure Combiner (BiConsumer) Merges each partial accumulation ArrayList<String>::add ArrayList<String>::addAll ArrayList<String>::new
  • 40. 40Hot Streaming Java Stream grep Mutable reduction private static List<String> grep(BufferedReader in, String upperCaseSearchWord){ // Accumulate the strings via collect return in.lines(). map(String::toUpperCase). filter(s -> s.contains(upperCaseSearchWord)). collect(ArrayList<String>::new, ArrayList<String>::add, ArrayList<String>::addAll); } Scary syntax can be replaced by…
  • 41. 41Hot Streaming Java Stream grep Mutable reduction private static List<String> grep(BufferedReader in, String upperCaseSearchWord){ // Accumulate the strings via collect with collectors return in.lines(). map(String::toUpperCase). filter(s -> s.contains(upperCaseSearchWord)). collect(Collectors.toList()); } Declarative collection
  • 42. Rolling your own Collector 42Hot Streaming Java • Implement Collector interface • Supplier, Accumulator, Combiner, Finisher • Set characteristics • Concurrent: accumulation can be concurrent • Unordered: Collection is not ordered • Identity finish: Turn on/off finishing
  • 43. 43Hot Streaming Java Stream grep Parallel streaming private static List<String> grep(BufferedReader in, String upperCaseSearchWord){ // Accumulate the strings via collect with collectors return in.lines(). map(String::toUpperCase).parallel(). filter(s -> s.contains(upperCaseSearchWord)). collect(Collectors.toList()); }
  • 45. 45Hot Streaming Java Parallel streaming 6 5 1 8 2 9 7 4 3 9 7 4 3 8 2 6 5 1
  • 47. Stream thinking 47 • Thinking in streams can be difficult • Steep learning curve • Lambdas • Method references • Standard functional interfaces • Generics • Requires a functional mindset • Look at algorithms differently • Not all algorithms translate to streams Hot Streaming Java
  • 48. Stream thinking Streams are best suited for algorithms that… Traverse a sequence of data Reduces sequence to a thing No read-ahead no read-behind No state change during traversal
  • 49. Stream thinking 49 • When algorithms don’t fit: • Re-think the algorithm functionally • Use stream operations to maintain state • Or don’t use streams! Hot Streaming Java
  • 51. 51Hot Streaming Java Compare imperative quick sort vs. stream quick sort
  • 52. 52Hot Streaming Java public static ArrayList<Integer> imperativeQuickSort(ArrayList<Integer> array, int low, int n) { int lo = low; int hi = n; if (lo >= n) return array; // Step 1: find pivot point int mid = array.get((lo + hi) / 2); // Step 2: find & swap values less & greater than pivot while (lo < hi) { while (lo < hi && array.get(lo) < mid) { lo++; } while (lo < hi && array.get(hi) > mid) { hi--; } if (lo < hi) { // Swap values int temp = array.get(lo); array.set(lo, array.get(hi)); array.set(hi, temp); lo++; hi--; } } if (hi < lo) lo = hi; // Steps 3: split the array and repeat recursively imperativeQuickSort(array, low, lo); imperativeQuickSort(array, lo == low ? lo + 1 : lo, n); Imperative quick sort
  • 53. public static List<Integer> functionalSort(List<Integer> array) { List<Integer> returnArray = array; if (array.size() > 1) { // Step 1 int mid = array.get(array.size() / 2); // Step 2 Map<Integer, List<Integer>> map = array.stream().parallel(). collect(Collectors.groupingBy(i -> i < mid ? 0 : i == mid ? 1 : 2)); // Step 3 List<Integer> left = functionalSort(map.getOrDefault(0, new ArrayList<>())); List<Integer> middle = map.getOrDefault(1, new ArrayList<>()); List<Integer> right = functionalSort(map.getOrDefault(2, new ArrayList<>())); left.addAll(middle); left.addAll(right); returnArray = left; } return returnArray; } 53Hot Streaming Java Functional quick sort
  • 54. 54Hot Streaming Java Compare imperative Conway’s Game Of Life vs. stream-based Game of life
  • 55. 55Hot Streaming Java public static boolean[][] getNextGeneration(boolean[][] oldGeneration) { boolean[][] newGeneration = new boolean[oldGeneration.length][oldGeneration.length]; for (int y = 0; y < oldGeneration.length; ++y) { for (int x = 0; x < oldGeneration.length; ++x) { newGeneration[y][x] = isAlive(oldGeneration, y, x); } } return newGeneration; } private static int countLiveNeighborCells(boolean[][] generation, int y, int x) { int count = 0; for (int yIndex = y - 1; yIndex <= y + 1; ++yIndex) { if (yIndex >= 0 && yIndex < generation.length) { for (int xIndex = x - 1; xIndex <= x + 1; ++xIndex) { if (xIndex >= 0 && xIndex < generation.length) { if (generation[yIndex][xIndex] && !(xIndex == x && yIndex == y)) { ++count; } } } } } return count; } private static boolean isAlive(boolean[][] generation, int y, int x) { int liveCells = countLiveNeighborCells(generation, y, x); return (generation[y][x] && liveCells >= 2 && liveCells <= 3) || (!generation[y][x] && liveCells == 3); } Imperative
  • 56. 56Hot Streaming Java Conway’s Game of Life public static boolean[][] getNextGeneration(boolean[][] oldGeneration) { boolean[][] newGeneration = new boolean[oldGeneration.length][oldGeneration.length]; for (int y = 0; y < oldGeneration.length; ++y) { for (int x = 0; x < oldGeneration.length; ++x) { newGeneration[y][x] = isAlive(oldGeneration, y, x); } } return newGeneration; } private static int countLiveNeighborCells(boolean[][] generation, int y, int x) { int count = 0; for (int yIndex = y - 1; yIndex <= y + 1; ++yIndex) { if (yIndex >= 0 && yIndex < generation.length) { for (int xIndex = x - 1; xIndex <= x + 1; ++xIndex) { if (xIndex >= 0 && xIndex < generation.length) { if (generation[yIndex][xIndex] && !(xIndex == x && yIndex == y)) { ++count; } } } } } return count; } private static boolean isAlive(boolean[][] generation, int y, int x) { int liveCells = countLiveNeighborCells(generation, y, x); return (generation[y][x] && liveCells >= 2 && liveCells <= 3) || (!generation[y][x] && liveCells == 3); } public static boolean[][] getNextGenerationFunctional(boolean[][] oldGeneration) { boolean[][] newGeneration = new boolean[oldGeneration.length][oldGeneration.length]; List<Coordinates> list = new ArrayList<>(); IntStream.range(0, oldGeneration.length). forEach(nextY -> { list.addAll(IntStream.range(0, oldGeneration.length). parallel(). mapToObj(nextX -> new Coordinates(nextX, nextY)). filter(coordinates -> isAlive(oldGeneration, coordinates.getY(), coordinates.getX())). collect(Collectors.toList())); }); list.parallelStream(). forEach(nextCoordinate -> newGeneration[nextCoordinate.getY()][nextCoordinate.getX()] = true); return newGeneration; } private static int countLiveNeighborCells(boolean[][] generation, int y, int x) { return IntStream.rangeClosed(y - 1, y + 1). filter(yInner -> yInner >= 0 && yInner < generation.length). reduce(0, (yLeft, yRight) -> yLeft + IntStream.rangeClosed(x - 1, x + 1). filter(xInner -> xInner >= 0 && xInner < generation.length && generation[yRight][xInner] && !(xInner == x && yRight == y)). reduce(0, (xLeft, xRight) -> xLeft + 1)); } Stream-based
  • 57. Takeaway Grep & Quick Sort offered compelling reasons to implement with Streams Conway’s Game of Life did not
  • 58. 58Hot Streaming Java Part IV Parallel streams
  • 59. Parallel streams Parallel streams are best suited for algorithms that… Can split data easily & quickly Have no order constraint Can reduce concurrently Have large N and/or Q Hot Streaming Java
  • 60. 60Hot Streaming Java Parallel streaming Remember: A parallel stream must do everything a serial one does and more
  • 61. 61Hot Streaming Java Parallel streaming Sometimes, parallel streams are slower than their serial counterparts… And also their imperative counterparts
  • 63. Streams recap Hot Streaming Java 5 Think functionally when using streams 4 3 2 1 Use the right reduction tool Break through the learning curve Use parallel streaming intelligently Adapt thinking for streams or don’t use ‘em

Hinweis der Redaktion

  1. ----- Meeting Notes (2014-02-24 20:42) ----- 45 minutes
  2. ----- Meeting Notes (2014-02-24 20:42) ----- 35 minutes
  3. Declarative: we say what we want not how Opposite of imperative programing
  4. Java 8 only introduced
  5. From a Collection via the stream() and parallelStream() methods; From an array via Arrays.stream(Object[]); From static factory methods on the stream classes, such as Stream.of(Object[]), IntStream.range(int, int) or Stream.iterate(Object, UnaryOperator); The lines of a file can be obtained from BufferedReader.lines(); Streams of file paths can be obtained from methods in Files; Streams of random numbers can be obtained from Random.ints(); Numerous other stream-bearing methods in the JDK, including BitSet.stream(), Pattern.splitAsStream(java.lang.CharSequence)
  6. forEach lambda has side-effects, interferes with stream parallelization, incorrect processing and not thread-safe