Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Stream Processing

Similar presentations


Presentation on theme: "More Stream Processing"— Presentation transcript:

1 More Stream Processing
Computer Science 312 More Stream Processing 1

2 Taking and Dropping In Haskell: Prelude> take 3 [1..10] [1,2,3]
Prelude> drop 3 [1..10] [4,5,6,7,8,9,10] Prelude> take 3 [1..]

3 Taking and Dropping In Java: List<Dish> dishes = menu.stream()
.filter(d -> d.getCalories() > 300) .limit(3) // Like take .collect(toList()); List<Dish> dishes = menu.stream() .filter(d -> d.getCalories() > 300) .skip(2) // Like drop .collect(toList());

4 Chained Mappings A single mapping: Consecutive mappings:
List<String> words = Arrays.asList("Java8", "Lambdas", "In", "Action"); List<Integer> wordLengths = words.stream() .map(String::length) .collect(toList()); Consecutive mappings: List<Integer> dishNameLengths = menu.stream() .map(Dish::getName) .map(String::length) .collect(toList()); Are three complete streams generated in the second code segment?

5 Flattening during Map Try to obtain a list of unique characters from a list of words: words.stream() .map(word -> word.split("")) .distinct() .collect(toList()); Actually sends a stream of arrays char out of the map

6 Flattening during Map Create a stream of characters from an array of words: String[] arrayOfWords = {"Goodbye", "World"}; Stream<String> streamOfwords = Arrays.stream(arrayOfWords); Do this on the stream of words resulting from the first map: But now we get a stream of streams of characters from the second map

7 Flattening during Map Use flatMap for the second mapping, which flattens the stream of streams into a single stream:

8 Detecting Items Several operations for detecting or finding items in a stream: anyMatch allMatch noneMatch findFirst findAny The first three are Boolean ops, and the last two return an item

9 Sample Boolean Detections
if(menu.stream().anyMatch(Dish::isVegetarian)){ System.out.println("The menu is kind of vegetarian friendly!!"); } boolean isHealthy = menu.stream() .allMatch(d -> d.getCalories() < 1000); .noneMatch(d -> d.getCalories() >= 1000); They all use short-circuit evaluation in the stream for optimization

10 Sample Item Detection Optional<Dish> dish = menu.stream() .filter(Dish::isVegetarian) .findAny(); Optional is Java’s version of Haskell’s Maybe type Optional<T> methods: isPresent() ifPresent(Consumer<T> block) T get() T orElse(T other)

11 Sample Item Detection Optional is Java’s version of Haskell’s Maybe type Optional<T> methods: isPresent() ifPresent(Consumer<T> block) T get() T orElse(T other)

12 Reducing (Folding) int sum = numbers.stream().reduce(0, (a, b) -> a + b); int product = numbers.stream().reduce(1, (a, b) -> a * b); int sum = numbers.stream().reduce(0, Integer::sum); Optional<Integer> sum = numbers.stream().reduce((a, b) -> (a + b));

13 Reducing (Folding) Optional<Integer> min = numbers.stream().reduce(Integer::min); Optional<Integer> min = numbers.stream().reduce((x,y)-> x < y ? x : y); Reduction operation must be associative and not include side effects, for parallelization

14 Streams from Values Stream<String> stream = Stream.of("Java 8 ", "Lambdas ", "In ", "Action"); stream.map(String::toUpperCase).forEach(System.out::println); Stream<String> emptyStream = Stream.empty();

15 Streams from Arrays and Files

16 Infinite Streams In Haskell: In Java:
take 10 (map (\n -> n + 2) [1..]) In Java: Stream.iterate(0, n -> n + 2) .limit(10) .forEach(System.out::println);

17 Terminal Operations Chapter 6
For next time Terminal Operations Chapter 6


Download ppt "More Stream Processing"

Similar presentations


Ads by Google