Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lambdas & Streams Laboratory

Similar presentations


Presentation on theme: "Lambdas & Streams Laboratory"— Presentation transcript:

1

2 Lambdas & Streams Laboratory
Stuart Marks Simon Ritter Angela Caicedo Oracle Corp. This is a Title Slide with Java FY15 Theme slide ideal for including the Java Theme with a brief title, subtitle and presenter information. To customize this slide with your own picture: Right-click the slide area and choose Format Background from the pop-up menu. From the Fill menu, click Picture and texture fill. Under Insert from: click File. Locate your new picture and click Insert. To copy the Customized Background from Another Presentation on PC Click New Slide from the Home tab's Slides group and select Reuse Slides. Click Browse in the Reuse Slides panel and select Browse Files. Double-click the PowerPoint presentation that contains the background you wish to copy. Check Keep Source Formatting and click the slide that contains the background you want. Click the left-hand slide preview to which you wish to apply the new master layout. Apply New Layout (Important): Right-click any selected slide, point to Layout, and click the slide containing the desired layout from the layout gallery. Delete any unwanted slides or duplicates. To copy the Customized Background from Another Presentation on Mac Click New Slide from the Home tab's Slides group and select Insert Slides from Other Presentation… Navigate to the PowerPoint presentation file that contains the background you wish to copy. Double-click or press Insert. This prompts the Slide Finder dialogue box. Make sure Keep design of original slides is unchecked and click the slide(s) that contains the background you want. Hold Shift key to select multiple slides. Apply New Layout (Important): Click Layout from the Home tab's Slides group, and click the slide containing the desired layout from the layout gallery. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

3 Lambdas and Functions Library Review

4 Lambda Expressions Lambda expression is an anonymous function
Think of it like a method But not associated with a class Can be used wherever you would use an anonymous inner class Single abstract method type Syntax ([optional-parameters]) -> body Types can be inferred (parameters and return type)

5 Lambda Examples SomeList<Student> students = ... double highestScore = students.stream(). filter(Student s -> s.getGradYear() == 2011). map(Student s -> s.getScore()). max(); Question: how are we going to get there with real collections?

6 Method References Method references let us reuse a method as a lambda expression FileFilter x = new FileFilter() { public boolean accept(File f) { return f.canRead(); } }; FileFilter x = (File f) -> f.canRead(); FileFilter x = File::canRead;

7 The Stream Class java.util.stream Stream<T>
A sequence of elements supporting sequential and parallel operations A Stream is opened by calling: Collection.stream() Collection.parallelStream() Many Stream methods return Stream objects Very simple (and logical) method chaining

8 Stream Basics Using a Stream means having three things A source
Something that creates a Stream of objects Zero or more intermediate objects Take a Stream as input, produce a Stream as output Potentially modify the contents of the Stream (but don’t have to) A terminal operation Takes a Stream as input Consumes the Stream, or generates some other type of output

9 Stream Usage Multiple operations available
collect, filter, count, skip, limit, sorted map (and map to types, e.g. mapToInt) flatMap maps each element in a Stream to possibly multiple elements e.g. flatMap(line -> Stream.of(line.split(REGEXP)); List<String> names = Arrays.asList(“Bob”, “Alice”, “Charlie”); System.out.println(names. stream(). filter(e -> e.getLength() > 4). findFirst(). get());

10 java.util.function Package
Predicate<T> Determine if the input of type T matches some criteria Consumer<T> Accept a single input argumentof type T, and return no result Function<T, R> Apply a function to the input type T, generating a result of type R Plus several more

11 The iterable Interface
Used by most collections One method forEach() The parameter is a Consumer wordList.forEach(s -> System.out.println(s)); wordList.forEach(System.out::println);

12 Files and Lines of Text BufferedReader has new method
Stream<String> lines() HINT: Test framework creates a BufferedReader for you

13 Maps and FlatMaps Map FlatMap Map Values in a Stream 1 to 1 mapping
Input Stream Output Stream Map 1 to many mapping Input Stream Output Stream FlatMap

14 Useful Stream Methods collect (terminal) filter (intermediate)
count (terminal) skip, limit (intermediate) max (terminal) getAsInt (terminal)

15 Getting Started Open the LambdasHOL project in NetBeans
The exercises are configured as tests Edit the tests Remove annotation Run the tests (Ctrl F6, or from the menu) Make the tests pass Simple!

16 Let’s Go!

17


Download ppt "Lambdas & Streams Laboratory"

Similar presentations


Ads by Google