Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods...

44
Java Streams

Transcript of Java StreamsJava Streams: What are they (not)? What is this… “functional” programming? methods...

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

Streams in code...

●●