SlideShare a Scribd company logo
Simplifying with Lambdas
Richard Warburton
Simplifying java with lambdas (short)
What do you mean simple?
Streams
Collectors
Conclusion
Lambda Expressions are here in Java 8!
Simplifying java with lambdas (short)
Simplifying java with lambdas (short)
What can we simplify?
Writing
Reading
Change
Better Libraries through Code as Data
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.out.println("button clicked");
}
});
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.out.println("button clicked");
}
});
button.addActionListener(?);
button.addActionListener(event -> ?);
button.addActionListener(event ->
System.out.println("button clicked")
);
String name = getName();
Runnable runnable =
() -> System.out.println(“Hello “ + name);
FUNCTIONAL INTERFACES
Everything in Java has a type
Problem: Need a type to represent a method
Solution: Use interfaces with a single method
Simplifying java with lambdas (short)
public interface ActionListener {
public void actionPerformed(ActionEvent event);
}
INFERENCE
button.addActionListener(event ->
System.out.println("button clicked")
);
public interface ActionListener {
public void actionPerformed(ActionEvent event);
}
What do you mean simple?
Streams
Collectors
Conclusion
STREAMS
Support automated data parallelism
Abstraction to build computation pipelines
Iterator with inversion of control
int count = 0;
for (Artist artist : artists) {
if (artist.isFrom("London")) {
count++;
}
}
EXTERNAL ITERATION
artists.stream()
.filter(artist -> artist.isFrom("London"))
.count();
INTERNAL ITERATION
Simplifying java with lambdas (short)
List<String> collected =
Stream.of("a", "b", "hello")
.map(item -> item.toUpperCase())
.collect(toList());
assertEquals(
asList("A", "B", "HELLO"),
collected);
Simplifying java with lambdas (short)
List<String> beginningWithNumbers =
Stream.of("a", "1abc", "abc1")
.filter(value -> isDigit(value.charAt(0)))
.collect(toList());
assertEquals(
asList("1abc"),
beginningWithNumbers);
Simplifying java with lambdas (short)
int sum =
Stream.of(1, 2, 3, 4)
.reduce(0, (acc, x) -> acc + x);
assertEquals(10, sum);
Putting it Together
for a given an album, find the nationality of every band
playing on that album
Putting it Together
1. get all the artists for an album,
2. figure out which artists are bands,
3. find the nationalities of each band
4. put together a list of these values.
Putting it Together
Set<String> origins =
album.getMusicians()
.filter(artist -> artist.getName().startsWith("The"))
.map(artist -> artist.getNationality())
.collect(toSet());
Eager vs Lazy
Set<String> origins =
album.getMusicians()
.filter(artist -> artist.getName().startsWith("The"))
.map(artist -> artist.getNationality())
// What’s happened at this point?
.collect(toSet());
Parallelism
Parallelism no longer means a rewrite of your code
Streams support parallelism out of the box
call .parallelStream() instead of .stream()
Performs well in the right circumstances, but not a
panacea
What do you mean simple?
Streams
Collectors
Conclusion
Enter the Collector
Collection is mutable reduction
Generic API for building up final values
Already seen collect(toList())
import static java.util.stream.Collectors.*;
collect(toList());
collect(toSet());
collect(toCollection(
() -> new TreeSet<>()));
Simplifying java with lambdas (short)
Map<Boolean, List<Artist>> bandsAndSolo =
artists.collect(partitioningBy(a -> a.isSolo()));
Simplifying java with lambdas (short)
Map<Artist, List<Album>> albumsByArtist =
albums.collect(
groupingBy(alb -> alb.getMainMusician()));
Map<Artist, Long> albumsByArtist =
albums.collect(
groupingBy(alb -> alb.getMainMusician(),
counting()));
Recap of Collectors
Mutable Reduction
A custom collector is just implementing an interface
Can collect other values
min, max
average, sum
summary statistics
join together strings
What do you mean simple?
Streams
Collectors
Design Patterns
Conclusion
How did we simplify?
Behavioural abstraction
A method/class which is parameterized by
different behaviours.
High Order Functions
Functions which take other functions as
arguments or return functions.
Writing
Avoided repeating basic looping and
collections constructs.
Reading
Words like map or reduce provide
immediate understanding.
Changing
Applying the same techniques in your own
code reaps huge rewards.
Simplifying java with lambdas (short)
Q & A
@richardwarburto
insightfullogic.com
tinyurl.com/java8lambdas

More Related Content

PDF
Lambda and Stream Master class - part 1
PDF
多治見IT勉強会 Groovy Grails
PDF
Lambdas and Streams Master Class Part 2
PDF
名古屋SGGAE/J勉強会 Grails、Gaelykでハンズオン
PDF
Java 8 Streams & Collectors : the Leuven edition
PDF
JDK8 : parallel programming made (too ?) easy
PDF
Java 8 Stream API. A different way to process collections.
PDF
Jggug 2010 330 Grails 1.3 観察
Lambda and Stream Master class - part 1
多治見IT勉強会 Groovy Grails
Lambdas and Streams Master Class Part 2
名古屋SGGAE/J勉強会 Grails、Gaelykでハンズオン
Java 8 Streams & Collectors : the Leuven edition
JDK8 : parallel programming made (too ?) easy
Java 8 Stream API. A different way to process collections.
Jggug 2010 330 Grails 1.3 観察

What's hot (20)

PPTX
Poor Man's Functional Programming
PDF
Javascript ES6 generators
PDF
Java 8 - project lambda
ODP
Java Boilerplate Busters
PDF
ES6 generators
PDF
Introduction kot iin
PDF
Refactoring to Macros with Clojure
PPTX
JFokus 50 new things with java 8
PDF
Java SE 8 for Java EE developers
ODP
Java Boilerplate Busters
PDF
Going reactive in java
PDF
Programming with Python and PostgreSQL
PDF
Functional Programming for OO Programmers (part 2)
PDF
Google guava - almost everything you need to know
PDF
Google Guava for cleaner code
PDF
Auto-GWT : Better GWT Programming with Xtend
PDF
Java 8 - Nuts and Bold - SFEIR Benelux
PDF
"PostgreSQL and Python" Lightning Talk @EuroPython2014
PDF
Stubる - Mockingjayを使ったHTTPクライアントのテスト -
KEY
Gwt and Xtend
Poor Man's Functional Programming
Javascript ES6 generators
Java 8 - project lambda
Java Boilerplate Busters
ES6 generators
Introduction kot iin
Refactoring to Macros with Clojure
JFokus 50 new things with java 8
Java SE 8 for Java EE developers
Java Boilerplate Busters
Going reactive in java
Programming with Python and PostgreSQL
Functional Programming for OO Programmers (part 2)
Google guava - almost everything you need to know
Google Guava for cleaner code
Auto-GWT : Better GWT Programming with Xtend
Java 8 - Nuts and Bold - SFEIR Benelux
"PostgreSQL and Python" Lightning Talk @EuroPython2014
Stubる - Mockingjayを使ったHTTPクライアントのテスト -
Gwt and Xtend
Ad

Viewers also liked (20)

PPTX
Java 8 lambdas expressions
PDF
Productive Programming in Java 8 - with Lambdas and Streams
PDF
Lambdas in Java 8
PDF
Hanoi JUG: Java 8 & lambdas
PPT
My first experience with lambda expressions in java
PPTX
Lambda expressions
PPTX
Lambda expressions
PDF
Lambda Expressions in Java 8
PDF
Java Logging discussion Log4j,Slf4j
PPTX
Lambda Expressions in Java 8
PDF
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
PPTX
Fun with lambda expressions
PDF
Lambda: A Peek Under The Hood - Brian Goetz
PPTX
SLF4J Explained........
PDF
Programming with Lambda Expressions in Java
PDF
Lambda Expressions in Java
PPTX
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
PPTX
Lambda expressions
PPTX
PPTX
Java 8 lambda
Java 8 lambdas expressions
Productive Programming in Java 8 - with Lambdas and Streams
Lambdas in Java 8
Hanoi JUG: Java 8 & lambdas
My first experience with lambda expressions in java
Lambda expressions
Lambda expressions
Lambda Expressions in Java 8
Java Logging discussion Log4j,Slf4j
Lambda Expressions in Java 8
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
Fun with lambda expressions
Lambda: A Peek Under The Hood - Brian Goetz
SLF4J Explained........
Programming with Lambda Expressions in Java
Lambda Expressions in Java
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Lambda expressions
Java 8 lambda
Ad

Similar to Simplifying java with lambdas (short) (20)

PPTX
Java8lambda
PPTX
Exploring Streams and Lambdas in Java8
PDF
Java 8
PPTX
Java8 training - Class 1
PDF
Java SE 8 library design
PPTX
Java 8 streams
PDF
JDK8 Functional API
PDF
Java 8 by example!
PDF
Charles Sharp: Java 8 Streams
PDF
Fun with java 8
PPTX
Java 8 Lambda and Streams
PPTX
Functional programming with Java 8
PPTX
PDF
Harnessing the Power of Java 8 Streams
PDF
Lambda.pdf
PDF
PDF
Java Lambda internals with invoke dynamic
PPTX
Java 8
PPTX
Functional programming in java
PPTX
Java 8 presentation
Java8lambda
Exploring Streams and Lambdas in Java8
Java 8
Java8 training - Class 1
Java SE 8 library design
Java 8 streams
JDK8 Functional API
Java 8 by example!
Charles Sharp: Java 8 Streams
Fun with java 8
Java 8 Lambda and Streams
Functional programming with Java 8
Harnessing the Power of Java 8 Streams
Lambda.pdf
Java Lambda internals with invoke dynamic
Java 8
Functional programming in java
Java 8 presentation

More from RichardWarburton (20)

PDF
Fantastic performance and where to find it
PDF
Production profiling what, why and how technical audience (3)
PDF
Production profiling: What, Why and How
PDF
Production profiling what, why and how (JBCN Edition)
PDF
Production Profiling: What, Why and How
PDF
Java collections the force awakens
PDF
Generics Past, Present and Future (Latest)
PDF
Collections forceawakens
PDF
Generics past, present and future
PDF
Jvm profiling under the hood
PDF
How to run a hackday
PDF
Generics Past, Present and Future
PDF
Pragmatic functional refactoring with java 8 (1)
PDF
Performance and predictability (1)
PDF
Performance and predictability
PDF
Twins: Object Oriented Programming and Functional Programming
PDF
Pragmatic functional refactoring with java 8
PDF
Introduction to lambda behave
PDF
Introduction to lambda behave
PDF
Performance and predictability
Fantastic performance and where to find it
Production profiling what, why and how technical audience (3)
Production profiling: What, Why and How
Production profiling what, why and how (JBCN Edition)
Production Profiling: What, Why and How
Java collections the force awakens
Generics Past, Present and Future (Latest)
Collections forceawakens
Generics past, present and future
Jvm profiling under the hood
How to run a hackday
Generics Past, Present and Future
Pragmatic functional refactoring with java 8 (1)
Performance and predictability (1)
Performance and predictability
Twins: Object Oriented Programming and Functional Programming
Pragmatic functional refactoring with java 8
Introduction to lambda behave
Introduction to lambda behave
Performance and predictability

Recently uploaded (20)

PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
creating-agentic-ai-solutions-leveraging-aws.pdf
PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
PPTX
CroxyProxy Instagram Access id login.pptx
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
PDF
ai-archetype-understanding-the-personality-of-agentic-ai.pdf
PDF
Enable Enterprise-Ready Security on IBM i Systems.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Dell Pro 14 Plus: Be better prepared for what’s coming
PDF
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
PDF
Sensors and Actuators in IoT Systems using pdf
PDF
KodekX | Application Modernization Development
PDF
Modernizing your data center with Dell and AMD
PDF
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
PDF
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Top Generative AI Tools for Patent Drafting in 2025.pdf
PDF
Transforming Manufacturing operations through Intelligent Integrations
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
creating-agentic-ai-solutions-leveraging-aws.pdf
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
CroxyProxy Instagram Access id login.pptx
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
ai-archetype-understanding-the-personality-of-agentic-ai.pdf
Enable Enterprise-Ready Security on IBM i Systems.pdf
Chapter 3 Spatial Domain Image Processing.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Dell Pro 14 Plus: Be better prepared for what’s coming
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
Sensors and Actuators in IoT Systems using pdf
KodekX | Application Modernization Development
Modernizing your data center with Dell and AMD
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
NewMind AI Weekly Chronicles - August'25 Week I
Top Generative AI Tools for Patent Drafting in 2025.pdf
Transforming Manufacturing operations through Intelligent Integrations
madgavkar20181017ppt McKinsey Presentation.pdf

Simplifying java with lambdas (short)