java streamsjava streams: what are they (not)? what is this… “functional” programming? methods...
TRANSCRIPT
![Page 1: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/1.jpg)
Java Streams
![Page 2: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/2.jpg)
Java Streams: What are they (not)?
●○
●
![Page 3: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/3.jpg)
What is this… “functional” programming?
● methods
○
![Page 4: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/4.jpg)
A Functional Programming Parable 1
2
![Page 5: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/5.jpg)
But what is a “Stream”?
●●
○○○
●●
![Page 6: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/6.jpg)
How to make streams?
●○ import java.util.stream.*
●○ String[] menuItemNames = {"Grits", "Pancakes", "Burrito"};
Stream.of(menuItemNames); // returns a stream, so needs “=” before it
○ Stream.of("Hedgehog", "Kitten", "Fox"); // arbitrary argument count
● stream() Collection
○ List<String> menuItemNameList = Arrays.asList(menuItemNames);
○ menuItemNameList.stream();
●
![Page 7: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/7.jpg)
forEach
● Intuition →
●
●
● Stream.of(users).forEach(e -> e.logOut());
○
![Page 8: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/8.jpg)
forEach
●○ Stream.of("hello", "world").forEach(word -> System.out.println(word));
○
●○ Stream.of("hello", "world").forEach(System.out::println);
○ class::method
![Page 9: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/9.jpg)
map
Some More Common Stream Operations
filter sorted
limit distinct collect
![Page 10: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/10.jpg)
collect (Basics)
●●
○ Stream<Integer> stream = Arrays.asList(3,2,1,5,4,7).stream();
●○ List<Integer> list = stream.collect(Collectors.toList());
○ Set<Integer> list = stream.collect(Collectors.toSet());
●○ Collectors.groupingBy( ) Collectors.reducing( );
![Page 11: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/11.jpg)
map
● Intuition →●
●List<Integer> numbersTripled =
numbers.stream().map(x -> x*3).collect(toList());
T → → K
Stream<T> .map ( ) → Stream<K>
![Page 12: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/12.jpg)
map
List<Integer> numbersTripled =numbers.stream().map(x -> x*3).collect(toList());
![Page 13: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/13.jpg)
map
List<Integer> numbersTripled =numbers.stream().map(x -> x*3).collect(toList());
3
→→
![Page 14: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/14.jpg)
map
List<Integer> numbersTripled =numbers.stream().map(x -> x*3).collect(toList());
3 6
→→
![Page 15: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/15.jpg)
map
List<Integer> numbersTripled =numbers.stream().map(x -> x*3).collect(toList());
3 6 9
→→
![Page 16: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/16.jpg)
map
List<Integer> numbersTripled =numbers.stream().map(x -> x*3).collect(toList());
3 6 9 12
→→
![Page 17: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/17.jpg)
map
List<Integer> numbersTripled =numbers.stream().map(x -> x*3).collect(toList());
3 6 9 12 15
→→
![Page 18: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/18.jpg)
map
List<Integer> numbersTripled =numbers.stream().map(x -> x*3).collect(toList());
3 6 9 12 15 18
→→
![Page 19: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/19.jpg)
map
●.map(x -> x/2)
●.map(x -> { … some code … return something; })
●.map(String::toUpperCase)
![Page 20: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/20.jpg)
filter
● Intuition →●●
List<Integer> goodYears = years.stream().filter(y -> y != 2020).collect(toList());
![Page 21: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/21.jpg)
filter
List<Integer> goodYears = years.stream().filter(y -> y != 2020).collect(toList());
![Page 22: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/22.jpg)
filter
List<Integer> goodYears = years.stream().filter(y -> y != 2020).collect(toList());
y y != 2020
![Page 23: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/23.jpg)
filter
List<Integer> goodYears = years.stream().filter(y -> y != 2020).collect(toList());
2000
y != 2020
y y != 2020
![Page 24: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/24.jpg)
filter
List<Integer> goodYears = years.stream().filter(y -> y != 2020).collect(toList());
2000
y != 2020
y y != 2020
![Page 25: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/25.jpg)
filter
List<Integer> goodYears = years.stream().filter(y -> y != 2020).collect(toList());
2005
y != 2020
y y != 2020
![Page 26: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/26.jpg)
filter
List<Integer> goodYears = years.stream().filter(y -> y != 2020).collect(toList());
2010
y != 2020
y y != 2020
![Page 27: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/27.jpg)
filter
List<Integer> goodYears = years.stream().filter(y -> y != 2020).collect(toList());
2015
y != 2020
y y != 2020
![Page 28: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/28.jpg)
filter
List<Integer> goodYears = years.stream().filter(y -> y != 2020).collect(toList());
2020
y != 2020
y y != 2020
![Page 29: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/29.jpg)
filter
List<Integer> goodYears = years.stream().filter(y -> y != 2020).collect(toList());
2025
y != 2020
y y != 2020
![Page 30: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/30.jpg)
filter
List<Integer> goodYears = years.stream().filter(y -> y != 2020).collect(toList());
y != 2020
![Page 31: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/31.jpg)
filter
●○ List<Integer> leapYears =
years.stream().filter(y -> { if (y % 400 == 0) return true; if (y % 100 == 0) return false; if (y % 4 == 0) return true; return false; }).collect(toList());
●● Predicate<T> boolean test(T t)
![Page 32: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/32.jpg)
sorted
var numbers = Arrays.asList(3, 2, 1, 5, 4, 7);numbers.stream().sorted().forEach(System.out::println);
![Page 33: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/33.jpg)
distinct
var numbers = Arrays.asList(3,3,1,1,4,7,8);numbers.stream().distinct().forEach(System.out::println);
![Page 34: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/34.jpg)
limit
var numbers = Arrays.asList(3,2,2,3,7,3,5);numbers.stream().limit(4).forEach(System.out::println);
![Page 35: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/35.jpg)
collect (Reductions)
● Stream.collect()
●
●reduced aggregate vote count
● reducedaverage height
● reduced maximum (oldest) age
![Page 36: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/36.jpg)
collect (Reductions)
●List<Integer> teamHeights = List.of(73, 68, 75, 77, 74);
● Collectors.reducing
●
int totalHeight = teamHeights.stream().collect(Collectors.reducing(0, (accumulator, curr) -> (accumulator + curr))
);
● System.out.println(totalHeight);
○ 367
![Page 37: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/37.jpg)
collect (Reductions)
Collectors.reducing(0, (accumulator, curr) -> (accumulator + curr))
[ 73, 68, 75, 77, 74 ]
^
Accumulator value: 0
Current stream element: 73
New accumulator value: 73
![Page 38: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/38.jpg)
collect (Reductions)
Collectors.reducing(0, (accumulator, curr) -> (accumulator + curr))
[ 73, 68, 75, 77, 74 ]
^
Accumulator value: 73
Current stream element: 68
New accumulator value: 141
![Page 39: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/39.jpg)
collect (Reductions)
Collectors.reducing(0, (accumulator, curr) -> (accumulator + curr))
[ 73, 68, 75, 77, 74 ]
^
Accumulator value: 141
Current stream element: 75
New accumulator value: 216
![Page 40: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/40.jpg)
collect (Reductions)
Collectors.reducing(0, (accumulator, curr) -> (accumulator + curr))
[ 73, 68, 75, 77, 74 ]
^
Accumulator value: 216
Current stream element: 77
New accumulator value: 293
![Page 41: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/41.jpg)
collect (Reductions)
Collectors.reducing(0, (accumulator, curr) -> (accumulator + curr))
[ 73, 68, 75, 77, 74 ]
^
Accumulator value: 293
Current stream element: 74
New accumulator value: 367 (Final result)
![Page 42: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/42.jpg)
count
Some More Common Stream Operations
skip findFirst
toArray flatMap peek
![Page 43: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/43.jpg)
Restaurant Example
Collection Stream filter sorted limitdistinctmap
Customer asks:
Stream Stream Stream Stream Stream
![Page 44: Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods A Functional Programming Parable 1 2 But what is a “Stream”? How to make streams?](https://reader033.vdocuments.site/reader033/viewer/2022053119/60a163e8b88a4d4c27156d7b/html5/thumbnails/44.jpg)
Streams in code...
●●