SlideShare ist ein Scribd-Unternehmen logo
1 von 17
Lambdas & Streams Laboratory 
Stuart Marks 
Simon Ritter 
Angela Caicedo 
Oracle Corp. 
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
Lambdas and Functions 
Copyright © 2012, Oracle and/or its affiliates. 3 All rights reserved. 
Library Review
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) 
Copyright © 2012, Oracle and/or its affiliates. 4 All rights reserved.
Lambda Examples 
SomeList<Student> students = ... 
double highestScore = 
students.stream(). 
filter(Student s -> s.getGradYear() == 2011). 
map(Student s -> s.getScore()). 
max(); 
Copyright © 2012, Oracle and/or its affiliates. 5 All rights reserved.
Method References 
• Method references let us reuse a method as a lambda expression 
FileFilter x = new FileFilter() { 
public boolean accept(File f) { 
FileFilter x = (File f) -> f.canRead(); 
FileFilter x = File::canRead; 
Copyright © 2012, Oracle and/or its affiliates. 6 All rights reserved. 
return f.canRead(); 
} 
};
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 
Copyright © 2012, Oracle and/or its affiliates. 7 All rights reserved.
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 
Copyright © 2012, Oracle and/or its affiliates. 8 All rights reserved.
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()); 
Copyright © 2012, Oracle and/or its affiliates. 9 All rights reserved.
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 
Copyright © 2012, Oracle and/or its affiliates. 10 All rights reserved.
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); 
Copyright © 2012, Oracle and/or its affiliates. 11 All rights reserved.
Files and Lines of Text 
 BufferedReader has new method 
– Stream<String> lines() 
 HINT: Test framework creates a BufferedReader for you 
Copyright © 2012, Oracle and/or its affiliates. 12 All rights reserved.
Maps and FlatMaps 
Map Values in a Stream 
Map 
FlatMap 
Input Stream 
Input Stream 
Copyright © 2012, Oracle and/or its affiliates. 13 All rights reserved. 
1 to 1 mapping 
1 to many mapping 
Output Stream 
Output Stream
Useful Stream Methods 
 collect (terminal) 
 filter (intermediate) 
 count (terminal) 
 skip, limit (intermediate) 
 max (terminal) 
 getAsInt (terminal) 
Copyright © 2012, Oracle and/or its affiliates. 14 All rights reserved.
Getting Started 
 Open the LambdasHOL project in NetBeans 
 The exercises are configured as tests 
 Edit the tests 
– Remove the @Ignore annotation 
 Run the tests (Ctrl F6, or from the menu) 
 Make the tests pass 
 Simple! 
Copyright © 2012, Oracle and/or its affiliates. 15 All rights reserved.
Copyright © 2012, Oracle and/or its affiliates. 16 All rights reserved. 
Let’s Go!
Copyright © 2012, Oracle and/or its affiliates. 17 All rights reserved.

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (15)

Apouc 2014-java-8-create-the-future
Apouc 2014-java-8-create-the-futureApouc 2014-java-8-create-the-future
Apouc 2014-java-8-create-the-future
 
Functional Programming With Lambdas and Streams in JDK8
 Functional Programming With Lambdas and Streams in JDK8 Functional Programming With Lambdas and Streams in JDK8
Functional Programming With Lambdas and Streams in JDK8
 
Introduction to new features in java 8
Introduction to new features in java 8Introduction to new features in java 8
Introduction to new features in java 8
 
Java SE 8 - New Features
Java SE 8 - New FeaturesJava SE 8 - New Features
Java SE 8 - New Features
 
Lambdas & Streams
Lambdas & StreamsLambdas & Streams
Lambdas & Streams
 
Streams in Java 8
Streams in Java 8Streams in Java 8
Streams in Java 8
 
Java 8
Java 8Java 8
Java 8
 
Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...
Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...
Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...
 
How Green are Java Best Coding Practices? - GreenDays @ Rennes - 2014-07-01
How Green are Java Best Coding Practices? - GreenDays @ Rennes - 2014-07-01How Green are Java Best Coding Practices? - GreenDays @ Rennes - 2014-07-01
How Green are Java Best Coding Practices? - GreenDays @ Rennes - 2014-07-01
 
Java 8 streams
Java 8 streamsJava 8 streams
Java 8 streams
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
 
Chap2java5th
Chap2java5thChap2java5th
Chap2java5th
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and Variables
 
Chap1java5th
Chap1java5thChap1java5th
Chap1java5th
 
Best practices in Java
Best practices in JavaBest practices in Java
Best practices in Java
 

Andere mochten auch

Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8Simon Ritter
 
Oracle Keynote from JMagghreb 2014
Oracle Keynote from JMagghreb 2014Oracle Keynote from JMagghreb 2014
Oracle Keynote from JMagghreb 2014Simon Ritter
 
Lambdas And Streams in JDK8
Lambdas And Streams in JDK8Lambdas And Streams in JDK8
Lambdas And Streams in JDK8Simon Ritter
 
Java: Create The Future Keynote
Java: Create The Future KeynoteJava: Create The Future Keynote
Java: Create The Future KeynoteSimon Ritter
 
Functional programming with_jdk8-s_ritter
Functional programming with_jdk8-s_ritterFunctional programming with_jdk8-s_ritter
Functional programming with_jdk8-s_ritterSimon Ritter
 
Project Jigsaw in JDK9
Project Jigsaw in JDK9Project Jigsaw in JDK9
Project Jigsaw in JDK9Simon Ritter
 
JDK 9: Big Changes To Make Java Smaller
JDK 9: Big Changes To Make Java SmallerJDK 9: Big Changes To Make Java Smaller
JDK 9: Big Changes To Make Java SmallerSimon Ritter
 
It's Java Jim, But Not As We Know It!
It's Java Jim, But Not As We Know It!It's Java Jim, But Not As We Know It!
It's Java Jim, But Not As We Know It!Simon Ritter
 
JDK 9: Big Changes To Make Java Smaller
JDK 9: Big Changes To Make Java SmallerJDK 9: Big Changes To Make Java Smaller
JDK 9: Big Changes To Make Java SmallerSimon Ritter
 
Is An Agile Standard Possible For Java?
Is An Agile Standard Possible For Java?Is An Agile Standard Possible For Java?
Is An Agile Standard Possible For Java?Simon Ritter
 
55 New Features in JDK 9
55 New Features in JDK 955 New Features in JDK 9
55 New Features in JDK 9Simon Ritter
 

Andere mochten auch (12)

Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8
 
Oracle Keynote from JMagghreb 2014
Oracle Keynote from JMagghreb 2014Oracle Keynote from JMagghreb 2014
Oracle Keynote from JMagghreb 2014
 
Lambdas And Streams in JDK8
Lambdas And Streams in JDK8Lambdas And Streams in JDK8
Lambdas And Streams in JDK8
 
Java: Create The Future Keynote
Java: Create The Future KeynoteJava: Create The Future Keynote
Java: Create The Future Keynote
 
Functional programming with_jdk8-s_ritter
Functional programming with_jdk8-s_ritterFunctional programming with_jdk8-s_ritter
Functional programming with_jdk8-s_ritter
 
Project Jigsaw in JDK9
Project Jigsaw in JDK9Project Jigsaw in JDK9
Project Jigsaw in JDK9
 
The Java Carputer
The Java CarputerThe Java Carputer
The Java Carputer
 
JDK 9: Big Changes To Make Java Smaller
JDK 9: Big Changes To Make Java SmallerJDK 9: Big Changes To Make Java Smaller
JDK 9: Big Changes To Make Java Smaller
 
It's Java Jim, But Not As We Know It!
It's Java Jim, But Not As We Know It!It's Java Jim, But Not As We Know It!
It's Java Jim, But Not As We Know It!
 
JDK 9: Big Changes To Make Java Smaller
JDK 9: Big Changes To Make Java SmallerJDK 9: Big Changes To Make Java Smaller
JDK 9: Big Changes To Make Java Smaller
 
Is An Agile Standard Possible For Java?
Is An Agile Standard Possible For Java?Is An Agile Standard Possible For Java?
Is An Agile Standard Possible For Java?
 
55 New Features in JDK 9
55 New Features in JDK 955 New Features in JDK 9
55 New Features in JDK 9
 

Ähnlich wie Lambdas And Streams Hands On Lab, JavaOne 2014

What's New in Java 8
What's New in Java 8What's New in Java 8
What's New in Java 8javafxpert
 
New features in jdk8 iti
New features in jdk8 itiNew features in jdk8 iti
New features in jdk8 itiAhmed mar3y
 
MapReduce Design Patterns
MapReduce Design PatternsMapReduce Design Patterns
MapReduce Design PatternsDonald Miner
 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8Martin Toshev
 
What's new in java 8
What's new in java 8What's new in java 8
What's new in java 8Dian Aditya
 
Java 8 New features
Java 8 New featuresJava 8 New features
Java 8 New featuresSon Nguyen
 
Java 8: more readable and flexible code
Java 8: more readable and flexible codeJava 8: more readable and flexible code
Java 8: more readable and flexible codeWeAreEsynergy
 
Integrate SparkR with existing R packages to accelerate data science workflows
 Integrate SparkR with existing R packages to accelerate data science workflows Integrate SparkR with existing R packages to accelerate data science workflows
Integrate SparkR with existing R packages to accelerate data science workflowsArtem Ervits
 
Big Data Analytics-Open Source Toolkits
Big Data Analytics-Open Source ToolkitsBig Data Analytics-Open Source Toolkits
Big Data Analytics-Open Source ToolkitsDataWorks Summit
 
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...Akaks
 
A Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its APIA Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its APIJörn Guy Süß JGS
 
Rdf Processing On The Java Platform
Rdf Processing On The Java PlatformRdf Processing On The Java Platform
Rdf Processing On The Java Platformguestc1b16406
 
Novidades do Java SE 8
Novidades do Java SE 8Novidades do Java SE 8
Novidades do Java SE 8Bruno Borges
 
Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 StreamsCharles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streamsjessitron
 

Ähnlich wie Lambdas And Streams Hands On Lab, JavaOne 2014 (20)

JDK8 Streams
JDK8 StreamsJDK8 Streams
JDK8 Streams
 
What's New in Java 8
What's New in Java 8What's New in Java 8
What's New in Java 8
 
New features in jdk8 iti
New features in jdk8 itiNew features in jdk8 iti
New features in jdk8 iti
 
MapReduce Design Patterns
MapReduce Design PatternsMapReduce Design Patterns
MapReduce Design Patterns
 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8
 
Java 8 Intro - Core Features
Java 8 Intro - Core FeaturesJava 8 Intro - Core Features
Java 8 Intro - Core Features
 
What's new in java 8
What's new in java 8What's new in java 8
What's new in java 8
 
Java 8 New features
Java 8 New featuresJava 8 New features
Java 8 New features
 
Java8
Java8Java8
Java8
 
Java 8: more readable and flexible code
Java 8: more readable and flexible codeJava 8: more readable and flexible code
Java 8: more readable and flexible code
 
Integrate SparkR with existing R packages to accelerate data science workflows
 Integrate SparkR with existing R packages to accelerate data science workflows Integrate SparkR with existing R packages to accelerate data science workflows
Integrate SparkR with existing R packages to accelerate data science workflows
 
Big Data Analytics-Open Source Toolkits
Big Data Analytics-Open Source ToolkitsBig Data Analytics-Open Source Toolkits
Big Data Analytics-Open Source Toolkits
 
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
 
A Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its APIA Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its API
 
Rdf Processing On The Java Platform
Rdf Processing On The Java PlatformRdf Processing On The Java Platform
Rdf Processing On The Java Platform
 
Colloquium Report
Colloquium ReportColloquium Report
Colloquium Report
 
Novidades do Java SE 8
Novidades do Java SE 8Novidades do Java SE 8
Novidades do Java SE 8
 
Wien15 java8
Wien15 java8Wien15 java8
Wien15 java8
 
Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 StreamsCharles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streams
 
Java 8 Feature Preview
Java 8 Feature PreviewJava 8 Feature Preview
Java 8 Feature Preview
 

Mehr von Simon Ritter

Cloud Native Compiler
Cloud Native CompilerCloud Native Compiler
Cloud Native CompilerSimon Ritter
 
The Art of Java Type Patterns
The Art of Java Type PatternsThe Art of Java Type Patterns
The Art of Java Type PatternsSimon Ritter
 
Modern Java Workshop
Modern Java WorkshopModern Java Workshop
Modern Java WorkshopSimon Ritter
 
Java performance monitoring
Java performance monitoringJava performance monitoring
Java performance monitoringSimon Ritter
 
Modern Java Workshop
Modern Java WorkshopModern Java Workshop
Modern Java WorkshopSimon Ritter
 
Getting the Most From Modern Java
Getting the Most From Modern JavaGetting the Most From Modern Java
Getting the Most From Modern JavaSimon Ritter
 
Building a Better JVM
Building a Better JVMBuilding a Better JVM
Building a Better JVMSimon Ritter
 
JDK 14 Lots of New Features
JDK 14 Lots of New FeaturesJDK 14 Lots of New Features
JDK 14 Lots of New FeaturesSimon Ritter
 
How to Choose a JDK
How to Choose a JDKHow to Choose a JDK
How to Choose a JDKSimon Ritter
 
The Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans TechnologyThe Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans TechnologySimon Ritter
 
Developing Enterprise Applications Using Java Technology
Developing Enterprise Applications Using Java TechnologyDeveloping Enterprise Applications Using Java Technology
Developing Enterprise Applications Using Java TechnologySimon Ritter
 
Is Java Still Free?
Is Java Still Free?Is Java Still Free?
Is Java Still Free?Simon Ritter
 
Moving Towards JDK 12
Moving Towards JDK 12Moving Towards JDK 12
Moving Towards JDK 12Simon Ritter
 
JDK 9, 10, 11 and Beyond
JDK 9, 10, 11 and BeyondJDK 9, 10, 11 and Beyond
JDK 9, 10, 11 and BeyondSimon Ritter
 
Java Is Still Free
Java Is Still FreeJava Is Still Free
Java Is Still FreeSimon Ritter
 
JDK 9, 10, 11 and Beyond
JDK 9, 10, 11 and BeyondJDK 9, 10, 11 and Beyond
JDK 9, 10, 11 and BeyondSimon Ritter
 
JDK 9 and JDK 10 Deep Dive
JDK 9 and JDK 10 Deep DiveJDK 9 and JDK 10 Deep Dive
JDK 9 and JDK 10 Deep DiveSimon Ritter
 

Mehr von Simon Ritter (20)

Cloud Native Compiler
Cloud Native CompilerCloud Native Compiler
Cloud Native Compiler
 
Java On CRaC
Java On CRaCJava On CRaC
Java On CRaC
 
The Art of Java Type Patterns
The Art of Java Type PatternsThe Art of Java Type Patterns
The Art of Java Type Patterns
 
Modern Java Workshop
Modern Java WorkshopModern Java Workshop
Modern Java Workshop
 
Java performance monitoring
Java performance monitoringJava performance monitoring
Java performance monitoring
 
Modern Java Workshop
Modern Java WorkshopModern Java Workshop
Modern Java Workshop
 
Getting the Most From Modern Java
Getting the Most From Modern JavaGetting the Most From Modern Java
Getting the Most From Modern Java
 
Building a Better JVM
Building a Better JVMBuilding a Better JVM
Building a Better JVM
 
JDK 14 Lots of New Features
JDK 14 Lots of New FeaturesJDK 14 Lots of New Features
JDK 14 Lots of New Features
 
Java after 8
Java after 8Java after 8
Java after 8
 
How to Choose a JDK
How to Choose a JDKHow to Choose a JDK
How to Choose a JDK
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
The Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans TechnologyThe Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans Technology
 
Developing Enterprise Applications Using Java Technology
Developing Enterprise Applications Using Java TechnologyDeveloping Enterprise Applications Using Java Technology
Developing Enterprise Applications Using Java Technology
 
Is Java Still Free?
Is Java Still Free?Is Java Still Free?
Is Java Still Free?
 
Moving Towards JDK 12
Moving Towards JDK 12Moving Towards JDK 12
Moving Towards JDK 12
 
JDK 9, 10, 11 and Beyond
JDK 9, 10, 11 and BeyondJDK 9, 10, 11 and Beyond
JDK 9, 10, 11 and Beyond
 
Java Is Still Free
Java Is Still FreeJava Is Still Free
Java Is Still Free
 
JDK 9, 10, 11 and Beyond
JDK 9, 10, 11 and BeyondJDK 9, 10, 11 and Beyond
JDK 9, 10, 11 and Beyond
 
JDK 9 and JDK 10 Deep Dive
JDK 9 and JDK 10 Deep DiveJDK 9 and JDK 10 Deep Dive
JDK 9 and JDK 10 Deep Dive
 

Kürzlich hochgeladen

Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....ShaimaaMohamedGalal
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 

Kürzlich hochgeladen (20)

Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 

Lambdas And Streams Hands On Lab, JavaOne 2014

  • 1.
  • 2. Lambdas & Streams Laboratory Stuart Marks Simon Ritter Angela Caicedo Oracle Corp. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
  • 3. Lambdas and Functions Copyright © 2012, Oracle and/or its affiliates. 3 All rights reserved. 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) Copyright © 2012, Oracle and/or its affiliates. 4 All rights reserved.
  • 5. Lambda Examples SomeList<Student> students = ... double highestScore = students.stream(). filter(Student s -> s.getGradYear() == 2011). map(Student s -> s.getScore()). max(); Copyright © 2012, Oracle and/or its affiliates. 5 All rights reserved.
  • 6. Method References • Method references let us reuse a method as a lambda expression FileFilter x = new FileFilter() { public boolean accept(File f) { FileFilter x = (File f) -> f.canRead(); FileFilter x = File::canRead; Copyright © 2012, Oracle and/or its affiliates. 6 All rights reserved. return f.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 Copyright © 2012, Oracle and/or its affiliates. 7 All rights reserved.
  • 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 Copyright © 2012, Oracle and/or its affiliates. 8 All rights reserved.
  • 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()); Copyright © 2012, Oracle and/or its affiliates. 9 All rights reserved.
  • 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 Copyright © 2012, Oracle and/or its affiliates. 10 All rights reserved.
  • 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); Copyright © 2012, Oracle and/or its affiliates. 11 All rights reserved.
  • 12. Files and Lines of Text  BufferedReader has new method – Stream<String> lines()  HINT: Test framework creates a BufferedReader for you Copyright © 2012, Oracle and/or its affiliates. 12 All rights reserved.
  • 13. Maps and FlatMaps Map Values in a Stream Map FlatMap Input Stream Input Stream Copyright © 2012, Oracle and/or its affiliates. 13 All rights reserved. 1 to 1 mapping 1 to many mapping Output Stream Output Stream
  • 14. Useful Stream Methods  collect (terminal)  filter (intermediate)  count (terminal)  skip, limit (intermediate)  max (terminal)  getAsInt (terminal) Copyright © 2012, Oracle and/or its affiliates. 14 All rights reserved.
  • 15. Getting Started  Open the LambdasHOL project in NetBeans  The exercises are configured as tests  Edit the tests – Remove the @Ignore annotation  Run the tests (Ctrl F6, or from the menu)  Make the tests pass  Simple! Copyright © 2012, Oracle and/or its affiliates. 15 All rights reserved.
  • 16. Copyright © 2012, Oracle and/or its affiliates. 16 All rights reserved. Let’s Go!
  • 17. Copyright © 2012, Oracle and/or its affiliates. 17 All rights reserved.