COS 260 DAY 10 Tony Gauvin.

Slides:



Advertisements
Similar presentations
Chapter 10 Introduction to Arrays
Advertisements

Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Grouping Objects Arrays and for loops. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Fixed-Size Collections.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Chapter Day 14. © 2007 Pearson Addison-Wesley. All rights reserved5-2 Agenda Day 14 Problem set 3 posted  10 problems from chapters 5 & 6  Due in 11.
Grouping Objects 2 Collections and the for-each loop Collections and the while loop.
Programming with Collections Grouping & Looping - Collections and Iteration Week 7.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators.
Invitation to Computer Science, Java Version, Second Edition.
1 COS 260 DAY 2 Tony Gauvin. 2 Agenda Questions? Class roll call Blackboard Web Resources Objects and classes 1 st Mini quiz on chap1 terms and concepts.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0.
Grouping objects Introduction to collections 5.0.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
© 2006 Pearson Education 1 More Operators  To round out our knowledge of Java operators, let's examine a few more  In particular, we will examine the.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
1 COS 260 DAY 10 Tony Gauvin. 2 Agenda Questions? 4 th Mini quiz Today –Chapter 4 Assignment 2 Due Capstone Discussion Proposals Due Oct 15 No class on.
1 COS 260 DAY 14 Tony Gauvin. 2 Agenda Questions? 6 th Mini quiz graded  Oct 29 –Chapter 6 Assignment 4 will be posted later Today –First two problems.
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Grouping objects Arrays. 2 Fixed-size collections The maximum collection size may be pre-determined with an upper limit Array is an fixed-size collection.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Collections and Iteration Week 13.  Collections  ArrayList objects  Using loops with collections Collections and Iteration CONCEPTS COVERED THIS WEEK.
1 COS 260 DAY 22 Tony Gauvin. 2 Agenda Questions? 9 th Mini Quiz corrected –Good results Assignment 5 Not corrected yet Assignment 6 Posted (one more)
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Fixed-sized collections Introduction to arrays 6.0.
Functional Processing of Collections (Advanced) 6.0.
6.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
1 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. for-each PROS easy to use access to ALL items one-by-one ability to change the state.
CS314 – Section 5 Recitation 9
for-each PROS CONS easy to use access to ALL items one-by-one
Objects First with Java CITS1001 week 4
Objects First with Java A Practical Introduction using BlueJ
Objects First with Java Introduction to collections
Functional Processing of Collections (Advanced)
COS 260 DAY 17 Tony Gauvin.
COS 260 DAY 23 Tony Gauvin.
COS 260 DAY 19 Tony Gauvin.
COS 260 DAY 9 Tony Gauvin.
Advanced Programming Behnam Hatami Fall 2017.
COS 260 DAY 13 Tony Gauvin.
COS 260 DAY 6 Tony Gauvin.
COS 260 DAY 19 Tony Gauvin.
COS 260 DAY 27 Tony Gauvin.
COS 260 DAY 11 Tony Gauvin.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
COS 260 DAY 2 Tony Gauvin.
COS 260 DAY 18 Tony Gauvin.
FP Foundations, Scheme In Text: Chapter 14.
COS 260 DAY 8 Tony Gauvin.
COS 260 DAY 16 Tony Gauvin.
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
COS 260 DAY 19 Tony Gauvin.
COS 260 DAY 11 Tony Gauvin.
Collections and iterators
COS 260 DAY 23 Tony Gauvin.
COS 260 DAY 14 Tony Gauvin.
COS 260 DAY 4 Tony Gauvin.
Arrays October 6, 2006 ComS 207: Programming I (in Java)
COS 260 DAY 23 Tony Gauvin.
Collections and iterators
Chapter 4: Loops and Iteration
Presentation transcript:

COS 260 DAY 10 Tony Gauvin

Agenda Questions Assignment 2 Due Assignment 3 posted soon Exercise 5.29 on Page 197 will be problem 1 Miniquiz 4 is Today Password Truthisalwaysstrange Next mini quiz will be on chapter 5 & 6 on Friday the 13TH after break Functional Processing of Collections

Capstone Project Capstone Project Description Fall 2017.pdf October 2 Proposal Due E-mailed to Tony in Blackboard October 27 Progress Report E-mailed to Tony in Blackboard November 16 Progress Report E-mailed to Tony in Blackboard November 30 Progress Report E-mailed to Tony in Blackboard December 12@8AM All Deliverables & Presentation Due Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Functional Processing of Collections (Advanced) 6.0

Overview An alternative look at collections and iteration. A functional style of programming. Complements the imperative style used so far. Streams & lambda notation. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

First introduced in Java 8 Lambdas borrow well-established techniques from the world of functional languages, such as Lisp, Haskell, Erlang, etc. Lambdas require additional syntax in the language. (-> operator) Stream operations provide an alternative means of implementing tasks associated with iteration over collections. Some existing library classes have been retro-fitted to support streams and lambda. Streams often involve multi-stage processing of data in the form of a pipeline of operations. (from Unix | pipe cmd) © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Lambdas Bear a strong similarity to simple methods. They have: A return type. Parameters. A body. They don’t have a name (anonymous methods). They have no associated object. They can be passed as parameters: As code to be executed by the receiving method. http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Lambda-QuickStart/index.html © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Example scenario Animal monitoring in a national park (animal-monitoring project). Spotters send back reports of animals they have seen (Sighting objects). Base collates sighting reports to check on population levels. Review version 1 of the project, which is implemented in a familiar (imperative) style: The AnimalMonitoring class has methods to: List all sighting records; List sightings of a particular animal; Identify animals that could be endangered; Calculate sighting totals; Etc. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Method and lambda equivalent public void printSighting(Sighting record) { System.out.println(record.getDetails()); } (Sighting record) -> { System.out.println(record.getDetails()); } © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Processing a collection – the usual approach loop (for each element in the collection): get one element; do something with the element; end loop for(Sighting record : sightings) { printSighting(record); } © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Processing a whole collection collection.doThisForEachElement(some code); sightings.forEach((Sighting record) -> { System.out.println(record.getDetails()); } ); © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Reduced lambda syntax: infer type sightings.forEach((record) -> { System.out.println(record.getDetails()); } ); © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Reduced lambda syntax: single parameter sightings.forEach(record -> { System.out.println(record.getDetails()); } ); © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Reduced lambda syntax: single statement sightings.forEach( record -> System.out.println(record.getDetails()) ); Ex 5.5 in text © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Streams Streams are often created from the contents of a collection. An ArrayList is not a stream, but its stream() method creates a stream of its contents. Elements in a stream are not accessed via an index, but usually sequentially (as coming from an end of a pipe) . The contents and ordering of the stream cannot be changed – changes require the creation of a new stream. A stream could potentially be infinite! Elements in a stream can be processed in parallel. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Filters, maps and reductions Streams are immutable, so operations often result in a new stream. There are three common types of operation: Filter: select items from the input stream to pass on to the output stream. Map: replace items from the input stream with different items in the output stream. Reduce: collapse the multiple elements of the input stream into a single element. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Filter © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Map

Reduce © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

A pipeline of operations filter(name is elephant).map(count).reduce(add up) © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Pipelines Pipelines start with a source. Operations are either: Intermediate, or Terminal. Intermediate operations produce a new stream as output. Terminal operations are the final operation in the pipeline. They might have a void return type. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Filters Filters require a Boolean lambda as a parameter. A Boolean lambda is called a predicate. If the predicate returns true for an element of the input stream then that element is passed on to the output stream; otherwise it is not. (Filters determine which elements to retain.) Some predicates: s -> s.getAnimal().equals("Elephant”) s -> s.getCount() > 0 (s) -> true // Pass on all elements. (s) -> false // Pass on none. Example: print details of only the Elephant sightings. sightings.stream() .filter(s -> "Elephant".equals(s.getAnimal())) .forEach(s -> System.out.println(s.getDetails())); EX 5.10 on page 192 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

The map method The type of the objects in the output stream is often (but not necessarily) different from the type in the input stream. E.g., extracting just the details String from a Sighting: sightings.stream() .map(sighting -> sighting.getDetails()) .forEach(details -> System.out.println(details)); EX 5.15 on page 193 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

The reduce method More complex than both filter and map. Its task is to ‘collapse’ a multi-element stream to a single ‘value’. It takes two parameters: a value and a lambda: reduce(start, (acc, element) -> acc + element) The first parameter is a starting value for the final result. The lambda parameter itself takes two parameters: an accumulating value for the final result, and an element of the stream. The lambda determines how to merge an element with the accumulating value. The lambda’s result will be used as the acc parameter of the lambda for the next element of the stream. The start value is used as the first acc parameter that is paired with the first element of the stream. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

The reduce method – a comparative example Error in textbook page 194 & 195 return sightings.stream() .filter(sighting -> animal.equals(sighting.getAnimal()) .map(sighting -> sighting.getCount()) .reduce(0, (total, count) -> total + count); Initial value int total = 0; for(Sighting sighting : sightings) { if(animal.equals(sighting.getAnimal())) { int count = sighting.getCount(); total = total + count; } Accumulation Ex 5.19 on page 195 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Removal from a collection using a predicate lambda /** * Remove from the sightings list all of * those records with a count of zero. */ public void removeZeroCounts() { sightings.removeIf( sighting -> sighting.getCount() == 0); } Ex 5.22 on page 196 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Summary Streams and lambdas are an important and powerful new feature of Java. They are likely to increase in importance over the coming years. Expect collection processing to move in that direction. Lambdas are widely used in other areas, too; e.g. GUI building for event handlers. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Summary A collection can be converted to a stream for processing in a pipeline. Typical pipeline operations are filter, map and reduce. Parallel processing of streams is possible. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.