More Stream Processing

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

Java 8 Stream API Raj Thavamani Application Developer / Java Group Biomedical Informatics.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Chapter 4 Making Decisions
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
Functional Programming Shane Carr CSE 232, September 4, 2015.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 10.
Expressions An expression is a series of variables, operators, and method calls (constructed according to the syntax of the language) that evaluates to.
ITIP © Ron Poet Lecture 12 1 Finding Out About Objects.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
Chapter Making Decisions 4. Relational Operators 4.1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
JAVA 8 AND FUNCTIONAL PROGRAMMING. What is Functional Programming?  Focus on Mathematical function computations  Generally don’t think about “state”
Lambdas and Streams. Stream manipulation Class Employee represents an employee with a first name, last name, salary and department and provides methods.
Functional Processing of Collections (Advanced) 6.0.
Building Java Programs Chapter 19 Functional Programming with Java 8 Copyright (c) Pearson All rights reserved.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Lambdas & Streams Laboratory
Information and Computer Sciences University of Hawaii, Manoa
INC 161 , CPE 100 Computer Programming
Stream API, Error handling
Programming Languages
Introduction to the C Language
Functional Programming and Stream API
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Comp 205: Comparative Programming Languages
Functional Processing of Collections (Advanced)
Java Algorithms.
Engineering Innovation Center
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Data Types
Advanced Topics in Concurrency and Reactive Programming: ReactiveX
امکانات جدید جاوا در نسخه 8 Java 8 Features
Notable Java 8 New Features – Not including Lambda S Marvasti
תוכנה 1 בשפת Java שיעור מספר 10: חידושים ב Java 8
תוכנה 1 בשפת Java שיעור מספר 11: חידושים ב Java 8
Programming Languages
Review for Final Exam.
FP Foundations, Scheme In Text: Chapter 14.
Advanced Topics in Concurrency and Reactive Programming: Functional Programming Majeed Kassis.
Building Java Programs Chapter 19
© 2016 Pearson Education, Ltd. All rights reserved.
Building Java Programs Chapter 19
Effective Java, 3rd Edition Chapter 7: Lambdas and Streams
CISC/CMPE320 - Prof. McLeod
Standard Version of Starting Out with C++, 4th Edition
Review for Final Exam.
Exceptions, Templates, and the Standard Template Library (STL)
Java Programming Review 1
Computer Science 312 Composing Functions Streams in Java 1.
Computer Science 312 Lambdas in Java 1.
Collectors and Terminal Operations
Arrays October 6, 2006 ComS 207: Programming I (in Java)
Computer Science 312 What’s New in Java 8? 1.
Introduction to Programming
Chapter 10: Compilers and Language Translation
Character Arrays char string1[] = “first”;
Chapter 3 Debugging Section 3.4
Chapter 2 Primitive Data Types and Operations
Topic 25 - more array algorithms
Laziness and Its Consequences
Advanced Topics in Functional and Reactive Programming: Java Functional Interfaces Majeed Kassis.
Introduction to C CS 3410.
Presentation transcript:

More Stream Processing Computer Science 312 More Stream Processing 1

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..]

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());

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?

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

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

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

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

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

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)

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)

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));

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

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();

Streams from Arrays and Files

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);

Terminal Operations Chapter 6 For next time Terminal Operations Chapter 6