parallel-ready java code: managing mutation in an imperative language
TRANSCRIPT
![Page 1: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/1.jpg)
Parallel-Ready Java CodeManaging Mutation in an Imperative Language
JavaDay KyivNov 2015
#parallelready@mauricenaftalin
![Page 2: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/2.jpg)
#parallelready @mauricenaftalin
Maurice Naftalin
Developer, designer, architect, teacher, learner, writer
![Page 3: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/3.jpg)
#parallelready @mauricenaftalin
Maurice Naftalin
Developer, designer, architect, teacher, learner, writer
JavaOne Rock Star
Java Champion
![Page 4: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/4.jpg)
#parallelready @mauricenaftalin
Repeat offender:
Maurice Naftalin
![Page 5: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/5.jpg)
#parallelready @mauricenaftalin
Repeat offender:
Maurice NaftalinJava 5
![Page 6: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/6.jpg)
#parallelready @mauricenaftalin
Repeat offender:
Maurice NaftalinJava 5 Java 8
![Page 8: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/8.jpg)
#parallelready @mauricenaftalin
Ideas in the Stream API
– Pipes/Filters
– Parallelism
• Recursive decomposition
• Stream-transforming (aka intermediate) operations
• Parallel merging
![Page 9: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/9.jpg)
#parallelready @mauricenaftalin
Pipes and Filters
• Venerable Unix tool-building pattern:ps -ef | grep login | cut -c 50- | head
• and in Enterprise Integration Patterns
![Page 10: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/10.jpg)
#parallelready @mauricenaftalin
Advantages of this pattern
Pipes and Filters
![Page 11: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/11.jpg)
#parallelready @mauricenaftalin
ps -ef | grep login | cut -c 50- | head
Advantages of this pattern
Pipes and Filters
![Page 12: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/12.jpg)
#parallelready @mauricenaftalin
- no intermediate variables
ps -ef | grep login | cut -c 50- | head
Advantages of this pattern
Pipes and Filters
![Page 13: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/13.jpg)
#parallelready @mauricenaftalin
- no intermediate variables
- less (or no) intermediate storage
ps -ef | grep login | cut -c 50- | head
Advantages of this pattern
Pipes and Filters
![Page 14: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/14.jpg)
#parallelready @mauricenaftalin
- no intermediate variables
- less (or no) intermediate storage
- lazy evaluation
ps -ef | grep login | cut -c 50- | head
Advantages of this pattern
Pipes and Filters
![Page 15: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/15.jpg)
#parallelready @mauricenaftalin
- no intermediate variables
- less (or no) intermediate storage
- lazy evaluation
- flexible tool-building:
ps -ef | grep login | cut -c 50- | head
Advantages of this pattern
Pipes and Filters
![Page 16: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/16.jpg)
#parallelready @mauricenaftalin
Pipes and Filters
Doug McIlroy, inventor of the Unix pipe:
![Page 17: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/17.jpg)
#parallelready @mauricenaftalin
Pipes and Filters
Write programs that do one thing well.
Doug McIlroy, inventor of the Unix pipe:
![Page 18: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/18.jpg)
#parallelready @mauricenaftalin
Pipes and Filters
Write programs that do one thing well.Write programs to work together.
Doug McIlroy, inventor of the Unix pipe:
![Page 19: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/19.jpg)
#parallelready @mauricenaftalin
Pipes and Filters
Write programs that do one thing well.Write programs to work together.
Write programs to handle text streams
Doug McIlroy, inventor of the Unix pipe:
![Page 20: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/20.jpg)
#parallelready @mauricenaftalin
Pipes and Filters
Write programs that do one thing well.Write programs to work together.
Write programs to handle text streams
Doug McIlroy, inventor of the Unix pipe:
XXXXXXXX
![Page 21: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/21.jpg)
#parallelready @mauricenaftalin
Pipes and Filters
Write programs that do one thing well.Write programs to work together.
Write programs to handle text streams
Doug McIlroy, inventor of the Unix pipe:
XXXXXXXX
value objects and primitives
![Page 22: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/22.jpg)
#parallelready @mauricenaftalin
Example Domain
city: City name: Stringage: int
Person
Person amy = new Person(Athens, "Amy", 21); ...
List<Person> people = Arrays.asList(jon, amy, bill);
![Page 23: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/23.jpg)
#parallelready @mauricenaftalin
Pipes and Filters in Java
boolean allAdults = people.stream() .mapToInt(Person::getAge) .allMatch(a -> a >= 21);
Is everyone an adult?
![Page 24: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/24.jpg)
#parallelready @mauricenaftalin
Pipes and Filters in Java
boolean allAdults = people.stream() .mapToInt(Person::getAge) .allMatch(a -> a >= 21);
people.stream() .map(Person::getCity) .forEach(System.out::println);
Is everyone an adult?
Print the names ofthe inhabited cities
![Page 25: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/25.jpg)
Streams
Sequence of values
• Not a collection — may be partially evaluated or exhausted
• Like an iterator, yielding elements for processing
• Not like an iterator, not associated with any storage mechanism
• Sources: collections, arrays, generators, IO channels
• Can be
- parallel
- infinite
• Primitive specialisations: IntStream, LongStream, DoubleStream
![Page 26: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/26.jpg)
#parallelready @mauricenaftalin
Sources – sequential streams
Stream Sources: – supply elements one at a time– implementations of java.util.Spliterator – 40+ methods in platform classes
– including Collection.stream
boolean allAdults = people.stream() .mapToInt(Person::getAge) .allMatch(a -> a >= 21);
![Page 27: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/27.jpg)
#parallelready @mauricenaftalin
Intermediate Operations
Filtering filter()
Mapping map()
One-to-Many Mapping flatMap()
Debugging peek()
Sorting/Deduplicating sorted(), distinct()
Truncating skip(), limit()
![Page 28: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/28.jpg)
#parallelready @mauricenaftalin
filter(s -> s.length() < 4)
Stream<String>
Predicate<String
Stream<String>
filter()
![Page 29: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/29.jpg)
#parallelready @mauricenaftalin
filter(s -> s.length() < 4)
Stream<String>
Predicate<String
Stream<String>
filter()
“jim”
✔
![Page 30: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/30.jpg)
#parallelready @mauricenaftalin
filter(s -> s.length() < 4)
Stream<String>
Predicate<String
Stream<String>
filter()
✔
![Page 31: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/31.jpg)
#parallelready @mauricenaftalin
✖
filter(s -> s.length() < 4)
Stream<String>
Predicate<String
Stream<String>
filter()
✔
![Page 32: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/32.jpg)
#parallelready @mauricenaftalin
✖
filter(s -> s.length() < 4)
Stream<String>
Predicate<String
Stream<String>
filter()
✔
“amy”
![Page 33: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/33.jpg)
#parallelready @mauricenaftalin
✖
filter(s -> s.length() < 4)
Stream<String>
Predicate<String
Stream<String>
filter()
✔
![Page 34: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/34.jpg)
#parallelready @mauricenaftalin
map()
map(Person::getCity)
Stream<Person> Stream<City>
Function<Person,City
![Page 35: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/35.jpg)
#parallelready @mauricenaftalin
map()
map(Person::getCity)
Stream<Person> Stream<City>
Function<Person,City
bill
![Page 36: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/36.jpg)
#parallelready @mauricenaftalin
map()
map(Person::getCity)
Stream<Person> Stream<City>
Function<Person,City
![Page 37: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/37.jpg)
#parallelready @mauricenaftalin
map()
map(Person::getCity)
Stream<Person> Stream<City>
Function<Person,City
amy
![Page 38: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/38.jpg)
#parallelready @mauricenaftalin
map()
map(Person::getCity)
Stream<Person> Stream<City>
Function<Person,City
![Page 39: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/39.jpg)
#parallelready @mauricenaftalin
peek()
peek(System.out::println)
Consumer<T>
System.out::println
T
Stream<T>
![Page 40: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/40.jpg)
#parallelready @mauricenaftalin
flatMapToInt()
Stream<String>
Function<String,IntStream>
IntStream
flatMapToInt(String::chars)
![Page 41: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/41.jpg)
#parallelready @mauricenaftalin
flatMapToInt()
Stream<String>
Function<String,IntStream>
IntStream
'a' 'm' 'y'
flatMapToInt(String::chars)
![Page 42: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/42.jpg)
#parallelready @mauricenaftalin
flatMapToInt()
Stream<String>
Function<String,IntStream>
IntStream
'm' 'y'
flatMapToInt(String::chars)
![Page 43: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/43.jpg)
#parallelready @mauricenaftalin
flatMapToInt()
Stream<String>
Function<String,IntStream>
IntStream
'y'
flatMapToInt(String::chars)
![Page 44: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/44.jpg)
#parallelready @mauricenaftalin
flatMapToInt()
Stream<String>
Function<String,IntStream>
IntStream
flatMapToInt(String::chars)
![Page 45: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/45.jpg)
Ending Streams
Search findFirst(),findAny()anyMatch(), allMatch()
Side-effecting forEach(), forEachOrdered()
Reductions ...
Stream Sinks (terminal operations):
![Page 46: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/46.jpg)
#parallelready @mauricenaftalin
Parallelism
“The best way to write parallel applications is not to have to think about parallelism.”
Guy Steele
![Page 47: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/47.jpg)
#parallelready @mauricenaftalin
Automated Resource Management
![Page 48: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/48.jpg)
#parallelready @mauricenaftalin
Automated Resource Management
![Page 49: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/49.jpg)
#parallelready @mauricenaftalin
Assemblers
Automated Resource Management
![Page 50: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/50.jpg)
#parallelready @mauricenaftalin
AssemblersLinkers
Automated Resource Management
![Page 51: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/51.jpg)
#parallelready @mauricenaftalin
AssemblersLinkersRegister allocation
Automated Resource Management
![Page 52: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/52.jpg)
#parallelready @mauricenaftalin
AssemblersLinkersRegister allocationHeap management
Automated Resource Management
![Page 53: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/53.jpg)
#parallelready @mauricenaftalin
AssemblersLinkersRegister allocationHeap managementVirtual memory
Automated Resource Management
![Page 54: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/54.jpg)
#parallelready @mauricenaftalin
AssemblersLinkersRegister allocationHeap managementVirtual memoryParallelisation
Automated Resource Management
![Page 55: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/55.jpg)
#parallelready @mauricenaftalin
Retooling
![Page 56: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/56.jpg)
#parallelready @mauricenaftalin
Some old habits won’t work any more
Retooling
![Page 57: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/57.jpg)
#parallelready @mauricenaftalin
Some old habits won’t work any moreminimising operation count (eg by reusing previously computed results)
Retooling
![Page 58: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/58.jpg)
#parallelready @mauricenaftalin
Some old habits won’t work any moreminimising operation count (eg by reusing previously computed results)
- be prepared to “waste” operations to reduce communication
Retooling
![Page 59: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/59.jpg)
#parallelready @mauricenaftalin
Some old habits won’t work any moreminimising operation count (eg by reusing previously computed results)
- be prepared to “waste” operations to reduce communication
minimising space usage (by sharing data between operations)
Retooling
![Page 60: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/60.jpg)
#parallelready @mauricenaftalin
Some old habits won’t work any moreminimising operation count (eg by reusing previously computed results)
- be prepared to “waste” operations to reduce communication
minimising space usage (by sharing data between operations)
- parallel programs trade space for reduced coupling
Retooling
![Page 61: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/61.jpg)
#parallelready @mauricenaftalin
Some old habits won’t work any moreminimising operation count (eg by reusing previously computed results)
- be prepared to “waste” operations to reduce communication
minimising space usage (by sharing data between operations)
- parallel programs trade space for reduced coupling
linear decomposition
Retooling
![Page 62: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/62.jpg)
#parallelready @mauricenaftalin
Some old habits won’t work any moreminimising operation count (eg by reusing previously computed results)
- be prepared to “waste” operations to reduce communication
minimising space usage (by sharing data between operations)
- parallel programs trade space for reduced coupling
linear decomposition
- processing one thing at a time to accumulate results
Retooling
![Page 63: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/63.jpg)
#parallelready @mauricenaftalin
Some old habits won’t work any moreminimising operation count (eg by reusing previously computed results)
- be prepared to “waste” operations to reduce communication
minimising space usage (by sharing data between operations)
- parallel programs trade space for reduced coupling
linear decomposition
- processing one thing at a time to accumulate results
Retooling
No more accumulators!
![Page 64: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/64.jpg)
#parallelready @mauricenaftalin
Seriously, No More Accumulators??
![Page 65: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/65.jpg)
#parallelready @mauricenaftalin
Seriously, No More Accumulators??
No more accumulators!
![Page 66: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/66.jpg)
#parallelready @mauricenaftalin
Seriously, No More Accumulators??
Divide And Conquer algorithms instead
- symmetric merge operations
No more accumulators!
![Page 67: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/67.jpg)
#parallelready @mauricenaftalin
Seriously, No More Accumulators??
Divide And Conquer algorithms instead
- symmetric merge operationsMajor impact on terminal operations
- almost all are accumulations of some kind
No more accumulators!
![Page 68: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/68.jpg)
#parallelready @mauricenaftalin
• Using an accumulator:
Simple Example – Summation
int[] vals = new int[100];Arrays.setAll(vals, i -> i);
int sum = 0;for (int i = 0 ; i < vals.length ; i++) { sum += vals[i];}
0 1 2 3
+
+
+
![Page 69: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/69.jpg)
#parallelready @mauricenaftalin
• Avoiding an accumulator:
Simple Example – Summation
![Page 70: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/70.jpg)
#parallelready @mauricenaftalin
• Avoiding an accumulator:
Simple Example – Summation
![Page 71: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/71.jpg)
#parallelready @mauricenaftalin
+
+
+
• Avoiding an accumulator:
Simple Example – Summation
int[] vals = new int[100];Arrays.setAll(vals, i -> i);
OptionalInt sum = Arrays.stream(vals) .reduce((a,b) -> a + b);
1 2 30
![Page 72: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/72.jpg)
#parallelready @mauricenaftalin
+ +
+
• Avoiding an accumulator:
Simple Example – Summation
int[] vals = new int[100];Arrays.setAll(vals, i -> i);
OptionalInt sum = Arrays.stream(vals) .reduce((a,b) -> a + b);
1 2 30
![Page 73: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/73.jpg)
#parallelready @mauricenaftalin
+ +
+
• Avoiding an accumulator:
Simple Example – Summation
int[] vals = new int[100];Arrays.setAll(vals, i -> i);
OptionalInt sum = Arrays.stream(vals) .reduce((a,b) -> a + b);
BinaryOperator must be associative!
1 2 30
![Page 74: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/74.jpg)
#parallelready @mauricenaftalin
+ +
+
• Avoiding an accumulator:
Simple Example – Summation
int[] vals = new int[100];Arrays.setAll(vals, i -> i);
OptionalInt sum = Arrays.stream(vals) .reduce((a,b) -> a + b);
BinaryOperator must be associative!a + (b + c) = (a + b) + c
1 2 30
![Page 75: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/75.jpg)
#parallelready @mauricenaftalin
Reduction of Primitives0 1 2
+
+
+
+
+
+
+
3 4 5
0 0
![Page 76: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/76.jpg)
#parallelready @mauricenaftalin
Reduction of Primitives0 1 2
+
+
+
+
+
+
+
3 4 5
Identity
BinaryOperator
0 0
![Page 77: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/77.jpg)
#parallelready @mauricenaftalin
Reduction of Primitives0 1 2
+
+
+
+
+
+
+
3 4 5
Identity
BinaryOperator
0 0
![Page 78: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/78.jpg)
#parallelready @mauricenaftalin
Reduction of Mutable Objects?
add
add
add
add
add
add
combine
[ ] [ ]
Identity
Accumulator
Combiner
e0 e1 e2 e3 e4 e5
![Page 79: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/79.jpg)
#parallelready @mauricenaftalin
Reduction of Mutable Objects?
add
add
add
add
add
add
combine
[ ] [ ]
Identity
Accumulator
Combiner
e0 e1 e2 e3 e4 e5
Not�Parallel-ReadyNot�Parallel-Ready
![Page 80: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/80.jpg)
#parallelready @mauricenaftalin
Collector
combine
()->[]
Supplier
Accumulator
Combiner
e0 e1 e2 e3 e4 e5
()->[]
add
add
add
add
add
add
![Page 81: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/81.jpg)
#parallelready @mauricenaftalin
Using the Predefined Collectors
Predefined Standalone Collectors – from factory methods in Collectors class
• toList(), toSet(), toMap(), joining()
• toMap(), toCollection()
• groupingBy(), partitioningBy()
![Page 82: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/82.jpg)
#parallelready @mauricenaftalin
Using the Predefined Collectors
Predefined Standalone Collectors – from factory methods in Collectors class
• toList(), toSet(), toMap(), joining()
• toMap(), toCollection()
• groupingBy(), partitioningBy()
framework providesthe Supplier
![Page 83: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/83.jpg)
#parallelready @mauricenaftalin
Using the Predefined Collectors
Predefined Standalone Collectors – from factory methods in Collectors class
• toList(), toSet(), toMap(), joining()
• toMap(), toCollection()
• groupingBy(), partitioningBy()
user providesthe Supplier
framework providesthe Supplier
![Page 84: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/84.jpg)
#parallelready @mauricenaftalin
Using the Predefined Collectors
Predefined Standalone Collectors – from factory methods in Collectors class
• toList(), toSet(), toMap(), joining()
• toMap(), toCollection()
• groupingBy(), partitioningBy()
user providesthe Supplier
framework providesthe Supplier
produce a classification map
![Page 85: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/85.jpg)
#parallelready @mauricenaftalin
Simple Collector – toSet()people.stream().collect(Collectors.toSet())
![Page 86: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/86.jpg)
#parallelready @mauricenaftalin
Collector<Person,?,Set<Pers
Stream<Person> Set<Person>
Simple Collector – toSet()people.stream().collect(Collectors.toSet())
![Page 87: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/87.jpg)
#parallelready @mauricenaftalin
Collector<Person,?,Set<Pers
Stream<Person> Set<Person>bill
Simple Collector – toSet()people.stream().collect(Collectors.toSet())
![Page 88: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/88.jpg)
#parallelready @mauricenaftalin
Collector<Person,?,Set<Pers
Stream<Person> Set<Person>bill
Simple Collector – toSet()people.stream().collect(Collectors.toSet())
![Page 89: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/89.jpg)
#parallelready @mauricenaftalin
Collector<Person,?,Set<Pers
Stream<Person> Set<Person>bill
Simple Collector – toSet()people.stream().collect(Collectors.toSet())
![Page 90: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/90.jpg)
#parallelready @mauricenaftalin
Collector<Person,?,Set<Pers
Stream<Person> Set<Person>billjon
Simple Collector – toSet()people.stream().collect(Collectors.toSet())
![Page 91: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/91.jpg)
#parallelready @mauricenaftalin
Collector<Person,?,Set<Pers
Stream<Person> Set<Person>billjon
Simple Collector – toSet()people.stream().collect(Collectors.toSet())
![Page 92: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/92.jpg)
#parallelready @mauricenaftalin
Collector<Person,?,Set<Pers
Stream<Person> Set<Person>amy
billjon
Simple Collector – toSet()people.stream().collect(Collectors.toSet())
![Page 93: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/93.jpg)
#parallelready @mauricenaftalin
Collector<Person,?,Set<Pers
Stream<Person> Set<Person>
amy
billjon
Simple Collector – toSet()people.stream().collect(Collectors.toSet())
![Page 94: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/94.jpg)
#parallelready @mauricenaftalin
Collector<Person,?,Set<Pers
Set<Person>
amy
billjon
Simple Collector – toSet()people.stream().collect(Collectors.toSet())
![Page 95: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/95.jpg)
#parallelready @mauricenaftalin
Collector<Person,?,Set<Pers
Set<Person>{ , , }amybilljon
Simple Collector – toSet()people.stream().collect(Collectors.toSet())
![Page 96: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/96.jpg)
#parallelready @mauricenaftalin
toMap(Function<T,K> keyMapper, Function<T,U> valueMapper)
people.stream().collect( Collectors.toMap(
Person::getCity, Person::getName))
![Page 97: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/97.jpg)
#parallelready @mauricenaftalin
toMap(Function<T,K> keyMapper, Function<T,U> valueMapper)
Stream<Person>
toMap() Map<City,String>
Collector<Person,?,Map<City,String>
![Page 98: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/98.jpg)
#parallelready @mauricenaftalin
toMap(Function<T,K> keyMapper, Function<T,U> valueMapper)
Stream<Person>
toMap() Map<City,String>
bill
![Page 99: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/99.jpg)
#parallelready @mauricenaftalin
toMap(Function<T,K> keyMapper, Function<T,U> valueMapper)
Stream<Person>
toMap() Map<City,String>
bill
Person::getCity
![Page 100: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/100.jpg)
#parallelready @mauricenaftalin
toMap(Function<T,K> keyMapper, Function<T,U> valueMapper)
Stream<Person>
toMap() Map<City,String>London
bill
![Page 101: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/101.jpg)
#parallelready @mauricenaftalin
toMap(Function<T,K> keyMapper, Function<T,U> valueMapper)
Stream<Person>
toMap() Map<City,String>London
bill Person::getName
![Page 102: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/102.jpg)
#parallelready @mauricenaftalin
toMap(Function<T,K> keyMapper, Function<T,U> valueMapper)
Stream<Person>
toMap() Map<City,String>London “Bill”
bill Person::getName
![Page 103: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/103.jpg)
#parallelready @mauricenaftalin
toMap(Function<T,K> keyMapper, Function<T,U> valueMapper)
Stream<Person>
toMap() Map<City,String>London “Bill”
![Page 104: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/104.jpg)
#parallelready @mauricenaftalin
toMap(Function<T,K> keyMapper, Function<T,U> valueMapper)
Stream<Person>
toMap() Map<City,String>
amy
London “Bill”
![Page 105: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/105.jpg)
#parallelready @mauricenaftalin
toMap(Function<T,K> keyMapper, Function<T,U> valueMapper)
Stream<Person>
toMap() Map<City,String>
Athensamy
London “Bill”
![Page 106: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/106.jpg)
#parallelready @mauricenaftalin
toMap(Function<T,K> keyMapper, Function<T,U> valueMapper)
Stream<Person>
toMap() Map<City,String>
“Amy”Athens
London “Bill”
![Page 107: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/107.jpg)
#parallelready @mauricenaftalin
toMap(Function<T,K> keyMapper, Function<T,U> valueMapper)
Stream<Person>
toMap() Map<City,String>
“Amy”Athensjon
London “Bill”
![Page 108: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/108.jpg)
#parallelready @mauricenaftalin
toMap(Function<T,K> keyMapper, Function<T,U> valueMapper)
Stream<Person>
toMap() Map<City,String>
“Amy”Athens
London “Bill”
Tulsa “Jon”
![Page 109: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/109.jpg)
#parallelready @mauricenaftalin
toMap(Function<T,K> keyMapper, Function<T,U> valueMapper)
toMap() Map<City,String>
“Amy”Athens
London “Bill”
Tulsa “Jon”
![Page 110: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/110.jpg)
#parallelready @mauricenaftalin
toMap(Function<T,K> keyMapper, Function<T,U> valueMapper)
toMap() Map<City,String>
“Amy”AthensLondon “Bill”
Tulsa “Jon”
![Page 111: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/111.jpg)
#parallelready @mauricenaftalin
groupingBy(Function<T,K> classifier)Uses the classifier function to make a classification mapping
Like toMap(), except that the values placed in the map are lists of the elements, one List corresponding to each classification key:
For example, use Person.getCity() to make a Map<City,List<Person>>
Map<City,List<Person>> peopleByCity = people.stream(). collect(Collectors.groupingBy(Person::getCity));
![Page 112: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/112.jpg)
#parallelready @mauricenaftalin
Stream<Person>
groupingBy() Map<City,List<Person>>
bill
jon
amy Athens
London
Collector<Person,?,Map<City,List<Person>>>
groupingBy(Function<Person,City>)
Classifier
Person→City
![Page 113: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/113.jpg)
#parallelready @mauricenaftalin
Stream<Person>
groupingBy() Map<City,List<Person>>
bill
jon
amy Athens
London
groupingBy(Function<Person,City>)
![Page 114: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/114.jpg)
#parallelready @mauricenaftalin
Stream<Person>
groupingBy() Map<City,List<Person>>
bill
jon
amy Athens
bill London
groupingBy(Function<Person,City>)
![Page 115: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/115.jpg)
#parallelready @mauricenaftalin
Stream<Person>
groupingBy() Map<City,List<Person>>
bill
jon
amy Athens
billLondon
groupingBy(Function<Person,City>)
[ ]
![Page 116: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/116.jpg)
#parallelready @mauricenaftalin
Stream<Person>
groupingBy() Map<City,List<Person>>
bill
jon
amy Athens [ ]
bill
amy
London
groupingBy(Function<Person,City>)
[ ]
![Page 117: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/117.jpg)
#parallelready @mauricenaftalin
Stream<Person>
groupingBy() Map<City,List<Person>>
bill
jon
amy Athens [ ]
[ , ]bill
amy
jonLondon
groupingBy(Function<Person,City>)
![Page 118: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/118.jpg)
#parallelready @mauricenaftalin
groupingBy() Map<City,List<Person>>
bill
jon
amy Athens [ ]
[ , ]bill
amy
jonLondon
groupingBy(Function<Person,City>)
![Page 119: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/119.jpg)
#parallelready @mauricenaftalin
groupingBy() Map<City,List<Person>>
bill
jon
amy
Athens [ ]
[ , ]bill
amy
jonLondon
groupingBy(Function<Person,City>)
![Page 120: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/120.jpg)
groupingBy(classifier, downstream)
Map<City,List<Person>> peopleByCity = people.stream(). collect(Collectors.groupingBy(Person::getCity,toSet()));
Uses the classifier function to make a classification mapping into a container defined by a downstream Collector
Like toMap(), except that the values placed in the map are containers of the elements, one container corresponding to each classification key:
For example, use Person.getCity() to make a Map<City,Set<Person>>
Map<City,Set<Person>> peopleByCity = people.stream(). collect(Collectors.groupingBy(Person::getCity,toSet()));
![Page 121: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/121.jpg)
#parallelready @mauricenaftalin
groupingBy(Function classifier,Collector downstream))
Stream<Person>
groupingBy()Map<City,Set<Person>>
bill
jon
amy
London
Athens
DownstreamCollector
—toSet()
Stream<Person>
Classifier
Person→City
![Page 122: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/122.jpg)
#parallelready @mauricenaftalin
groupingBy(Function classifier,Collector downstream))
Stream<Person>
groupingBy()Map<City,Set<Person>>
bill
jon
amy
London
Athens
![Page 123: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/123.jpg)
#parallelready @mauricenaftalin
groupingBy(Function classifier,Collector downstream))
Stream<Person>
groupingBy()Map<City,Set<Person>>
bill
jon
amy
London
Athens
bill
![Page 124: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/124.jpg)
#parallelready @mauricenaftalin
groupingBy(Function classifier,Collector downstream))
Stream<Person>
groupingBy()Map<City,Set<Person>>
bill
jon
amy
London
Athens
bill
![Page 125: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/125.jpg)
#parallelready @mauricenaftalin
groupingBy(Function classifier,Collector downstream))
Stream<Person>
groupingBy()Map<City,Set<Person>>
bill
jon
amy
London
Athens
bill
![Page 126: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/126.jpg)
#parallelready @mauricenaftalin
groupingBy(Function classifier,Collector downstream))
Stream<Person>
groupingBy()Map<City,Set<Person>>
bill
jon
amy
London
Athensamy
bill
![Page 127: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/127.jpg)
#parallelready @mauricenaftalin
groupingBy(Function classifier,Collector downstream))
Stream<Person>
groupingBy()Map<City,Set<Person>>
bill
jon
amy
London
Athens amy
bill
![Page 128: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/128.jpg)
#parallelready @mauricenaftalin
groupingBy(Function classifier,Collector downstream))
Stream<Person>
groupingBy()Map<City,Set<Person>>
bill
jon
amy
London
Athens amy
bill
![Page 129: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/129.jpg)
#parallelready @mauricenaftalin
groupingBy(Function classifier,Collector downstream))
Stream<Person>
groupingBy()Map<City,Set<Person>>
bill
jon
amy
London
Athens amy
jon bill
![Page 130: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/130.jpg)
#parallelready @mauricenaftalin
groupingBy(Function classifier,Collector downstream))
Stream<Person>
groupingBy()Map<City,Set<Person>>
bill
jon
amy
London
Athens amy
jonbill
![Page 131: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/131.jpg)
#parallelready @mauricenaftalin
groupingBy(Function classifier,Collector downstream))
groupingBy()Map<City,Set<Person>>
bill
jon
amy
London
Athens amy
jonbill
![Page 132: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/132.jpg)
#parallelready @mauricenaftalin
groupingBy(Function classifier,Collector downstream))
groupingBy()Map<City,Set<Person>>
bill
jon
amy
London
Athens amy
jonbill
![Page 133: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/133.jpg)
#parallelready @mauricenaftalin
groupingBy(Function classifier,Collector downstream))
groupingBy()Map<City,Set<Person>>
bill
jon
amy
London
Athens { }amy
{ , }jonbill
![Page 134: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/134.jpg)
#parallelready @mauricenaftalin
groupingBy(Function classifier,Collector downstream))
groupingBy()Map<City,Set<Person>>
bill
jon
amy
London
Athens { }amy
{ , }jonbill
![Page 135: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/135.jpg)
#parallelready @mauricenaftalin
Specialised Downstream Collectors
Stream<Person>
groupingBy()
bill
jon
amy
London
Athens
amyjonbillDownstream
CollectorClassifier
Duals of convenience reductionsMapping collector
“Ordinary”collectors +
![Page 136: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/136.jpg)
Concurrent Collection
![Page 137: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/137.jpg)
Concurrent Collection
![Page 138: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/138.jpg)
Concurrent CollectionThread safety is guaranteed by the framework
• Even for non-threadsafe containers!
![Page 139: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/139.jpg)
Concurrent CollectionThread safety is guaranteed by the framework
• Even for non-threadsafe containers!
But at a price... So what if your container is already threadsafe?
• a concurrentMap implementation?
![Page 140: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/140.jpg)
Concurrent CollectionThread safety is guaranteed by the framework
• Even for non-threadsafe containers!
But at a price... So what if your container is already threadsafe?
• a concurrentMap implementation?
![Page 141: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/141.jpg)
Concurrent CollectionThread safety is guaranteed by the framework
• Even for non-threadsafe containers!
But at a price... So what if your container is already threadsafe?
• a concurrentMap implementation?
Every overload of toMap() and groupingBy() has a dual
• toConcurrentMap(...)
• groupingByConcurrent(...)
![Page 142: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/142.jpg)
Writing a Collector
![Page 143: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/143.jpg)
Writing a Collector
Why would you want to?
![Page 144: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/144.jpg)
Writing a Collector
Why would you want to?
• accumulate to a container that doesn’t implement Collection
![Page 145: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/145.jpg)
Writing a Collector
Why would you want to?
• accumulate to a container that doesn’t implement Collection
• share state between values being collected
![Page 146: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/146.jpg)
Writing a Collector
Why would you want to?
• accumulate to a container that doesn’t implement Collection
• share state between values being collected
• to show how to parallelize “iterative-looking” problems
![Page 147: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/147.jpg)
Oprah’s Problem
![Page 148: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/148.jpg)
Oprah’s Problem
How to find a book?
![Page 149: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/149.jpg)
Oprah’s Problem
How to find a book?
336640
144624
Page count 239640
3841104
172400
![Page 150: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/150.jpg)
What Oprah Needs
New Earth 0Poisonwood Bible 336
Night 976A Fine Balance 1120
...
Cumulative page counts
![Page 151: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/151.jpg)
Creating A Partial Solution
,“Night” 144 976 “AFB” 624 1120 ]“NE” 336 0 “PB” 640 336[ , ,
![Page 152: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/152.jpg)
Creating A Partial Solution
,“Night” 144 0[ “AFB” 624 144 ]“NE” 336 0 “PB” 640 336[ ],
,“Night” 144 976 “AFB” 624 1120 ]“NE” 336 0 “PB” 640 336[ , ,
![Page 153: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/153.jpg)
Creating A Partial Solution
combiner
,“Night” 144 0[ “AFB” 624 144 ]“NE” 336 0 “PB” 640 336[ ],
,“Night” 144 976 “AFB” 624 1120 ]“NE” 336 0 “PB” 640 336[ , ,
![Page 154: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/154.jpg)
Creating A Partial Solution
combiner
,“Night” 144 0[ “AFB” 624 144 ]“NE” 336 0 “PB” 640 336[ ],
,“Night” 144 976 “AFB” 624 1120 ]“NE” 336 0 “PB” 640 336[ , ,
![Page 155: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/155.jpg)
Supplier and Accumulator
“NE” 336 0 “PB” 640 336[ ],
]“NE” 336 0[
][
aNewEarth thePoisonwoodBible
accumulator
accumulator
supplier
![Page 156: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/156.jpg)
Combiner
,“Night” 144 976 “AFB” 624 1120 ]“NE” 336 0 “PB” 640 336[ , ,
![Page 157: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/157.jpg)
Combiner
,“Night” 144 0[ “AFB” 624 144 ]“NE” 336 0 “PB” 640 336[ ],
,“Night” 144 976 “AFB” 624 1120 ]“NE” 336 0 “PB” 640 336[ , ,
![Page 158: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/158.jpg)
Combiner
combiner
,“Night” 144 0[ “AFB” 624 144 ]“NE” 336 0 “PB” 640 336[ ],
,“Night” 144 976 “AFB” 624 1120 ]“NE” 336 0 “PB” 640 336[ , ,
![Page 159: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/159.jpg)
Combiner
combiner
,“Night” 144 0[ “AFB” 624 144 ]“NE” 336 0 “PB” 640 336[ ],
,“Night” 144 976 “AFB” 624 1120 ]“NE” 336 0 “PB” 640 336[ , ,
976
![Page 160: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/160.jpg)
Combiner
combiner
,“Night” 144 0[ “AFB” 624 144 ]“NE” 336 0 “PB” 640 336[ ],
,“Night” 144 976 “AFB” 624 1120 ]“NE” 336 0 “PB” 640 336[ , ,
976
![Page 161: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/161.jpg)
Combiner
combiner
,“Night” 144 0[ “AFB” 624 144 ]“NE” 336 0 “PB” 640 336[ ],
,“Night” 144 976 “AFB” 624 1120 ]“NE” 336 0 “PB” 640 336[ , ,
976
![Page 162: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/162.jpg)
#parallelready @mauricenaftalin
Sources – parallel streams
Implemented by a Spliterator
![Page 163: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/163.jpg)
#parallelready @mauricenaftalin
Sources – parallel streams
Implemented by a Spliterator
![Page 164: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/164.jpg)
#parallelready @mauricenaftalin
Sources – parallel streams
Implemented by a Spliterator
![Page 165: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/165.jpg)
#parallelready @mauricenaftalin
Sources – parallel streams
Implemented by a Spliterator
![Page 166: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/166.jpg)
#parallelready @mauricenaftalin
Sources – parallel streams
Implemented by a Spliterator
![Page 167: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/167.jpg)
#parallelready @mauricenaftalin
Sources – parallel streams
Implemented by a Spliterator
![Page 168: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/168.jpg)
#parallelready @mauricenaftalin
Sources – parallel streams
Implemented by a Spliterator
![Page 169: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/169.jpg)
#parallelready @mauricenaftalin
Sources – parallel streams
Implemented by a Spliterator
![Page 170: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/170.jpg)
#parallelready @mauricenaftalin
Sources – parallel streams
Implemented by a Spliterator
![Page 171: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/171.jpg)
#parallelready @mauricenaftalin
x2
x0
x1
x3
x0
x1
x2
x3
Intermediate Op(s)
(Mutable)Reduction
Spliterator
Visualizing Stream Operations
![Page 172: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/172.jpg)
#parallelready @mauricenaftalin
x2
x0
x1
x3
x0
x1
x2x3
Intermediate Op(s)
(Mutable)Reduction
Spliterator
Visualizing Stream Operations
![Page 173: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/173.jpg)
#parallelready @mauricenaftalin
x2
x0
x1
x3
y0
y1
y2
y3
Intermediate Op(s)
(Mutable)Reduction
Spliterator
Visualizing Stream Operations
![Page 174: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/174.jpg)
#parallelready @mauricenaftalin
x2
x0
x1
x3
y0y1y2y3
Intermediate Op(s)
(Mutable)Reduction
Spliterator
Visualizing Stream Operations
![Page 175: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/175.jpg)
#parallelready @mauricenaftalin
x2
x0
x1
x3
x0
x1
x2
x3
Visualizing Stream Operations
Intermediate Op(s)
(Mutable)Reduction
Spliterator
![Page 176: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/176.jpg)
#parallelready @mauricenaftalin
x2
x0
x1
x3
x0
x1
x2
x3
Visualizing Stream Operations
Intermediate Op(s)
(Mutable)Reduction
Spliterator
![Page 177: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/177.jpg)
#parallelready @mauricenaftalin
x2
x0
x1
x3
x0
x1
x2x3
Visualizing Stream Operations
Intermediate Op(s)
(Mutable)Reduction
Spliterator
![Page 178: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/178.jpg)
#parallelready @mauricenaftalin
x2
x0
x1
x3
x0
x1
x2x3
Visualizing Stream Operations
Intermediate Op(s)
(Mutable)Reduction
Spliterator
![Page 179: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/179.jpg)
#parallelready @mauricenaftalin
x2
x0
x1
x3
x0
x1
x2x3
Visualizing Stream Operations
Intermediate Op(s)
(Mutable)Reduction
Spliterator
![Page 180: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/180.jpg)
#parallelready @mauricenaftalin
x2
x0
x1
x3
Visualizing Stream Operations
y0
y1
y2
y3
Intermediate Op(s)
(Mutable)Reduction
Spliterator
![Page 181: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/181.jpg)
#parallelready @mauricenaftalin
x2
x0
x1
x3
Visualizing Stream Operations
y0y1y2y3
Intermediate Op(s)
(Mutable)Reduction
Spliterator
![Page 182: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/182.jpg)
Visualizing Stream Operations
x2
x1
x3
x1
x2
x3
Intermediate Op(s)
(Mutable)Reduction
Spliterator
x0x0
peek(System.out::print)
![Page 183: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/183.jpg)
Visualizing Stream Operations
x2
x1
x3
x1
x2
x3
Intermediate Op(s)
(Mutable)Reduction
Spliterator
x0x0
peek(System.out::print)
![Page 184: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/184.jpg)
Visualizing Stream Operations
x2
x1
x3
x1
x2
x3
Intermediate Op(s)
(Mutable)Reduction
Spliterator
x0
x0
x0
peek(System.out::print)
![Page 185: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/185.jpg)
Visualizing Stream Operations
x2
x1
x3
x1
x2
x3
Intermediate Op(s)
(Mutable)Reduction
Spliterator
x0
x0
x0
peek(System.out::print)
![Page 186: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/186.jpg)
Visualizing Stream Operations
x2
x1
x3
x1
x2x3
Intermediate Op(s)
(Mutable)Reduction
Spliterator
x0
x2
x0
x0
peek(System.out::print)
![Page 187: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/187.jpg)
Visualizing Stream Operations
x2
x1
x3
x1
x2x3
Intermediate Op(s)
(Mutable)Reduction
Spliterator
x0x2
x0
x0
peek(System.out::print)
![Page 188: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/188.jpg)
Visualizing Stream Operations
x2
x1
x3
x1
x2x3
Intermediate Op(s)
(Mutable)Reduction
Spliterator
x0x2
x0
x0
peek(System.out::print)
![Page 189: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/189.jpg)
Visualizing Stream Operations
x2
x1
x3
x1
x2x3
Intermediate Op(s)
(Mutable)Reduction
Spliterator
x0x2
x0
x3
x0
peek(System.out::print)
![Page 190: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/190.jpg)
Visualizing Stream Operations
x2
x1
x3
x1
x2x3
Intermediate Op(s)
(Mutable)Reduction
Spliterator
x0x2
x0
x3
x0
peek(System.out::print)
![Page 191: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/191.jpg)
Visualizing Stream Operations
x2
x1
x3
x1
x2x3
Intermediate Op(s)
(Mutable)Reduction
Spliterator
x0x2
x0
x3
x0
peek(System.out::print)
![Page 192: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/192.jpg)
Visualizing Stream Operations
x2
x1
x3
x1
x2x3
Intermediate Op(s)
(Mutable)Reduction
Spliterator
x0x2
x0
x3
x0
peek(System.out::print)
![Page 193: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/193.jpg)
Visualizing Stream Operations
x2
x1
x3
y0
y1
y2
y3
Intermediate Op(s)
(Mutable)Reduction
Spliterator
x0x2
x0
x3peek(System.out::print)
![Page 194: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/194.jpg)
Visualizing Stream Operations
x2
x1
x3
y0y1y2y3
Intermediate Op(s)
(Mutable)Reduction
Spliterator
x0x2
x0
x3peek(System.out::print)
![Page 195: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/195.jpg)
Visualizing Stream Operations
x2
x1
x3
Intermediate Op(s)
(Mutable)Reduction
Spliterator
x0x2
x0
x3peek(System.out::print)
![Page 196: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/196.jpg)
#parallelready @mauricenaftalin
What Does Parallel-Ready Mean?
![Page 197: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/197.jpg)
#parallelready @mauricenaftalin
What Does Parallel-Ready Mean?
– sequential execution is no longer the default
![Page 198: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/198.jpg)
#parallelready @mauricenaftalin
What Does Parallel-Ready Mean?
– sequential execution is no longer the default– it’s still often more efficient, but that will change
![Page 199: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/199.jpg)
#parallelready @mauricenaftalin
What Does Parallel-Ready Mean?
– sequential execution is no longer the default– it’s still often more efficient, but that will change– code must be agnostic about its execution mode
![Page 200: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/200.jpg)
#parallelready @mauricenaftalin
What Does Parallel-Ready Mean?
– sequential execution is no longer the default– it’s still often more efficient, but that will change– code must be agnostic about its execution mode– operations in the Stream API have equivalent effect
![Page 201: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/201.jpg)
#parallelready @mauricenaftalin
What Does Parallel-Ready Mean?
– sequential execution is no longer the default– it’s still often more efficient, but that will change– code must be agnostic about its execution mode– operations in the Stream API have equivalent effect- sequential or parallel
![Page 202: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/202.jpg)
#parallelready @mauricenaftalin
What Does Parallel-Ready Mean?
– sequential execution is no longer the default– it’s still often more efficient, but that will change– code must be agnostic about its execution mode– operations in the Stream API have equivalent effect- sequential or parallel
- may be equally non-deterministic
![Page 203: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/203.jpg)
#parallelready @mauricenaftalin
What Does Parallel-Ready Mean?
– sequential execution is no longer the default– it’s still often more efficient, but that will change– code must be agnostic about its execution mode– operations in the Stream API have equivalent effect- sequential or parallel
- may be equally non-deterministic
– decouples execution mode from functional behavior
![Page 204: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/204.jpg)
#parallelready @mauricenaftalin
What Does Parallel-Ready Mean?
– sequential execution is no longer the default– it’s still often more efficient, but that will change– code must be agnostic about its execution mode– operations in the Stream API have equivalent effect- sequential or parallel
- may be equally non-deterministic
– decouples execution mode from functional behavior– parallel-ready is not only about parallel execution!
![Page 205: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/205.jpg)
#parallelready @mauricenaftalin
What Does Parallel-Ready Mean?
– sequential execution is no longer the default– it’s still often more efficient, but that will change– code must be agnostic about its execution mode– operations in the Stream API have equivalent effect- sequential or parallel
- may be equally non-deterministic
– decouples execution mode from functional behavior– parallel-ready is not only about parallel execution!- or even mainly, for now
![Page 206: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/206.jpg)
#parallelready @mauricenaftalin
So, Is Your Code Parallel-Ready?
Most unlikely. But it can be!
The Stream API provides a framework and a lot of support for writing PR code. You just need to co-operate with it.
- it depends on you behaving sanely
![Page 207: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/207.jpg)
#parallelready @mauricenaftalin
Using Streams Sanely
Rules in the JavaDoc for java.util.stream Stateless behavior:- avoid stateful behavioral parameters
- “one whose result depends on any state which might change during the execution of the stream pipeline”
...
![Page 208: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/208.jpg)
#parallelready @mauricenaftalin
Using Streams Sanely
Rules in the JavaDoc for java.util.stream Stateless behavior:- avoid stateful behavioral parameters
- “one whose result depends on any state which might change during the execution of the stream pipeline”
...
Safely
![Page 209: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/209.jpg)
#parallelready @mauricenaftalin
Using Streams Sanely
Rules in the JavaDoc for java.util.stream ...Non-interference- don’t modify a stream’s data source during execution
- you may if it’s a thread-safe class
Don’t rely on side-effects- use for debugging only
![Page 210: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/210.jpg)
#parallelready @mauricenaftalin
Using Streams Sanely
Rules in the JavaDoc for java.util.stream ...Non-interference- don’t modify a stream’s data source during execution
- you may if it’s a thread-safe class
Don’t rely on side-effects- use for debugging only
Safely
![Page 211: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/211.jpg)
#parallelready @mauricenaftalin
Conclusion
The Streams API offers a framework for- programming bulk data operations in parallel-ready style
- easy parallellism, with thread-safe access to non-threadsafe containers
All we have to do is co-operate with it!Parallel-Ready code starts today!
![Page 212: Parallel-Ready Java Code: Managing Mutation in an Imperative Language](https://reader031.vdocuments.net/reader031/viewer/2022030313/58a7f4ad1a28abfa488b4cc3/html5/thumbnails/212.jpg)
#parallelready @mauricenaftalin
Resources
www.lambdafaq.orgwww.masteringlambdas.org