SlideShare a Scribd company logo
Lambdas And Streams Hands On Lab, JavaOne 2014
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.

More Related Content

What's hot (15)

PDF
Apouc 2014-java-8-create-the-future
OUGTH Oracle User Group in Thailand
 
PPTX
Functional Programming With Lambdas and Streams in JDK8
IndicThreads
 
PPTX
Java SE 8 - New Features
Naveen Hegde
 
PDF
Lambdas & Streams
C4Media
 
PDF
Streams in Java 8
Tobias Coetzee
 
PPTX
Java 8
Sudipta K Paik
 
PPTX
Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...
jaxLondonConference
 
PDF
How Green are Java Best Coding Practices? - GreenDays @ Rennes - 2014-07-01
Jérôme Rocheteau
 
PPTX
Java 8 streams
Manav Prasad
 
PPTX
Java 8 lambda
Manav Prasad
 
PPTX
Chap2java5th
Asfand Hassan
 
PPT
3. Data types and Variables
Nilesh Dalvi
 
PPTX
Chap1java5th
Asfand Hassan
 
ODP
Best practices in Java
Mudit Gupta
 
Apouc 2014-java-8-create-the-future
OUGTH Oracle User Group in Thailand
 
Functional Programming With Lambdas and Streams in JDK8
IndicThreads
 
Java SE 8 - New Features
Naveen Hegde
 
Lambdas & Streams
C4Media
 
Streams in Java 8
Tobias Coetzee
 
Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...
jaxLondonConference
 
How Green are Java Best Coding Practices? - GreenDays @ Rennes - 2014-07-01
Jérôme Rocheteau
 
Java 8 streams
Manav Prasad
 
Java 8 lambda
Manav Prasad
 
Chap2java5th
Asfand Hassan
 
3. Data types and Variables
Nilesh Dalvi
 
Chap1java5th
Asfand Hassan
 
Best practices in Java
Mudit Gupta
 

Viewers also liked (12)

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

Similar to Lambdas And Streams Hands On Lab, JavaOne 2014 (20)

PPTX
Java 8 Lambda and Streams
Venkata Naga Ravi
 
PDF
Lambda.pdf
ManishWalia18
 
PPTX
java8
Arik Abulafya
 
PDF
Java Lambda internals with invoke dynamic
Mohit Kumar
 
PDF
Charles Sharp: Java 8 Streams
jessitron
 
PPTX
Java 8
vpulec
 
PPTX
A brief tour of modern Java
Sina Madani
 
PPTX
JDK8 Streams
Bansilal Haudakari
 
PPTX
Java8 training - Class 1
Marut Singh
 
PDF
Java8
Felipe Mamud
 
PPTX
What's New in Java 8
javafxpert
 
PPTX
Functional programming in java
Mohammad Alhobayyeb
 
PPTX
The Road to Lambda - Mike Duigou
jaxconf
 
PDF
Lambdas HOL
Oleg Tsal-Tsalko
 
PDF
Sailing with Java 8 Streams
Ganesh Samarthyam
 
PDF
Java 8
vilniusjug
 
PDF
NUS Hackers Club Mar 21 - Whats New in JavaSE 8?
Chuk-Munn Lee
 
PPTX
A Brief Conceptual Introduction to Functional Java 8 and its API
Jörn Guy Süß JGS
 
PDF
Java 8 - functional features
Rafal Rybacki
 
PPTX
FUNctional Programming in Java 8
Richard Walker
 
Java 8 Lambda and Streams
Venkata Naga Ravi
 
Lambda.pdf
ManishWalia18
 
Java Lambda internals with invoke dynamic
Mohit Kumar
 
Charles Sharp: Java 8 Streams
jessitron
 
Java 8
vpulec
 
A brief tour of modern Java
Sina Madani
 
JDK8 Streams
Bansilal Haudakari
 
Java8 training - Class 1
Marut Singh
 
What's New in Java 8
javafxpert
 
Functional programming in java
Mohammad Alhobayyeb
 
The Road to Lambda - Mike Duigou
jaxconf
 
Lambdas HOL
Oleg Tsal-Tsalko
 
Sailing with Java 8 Streams
Ganesh Samarthyam
 
Java 8
vilniusjug
 
NUS Hackers Club Mar 21 - Whats New in JavaSE 8?
Chuk-Munn Lee
 
A Brief Conceptual Introduction to Functional Java 8 and its API
Jörn Guy Süß JGS
 
Java 8 - functional features
Rafal Rybacki
 
FUNctional Programming in Java 8
Richard Walker
 
Ad

More from Simon Ritter (20)

PPTX
Java Pattern Puzzles Java Pattern Puzzles
Simon Ritter
 
PPTX
Keeping Your Java Hot by Solving the JVM Warmup Problem
Simon Ritter
 
PPTX
Cloud Native Compiler
Simon Ritter
 
PPTX
Java On CRaC
Simon Ritter
 
PPTX
The Art of Java Type Patterns
Simon Ritter
 
PPTX
Modern Java Workshop
Simon Ritter
 
PPTX
Java performance monitoring
Simon Ritter
 
PPTX
Modern Java Workshop
Simon Ritter
 
PPTX
Getting the Most From Modern Java
Simon Ritter
 
PPTX
Building a Better JVM
Simon Ritter
 
PPTX
JDK 14 Lots of New Features
Simon Ritter
 
PPTX
Java after 8
Simon Ritter
 
PPTX
How to Choose a JDK
Simon Ritter
 
PPTX
Java Programming
Simon Ritter
 
PPTX
The Latest in Enterprise JavaBeans Technology
Simon Ritter
 
PPTX
Developing Enterprise Applications Using Java Technology
Simon Ritter
 
PPTX
Is Java Still Free?
Simon Ritter
 
PPTX
Moving Towards JDK 12
Simon Ritter
 
PPTX
JDK 9, 10, 11 and Beyond
Simon Ritter
 
PPTX
Java Is Still Free
Simon Ritter
 
Java Pattern Puzzles Java Pattern Puzzles
Simon Ritter
 
Keeping Your Java Hot by Solving the JVM Warmup Problem
Simon Ritter
 
Cloud Native Compiler
Simon Ritter
 
Java On CRaC
Simon Ritter
 
The Art of Java Type Patterns
Simon Ritter
 
Modern Java Workshop
Simon Ritter
 
Java performance monitoring
Simon Ritter
 
Modern Java Workshop
Simon Ritter
 
Getting the Most From Modern Java
Simon Ritter
 
Building a Better JVM
Simon Ritter
 
JDK 14 Lots of New Features
Simon Ritter
 
Java after 8
Simon Ritter
 
How to Choose a JDK
Simon Ritter
 
Java Programming
Simon Ritter
 
The Latest in Enterprise JavaBeans Technology
Simon Ritter
 
Developing Enterprise Applications Using Java Technology
Simon Ritter
 
Is Java Still Free?
Simon Ritter
 
Moving Towards JDK 12
Simon Ritter
 
JDK 9, 10, 11 and Beyond
Simon Ritter
 
Java Is Still Free
Simon Ritter
 

Recently uploaded (20)

PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PDF
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PDF
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PDF
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PDF
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 

Lambdas And Streams Hands On Lab, JavaOne 2014

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