SlideShare a Scribd company logo
Lambda Expressions pt1
1.1 Getting Started with Java 8
1.2 Java 8 References
1.3 Quick review of Pre-Lambda Handlers
1.4. Functional Interface Annotation
1.5 Lambda Handlers
1.6 Why Java 8?
1.7 Wra-Up
Lars A. Lemos
1
1.Getting Started,Setup and
Review
1.1 Getting Started with Java 8
1.2 Java 8 References
1.3 Quick review of Pre-Lambda Handlers
1.4. Functional Interface Annotation
1.5 Lambda Handlers
1.6 Why Java 8?
1.7 Wra-Up
Lars A. Lemos
2
Starting point
 Jdk 8 download, Jdk 8 Demos and Samples
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-
downloads-2133151.html
 More Info
http://docs.oracle.com/javase/8/docs/
http://docs.oracle.com/javase/tutorial/java/javaOO/lambda
expressions.html
 IDE
• IntelliJ IDEA 13 - : http://www.jetbrains.com/idea/download/
• Netbeans 8 : https://netbeans.org/downloads/
• Eclipse Kepler : https://www.eclipse.org/downloads/
Lars A. Lemos 3
1.1 Getting Started with Java 8
 Books
Functional Programming in Java - Venkat Subramanian
Java 8 Lambdas: Pragmatic Functional Programming – Richard Warburton
Java SE 8 for the Really Impatient – Cay S. Horstman
Java The Complete Reference 9th Ed – Herbert Schildt
Lars A. Lemos 4
1.2 Java 8 References
1.3 Quick review of Pre-Lambda Handlers
 Anonymous inner classess
 Idea
You need code to respond a button click, to run in the background
for threads, or to handle sorting comparisons.
 Pre-lambda alternatives
 Separete class
• Arrays.sort(theArray, new SeparateClass(...));
 Main class(which implements interface)
• Arrays.sort(theArray, this);
 Named inner class
• Arrays.sort(theArray, new InnerClass(...));
 Anonymous inner class
• Arrays.sort(theArray, new
Comparator<String>(){...});
Lars A. Lemos 5
1.3 Quick review of Pre-Lambda Handlers
 Separete class
public class StringSorter1 {
public void doTests() {
String[] testStrings = {"one","two","three","four"};
System.out.println("Original: ");
ArrayUtils.printArray(testStrings);
Arrays.sort(testStrings, new StringLengthComparator());
System.out.println("After sorting by length: ");
ArrayUtils.printArray(testStrings);
Arrays.sort(testStrings, new LastLetterComparator());
System.out.println("After sorting by last letter: ");
ArrayUtils.printArray(testStrings);
}}
Example: StringSorter1.java
 Advantages
• Flexible: can pass arguments to class constructor
• More reusable: loosely coupled
 Disadavantages
• Need extra stpes to call method in main app. How does handler call method in main
app?
• It needs reference to the main app to do so.
Lars A. Lemos 6
1.3 Quick review of Pre-Lambda Handlers
 Implementing Interface
 Advantages
• No extra steps need to call methods in the main app.
o The code is part of the main app, so it can call any
method or access any instance variable, even private
ones.
• Simple
o Widely used in real life for button handlers, when you
know you will have only one button, or for threading
code, when you need one method to run in the
background.
 Disadavantages
• Inflexible: hard to have multiple different versions, since
you cannot pass argumens to the handler.
o For example, if you want to sort array using two or more
ways? If you pass “this” to the second argument in
Arrays.sort, it will refer to the same “compare” method both
times. Lars A. Lemos 7
1.3 Quick review of Pre-Lambda Handlers
 Named Inner Class
 Advantages
• No extra steps need to call methods in the main app.Inner class have
full access to all methods and instances variables of surrounding class,
including private ones.
o However if there is a name conflict, it is necessary to use the
unpopular OuterClass.this.name syntax.
• Flexible: can define constructor and pass arguments.
 Disadavantages
o A bit harder to understand.
 Anonymous Inner Class
 Advantages
• Full access to code of surrounding class (even private methods and
variables).
• Slightly more concice than named inner class(but still bulk and
verbose).
 Disadavantages
o Harder to understand
o Not reusable(cannot use the anonymous class definition in more).
Example: StringSorter3.java Lars A. Lemos 8
1.4 @FunctionalInterface Annotation
 @FunctionalInterface
 Tells the compiler you intend this to be a functional Single Abstract Method (SAM)
interface
 One with exactly one abstract method
 Thus, for which lambdas can be used.
 Check performed at compile time
 Also informs others developers that lambdas can be used for this interface
 Analogous to @Override
 Does extra work at compile time, but no difference at run time (assuming it passes
the check)
 Goals
 Simple numerical integration using rectangle (mid-point) rule
 Use lambdas for function to be integrated. Convenient and succinct.
Example: SimpleInterface.java , TestInterface.java
Lars A. Lemos 9
1.3 Lambda Handlers
 Desired features
 Full access to code from surrounding class
 No confusion about meaning of “this”
 Much more concise, succinct, and readable
 Enourage a functional programming style
 Lambda
Arrays.sort(testStrings, (s1, s2) -> s1.length() –
s2.length());
 What is the under the hood implementation
Arrays.sort(testStrings, new Comparator<String>() {
@Override
public int compare(string s1, String s2) {
s1.length() – s2.length());
}
});
Lars A. Lemos 10
1.4 Why Java 8 and Lambdas ?
 Weakly (and usually dynamically ) typed
 JavaScript, Lisp, Scheme, etc.
 Strongly typed
 Ruby, Scala, Clojure, etc.
 Functional approach proven concise, useful, and
parallelizable
 Concise syntax(clear than anonymous inner classes)
 Convienent for new streams library
 Shapes.forEach(s -> s.setColor(Color.RED));
 Similar constructs used in other languages
 Callbacks, closures, map/reduce idiom
 Step toward true functional programming
 When funct. Prog approach is used, many classes of problems
are easier to solve and result in code that is clearer to read and
simpler to maintain. Lars A. Lemos 11
2. Lambdas Basics
2.1 Lambdas Expressions
2.2 Type Inferencing
2.3 Implied Return Values
2.4 Omitting Parents for One-Arg Lambdas
2.5 Effectively Final Local Variables
2.6 @FunctionalInterface Annotation
2.7 Method References
2.8 The java.util.function Package
2.9 Wrap Up
Lars A. Lemos
12
2.1 Lambada Expressions
 Replace this
new SometInterface () {
@Override
public someType someMethod(args) { body; }
}
 With this
(args) - > { body }
 Example
Array.sort(testStrings, new Comparator<String> () ) {
@Override
public int compare(String s1, String s2) {
return (s1.length() – s2.length());
}
Array.sort(testStrings,
(String s1, String s2) - > {
return (s1.length() – s2.length() ) ; } ) ;
Example: JavaLambdas.java
Lars A. Lemos 13
2.1 Lambada Expressions Part
1Basics
 Old
button.addActionListener (new ActionListener () {
@Override
public void actionPerformed(ActionEvent event) {
action();
}
});
 New
button.addActionListener(event -> action());
 You get an Instance of an inner class that
implements interface
 The expected type must be an interface that has exactly one
(abstract).
 It’s called “Functional Interface” or “Single Abstract Method” (SAM)
Example: ButtonFrame1.java
Lars A. Lemos 14
2.2 Type Inferencing
 Types in argument list can usually be omitted
 Since Java usually already knows the expected parameter types
for the single method of the functional interface (SAM interface)
 Basic lambda
(Type1 var1, Type2 var 2 …) - > { method body }
 Lambda with type inference
( var1, var 2 …) - > { method body }
Lars A. Lemos 15
2.2 Type Inferencing
 Replace this
new SometInterface () {
@Override
public someType someMethod( T1 v1, T2 v2 ) { body; }
}
 With this
(v1, v2) - > { body }
Array.sort(testStrings, new Comparator<String> () ) {
@Override
public int compare(String s1, String s2) {
return (s1.length() – s2.length());
}
Array.sort(testStrings,
(s1, s2) - > {
return (s1.length() – s2.length() ) ; } ) ;
Example: JavaLambdas.java
Lars A. Lemos 16
2.3 Implied Return Values
 For body, use expression instead of block
 Value of expression will be the return value, with no explicit “return” needed
 If method has a void return type, then automatically no return value
 Since lambdas are usually used only when method body is short, this
approach (using expression instead of block ) is very common.
 Previous Version
(var1, var2) - > { return (something); }
 Lambda with expression for body
(var1, var2) - > { something }
Example: JavaLambdas.java
Lars A. Lemos 17
2.4 Omitting Parents for One-Arg
Lambdas
 If method take single argument, parents
optional
 No type should be used: you must let Java infer the type.
 Ommiting types is normal practice
 Previous Version
( varName ) - > someResult()
 Lambda with parentheses omitted
var - > someResult()
Example: ButtonFrame3.java
Lars A. Lemos 18
2.5 Effectively Final Local Variables
 Lambdas can refer to local variables that are not
declared final (but are never modified)
 This is known as “effectively final” – variables where it would have been legal
to declare them final.
 You can still refer to mutable instance variables.
 “this” in a lambda refers to main class, not inner class that was created for
the lambda. There is no OuterClass.this. Also, no new level of scoping.
 With explicit declaration
final String s = “…”;
doSomething(someArg - > use(s) );
 Effectively final
String s = “…”;
doSomething(someArg - > use(s) );
Example: ButtonFrame4.java
Lars A. Lemos 19
2.6 @FunctionalInterface Annotation
MathUtilities.integrationTest(x -> x*x, 10, 100);
MathUtilities.integrationTest(x -> Math.pow(x,3), 50, 500);
MathUtilities.integrationTest(x -> Math.sin(x), 0, Math.PI);
MathUtilities.integrationTest(x -> Math.exp(x) , 2, 20);
Example: MathUtilities.java
Lars A. Lemos 20
2.7 Method References
 Use ClassName:: staticMethodName or
variable::instanceMethodName for lambdas
 Ex: Math::cos or myVar::myMethod
 The function must match signature of method in functional (SAM)
interface to which it is assigned.
 The type is found only from the context
 The type of a method reference depends on what it is assigned to.
This is always true with lambdas, but more obvious here.
 Ex: no predifined type for Math::cos
Lars A. Lemos 21
2.7 Method References
 In previous example, replace
MathUtilities.integrationTest(x-> Math.sin(x), 0, Math.PI );
MathUtilities.integrationTest(x-> Math.exp(x), 2, 20 );
 With these
MathUtilities.integrationTest( Math::sin(x), 0, Math.PI );
MathUtilities.integrationTest(Math::exp(x), 2, 20 );
 Type of a method reference?
Is not known until you try to assign it to a variable, in which case its
type is whatever interface that variable expected.
Ex: Math::sin could be different types in different contexts, but all the
types would be single-method interfaces whose method could accept
a single double as an argument.
Example: IntegrationTest1.java
Lars A. Lemos 22
2.8 The java.util.function
Package
 Interfaces like Integratable widely used
 So, Java 8 should build in many common cases
 Java.util.function defines many simple functional(SAM)
interfaces
 Named according to arguments and return values
Ex: replace my Integratable with builtin DoubleUnaryOperator
 Look in API for the method names
Althoug lambdas don’t refer to method names, your code that uses
the lambdas will need to call the methods.
 Type of a method reference?
Lars A. Lemos 23
2.8 The java.util.function
Package
 Types given
 Samples
 IntPredicate(int n, boolean out)
 LongUnaryOperator(long in, long out)
 DoubleBinaryOperator(two double in, double out)
 Example
 DoubleBinaryOperator f = (d1, d2) -> Math.cos(d1 + d2);
 Genericized
 There are also generic interfaces (Function<T,R> , Predicate<T>)
with widespread applicability.
 And concrete methods like “compose” and “negate”
Lars A. Lemos 24
2.8 The java.util.function
Package
 In previous example, replace this
public static double integrate(Integratable function, ...) {
... function.eval(...);
....
 With this
public static double integrate(DoubleUnaryOperator function, ...) {
... function.applyAsDouble(...);
....
}
Example: MathUtils.java
 Then, omit definition of Integratable entirely
 Because DoubleUnaryOperator is a functional (SAM) interface
containing a method with same signature as the method of the
Integratable interface.
Lars A. Lemos 25
2.8 The java.util.function
Package
 General Case
 If you are tempted to create an interface purely to be used as
target for a lambda
 Look through java.util.function and see if one of the functional
(SAM) interfaces can be used instead.
• DoubleUnaryOperator, IntUnaryOperator, LongUnaryOperator
o double/int/long in, same type out
• DoubleBinaryOpeartor, IntBinaryOperator, LongBinaryOperator
o Two doubles/ints/longs in, same type out
• DoublePredicate, IntPredicate, LongPredicate
o double/int/long in, boolean out
• DoubleConsumer, IntConsumer , LongConsumer
o double/int/long in, void return type
• Genericized interfaces: Function, Predicate, Consumer, etc
Lars A. Lemos 26
2.9 Wrap Up
 We have lambdas
 Concise and succint
 Familiar to developers that know functional programming
 Fits well with new streams API
 Have method references
 Many functional interfaces built in
 Still not real functional programming
 Type is inner class that implements interface, not a function
 Must create or find interface 1st, must know method name.
 Cannot use mutable local variables
Lars A. Lemos 27
End of part I
Part II – Streams
Thank you for your patience….
larslemos@gmail.com
Lars Lemos
toplars
Lars A. Lemos 28

More Related Content

PPTX
The Dark Side Of Lambda Expressions in Java 8
PPTX
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
PPTX
Java 8 Feature Preview
PPTX
Java 8 lambda
PDF
Java 8 Lambda Expressions & Streams
PPTX
Java 8 Features
PDF
Java 8 features
ODP
Introduction to Java 8
The Dark Side Of Lambda Expressions in Java 8
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Java 8 Feature Preview
Java 8 lambda
Java 8 Lambda Expressions & Streams
Java 8 Features
Java 8 features
Introduction to Java 8

What's hot (20)

PPTX
Java SE 8 - New Features
PPTX
Lambda Expressions in Java 8
PPTX
java 8 new features
PDF
Java8 features
PPTX
Java 8 - Features Overview
PDF
Scala tutorial
PPT
Java findamentals1
PPT
Java findamentals1
PPT
Java findamentals1
PDF
Java SE 8 library design
PPT
Major Java 8 features
PDF
Programming with Lambda Expressions in Java
PPTX
Functional Programming In Jdk8
PDF
Productive Programming in Java 8 - with Lambdas and Streams
PDF
Lambda Expressions in Java
PPTX
Java concurrency questions and answers
PPTX
Java moderno java para Jedis episodio I
PPTX
PDF
Java SE 8
Java SE 8 - New Features
Lambda Expressions in Java 8
java 8 new features
Java8 features
Java 8 - Features Overview
Scala tutorial
Java findamentals1
Java findamentals1
Java findamentals1
Java SE 8 library design
Major Java 8 features
Programming with Lambda Expressions in Java
Functional Programming In Jdk8
Productive Programming in Java 8 - with Lambdas and Streams
Lambda Expressions in Java
Java concurrency questions and answers
Java moderno java para Jedis episodio I
Java SE 8
Ad

Viewers also liked (11)

PDF
Towards a Principle-based Classification of Structural Design Smells
PDF
PHAME: Principles of Hierarchy Abstraction Modularization and Encapsulation
PDF
Tools for refactoring
PDF
Why care about technical debt?
PDF
Infographic - Pragmatic Technical Debt Management
PDF
Pragmatic Technical Debt Management
PDF
Tools for Identifying and Addressing Technical Debt
PDF
A Checklist for Design Reviews
PDF
Refactoring for Software Design Smells: Managing Technical Debt
PDF
Applying Design Principles in Practice
PDF
SOLID Principles and Design Patterns
Towards a Principle-based Classification of Structural Design Smells
PHAME: Principles of Hierarchy Abstraction Modularization and Encapsulation
Tools for refactoring
Why care about technical debt?
Infographic - Pragmatic Technical Debt Management
Pragmatic Technical Debt Management
Tools for Identifying and Addressing Technical Debt
A Checklist for Design Reviews
Refactoring for Software Design Smells: Managing Technical Debt
Applying Design Principles in Practice
SOLID Principles and Design Patterns
Ad

Similar to Java 8 lambdas expressions (20)

PDF
Lambdas in Java 8
PPTX
Java 8 features
PPTX
PPTX
New features in jdk8 iti
PPTX
Java 8 new features
PPTX
Java 8 Lambda Expressions
PDF
Java 8-revealed
PDF
JSR 335 / java 8 - update reference
PPTX
Java 8 - An Overview
PPTX
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
PPTX
Java 8 presentation
PPTX
Lambdas, Collections Framework, Stream API
PPTX
PPTX
What's New in Java 8
PDF
Lambdas & Streams
PDF
Java 8
PDF
Functional programming in java 8 by harmeet singh
PPTX
Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...
PPTX
java150929145120-lva1-app6892 (2).pptx
PPTX
Lambda expressions java8 - yousry
Lambdas in Java 8
Java 8 features
New features in jdk8 iti
Java 8 new features
Java 8 Lambda Expressions
Java 8-revealed
JSR 335 / java 8 - update reference
Java 8 - An Overview
JDK8 Lambdas and Streams: Changing The Way You Think When Developing Java
Java 8 presentation
Lambdas, Collections Framework, Stream API
What's New in Java 8
Lambdas & Streams
Java 8
Functional programming in java 8 by harmeet singh
Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...
java150929145120-lva1-app6892 (2).pptx
Lambda expressions java8 - yousry

Recently uploaded (20)

PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
L1 - Introduction to python Backend.pptx
PPTX
Transform Your Business with a Software ERP System
PDF
medical staffing services at VALiNTRY
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
history of c programming in notes for students .pptx
PDF
Nekopoi APK 2025 free lastest update
PDF
AI in Product Development-omnex systems
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Upgrade and Innovation Strategies for SAP ERP Customers
L1 - Introduction to python Backend.pptx
Transform Your Business with a Software ERP System
medical staffing services at VALiNTRY
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
history of c programming in notes for students .pptx
Nekopoi APK 2025 free lastest update
AI in Product Development-omnex systems
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
CHAPTER 2 - PM Management and IT Context
Reimagine Home Health with the Power of Agentic AI​
Design an Analysis of Algorithms I-SECS-1021-03
VVF-Customer-Presentation2025-Ver1.9.pptx
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Wondershare Filmora 15 Crack With Activation Key [2025
wealthsignaloriginal-com-DS-text-... (1).pdf

Java 8 lambdas expressions

  • 1. Lambda Expressions pt1 1.1 Getting Started with Java 8 1.2 Java 8 References 1.3 Quick review of Pre-Lambda Handlers 1.4. Functional Interface Annotation 1.5 Lambda Handlers 1.6 Why Java 8? 1.7 Wra-Up Lars A. Lemos 1
  • 2. 1.Getting Started,Setup and Review 1.1 Getting Started with Java 8 1.2 Java 8 References 1.3 Quick review of Pre-Lambda Handlers 1.4. Functional Interface Annotation 1.5 Lambda Handlers 1.6 Why Java 8? 1.7 Wra-Up Lars A. Lemos 2
  • 3. Starting point  Jdk 8 download, Jdk 8 Demos and Samples http://www.oracle.com/technetwork/java/javase/downloads/jdk8- downloads-2133151.html  More Info http://docs.oracle.com/javase/8/docs/ http://docs.oracle.com/javase/tutorial/java/javaOO/lambda expressions.html  IDE • IntelliJ IDEA 13 - : http://www.jetbrains.com/idea/download/ • Netbeans 8 : https://netbeans.org/downloads/ • Eclipse Kepler : https://www.eclipse.org/downloads/ Lars A. Lemos 3 1.1 Getting Started with Java 8
  • 4.  Books Functional Programming in Java - Venkat Subramanian Java 8 Lambdas: Pragmatic Functional Programming – Richard Warburton Java SE 8 for the Really Impatient – Cay S. Horstman Java The Complete Reference 9th Ed – Herbert Schildt Lars A. Lemos 4 1.2 Java 8 References
  • 5. 1.3 Quick review of Pre-Lambda Handlers  Anonymous inner classess  Idea You need code to respond a button click, to run in the background for threads, or to handle sorting comparisons.  Pre-lambda alternatives  Separete class • Arrays.sort(theArray, new SeparateClass(...));  Main class(which implements interface) • Arrays.sort(theArray, this);  Named inner class • Arrays.sort(theArray, new InnerClass(...));  Anonymous inner class • Arrays.sort(theArray, new Comparator<String>(){...}); Lars A. Lemos 5
  • 6. 1.3 Quick review of Pre-Lambda Handlers  Separete class public class StringSorter1 { public void doTests() { String[] testStrings = {"one","two","three","four"}; System.out.println("Original: "); ArrayUtils.printArray(testStrings); Arrays.sort(testStrings, new StringLengthComparator()); System.out.println("After sorting by length: "); ArrayUtils.printArray(testStrings); Arrays.sort(testStrings, new LastLetterComparator()); System.out.println("After sorting by last letter: "); ArrayUtils.printArray(testStrings); }} Example: StringSorter1.java  Advantages • Flexible: can pass arguments to class constructor • More reusable: loosely coupled  Disadavantages • Need extra stpes to call method in main app. How does handler call method in main app? • It needs reference to the main app to do so. Lars A. Lemos 6
  • 7. 1.3 Quick review of Pre-Lambda Handlers  Implementing Interface  Advantages • No extra steps need to call methods in the main app. o The code is part of the main app, so it can call any method or access any instance variable, even private ones. • Simple o Widely used in real life for button handlers, when you know you will have only one button, or for threading code, when you need one method to run in the background.  Disadavantages • Inflexible: hard to have multiple different versions, since you cannot pass argumens to the handler. o For example, if you want to sort array using two or more ways? If you pass “this” to the second argument in Arrays.sort, it will refer to the same “compare” method both times. Lars A. Lemos 7
  • 8. 1.3 Quick review of Pre-Lambda Handlers  Named Inner Class  Advantages • No extra steps need to call methods in the main app.Inner class have full access to all methods and instances variables of surrounding class, including private ones. o However if there is a name conflict, it is necessary to use the unpopular OuterClass.this.name syntax. • Flexible: can define constructor and pass arguments.  Disadavantages o A bit harder to understand.  Anonymous Inner Class  Advantages • Full access to code of surrounding class (even private methods and variables). • Slightly more concice than named inner class(but still bulk and verbose).  Disadavantages o Harder to understand o Not reusable(cannot use the anonymous class definition in more). Example: StringSorter3.java Lars A. Lemos 8
  • 9. 1.4 @FunctionalInterface Annotation  @FunctionalInterface  Tells the compiler you intend this to be a functional Single Abstract Method (SAM) interface  One with exactly one abstract method  Thus, for which lambdas can be used.  Check performed at compile time  Also informs others developers that lambdas can be used for this interface  Analogous to @Override  Does extra work at compile time, but no difference at run time (assuming it passes the check)  Goals  Simple numerical integration using rectangle (mid-point) rule  Use lambdas for function to be integrated. Convenient and succinct. Example: SimpleInterface.java , TestInterface.java Lars A. Lemos 9
  • 10. 1.3 Lambda Handlers  Desired features  Full access to code from surrounding class  No confusion about meaning of “this”  Much more concise, succinct, and readable  Enourage a functional programming style  Lambda Arrays.sort(testStrings, (s1, s2) -> s1.length() – s2.length());  What is the under the hood implementation Arrays.sort(testStrings, new Comparator<String>() { @Override public int compare(string s1, String s2) { s1.length() – s2.length()); } }); Lars A. Lemos 10
  • 11. 1.4 Why Java 8 and Lambdas ?  Weakly (and usually dynamically ) typed  JavaScript, Lisp, Scheme, etc.  Strongly typed  Ruby, Scala, Clojure, etc.  Functional approach proven concise, useful, and parallelizable  Concise syntax(clear than anonymous inner classes)  Convienent for new streams library  Shapes.forEach(s -> s.setColor(Color.RED));  Similar constructs used in other languages  Callbacks, closures, map/reduce idiom  Step toward true functional programming  When funct. Prog approach is used, many classes of problems are easier to solve and result in code that is clearer to read and simpler to maintain. Lars A. Lemos 11
  • 12. 2. Lambdas Basics 2.1 Lambdas Expressions 2.2 Type Inferencing 2.3 Implied Return Values 2.4 Omitting Parents for One-Arg Lambdas 2.5 Effectively Final Local Variables 2.6 @FunctionalInterface Annotation 2.7 Method References 2.8 The java.util.function Package 2.9 Wrap Up Lars A. Lemos 12
  • 13. 2.1 Lambada Expressions  Replace this new SometInterface () { @Override public someType someMethod(args) { body; } }  With this (args) - > { body }  Example Array.sort(testStrings, new Comparator<String> () ) { @Override public int compare(String s1, String s2) { return (s1.length() – s2.length()); } Array.sort(testStrings, (String s1, String s2) - > { return (s1.length() – s2.length() ) ; } ) ; Example: JavaLambdas.java Lars A. Lemos 13
  • 14. 2.1 Lambada Expressions Part 1Basics  Old button.addActionListener (new ActionListener () { @Override public void actionPerformed(ActionEvent event) { action(); } });  New button.addActionListener(event -> action());  You get an Instance of an inner class that implements interface  The expected type must be an interface that has exactly one (abstract).  It’s called “Functional Interface” or “Single Abstract Method” (SAM) Example: ButtonFrame1.java Lars A. Lemos 14
  • 15. 2.2 Type Inferencing  Types in argument list can usually be omitted  Since Java usually already knows the expected parameter types for the single method of the functional interface (SAM interface)  Basic lambda (Type1 var1, Type2 var 2 …) - > { method body }  Lambda with type inference ( var1, var 2 …) - > { method body } Lars A. Lemos 15
  • 16. 2.2 Type Inferencing  Replace this new SometInterface () { @Override public someType someMethod( T1 v1, T2 v2 ) { body; } }  With this (v1, v2) - > { body } Array.sort(testStrings, new Comparator<String> () ) { @Override public int compare(String s1, String s2) { return (s1.length() – s2.length()); } Array.sort(testStrings, (s1, s2) - > { return (s1.length() – s2.length() ) ; } ) ; Example: JavaLambdas.java Lars A. Lemos 16
  • 17. 2.3 Implied Return Values  For body, use expression instead of block  Value of expression will be the return value, with no explicit “return” needed  If method has a void return type, then automatically no return value  Since lambdas are usually used only when method body is short, this approach (using expression instead of block ) is very common.  Previous Version (var1, var2) - > { return (something); }  Lambda with expression for body (var1, var2) - > { something } Example: JavaLambdas.java Lars A. Lemos 17
  • 18. 2.4 Omitting Parents for One-Arg Lambdas  If method take single argument, parents optional  No type should be used: you must let Java infer the type.  Ommiting types is normal practice  Previous Version ( varName ) - > someResult()  Lambda with parentheses omitted var - > someResult() Example: ButtonFrame3.java Lars A. Lemos 18
  • 19. 2.5 Effectively Final Local Variables  Lambdas can refer to local variables that are not declared final (but are never modified)  This is known as “effectively final” – variables where it would have been legal to declare them final.  You can still refer to mutable instance variables.  “this” in a lambda refers to main class, not inner class that was created for the lambda. There is no OuterClass.this. Also, no new level of scoping.  With explicit declaration final String s = “…”; doSomething(someArg - > use(s) );  Effectively final String s = “…”; doSomething(someArg - > use(s) ); Example: ButtonFrame4.java Lars A. Lemos 19
  • 20. 2.6 @FunctionalInterface Annotation MathUtilities.integrationTest(x -> x*x, 10, 100); MathUtilities.integrationTest(x -> Math.pow(x,3), 50, 500); MathUtilities.integrationTest(x -> Math.sin(x), 0, Math.PI); MathUtilities.integrationTest(x -> Math.exp(x) , 2, 20); Example: MathUtilities.java Lars A. Lemos 20
  • 21. 2.7 Method References  Use ClassName:: staticMethodName or variable::instanceMethodName for lambdas  Ex: Math::cos or myVar::myMethod  The function must match signature of method in functional (SAM) interface to which it is assigned.  The type is found only from the context  The type of a method reference depends on what it is assigned to. This is always true with lambdas, but more obvious here.  Ex: no predifined type for Math::cos Lars A. Lemos 21
  • 22. 2.7 Method References  In previous example, replace MathUtilities.integrationTest(x-> Math.sin(x), 0, Math.PI ); MathUtilities.integrationTest(x-> Math.exp(x), 2, 20 );  With these MathUtilities.integrationTest( Math::sin(x), 0, Math.PI ); MathUtilities.integrationTest(Math::exp(x), 2, 20 );  Type of a method reference? Is not known until you try to assign it to a variable, in which case its type is whatever interface that variable expected. Ex: Math::sin could be different types in different contexts, but all the types would be single-method interfaces whose method could accept a single double as an argument. Example: IntegrationTest1.java Lars A. Lemos 22
  • 23. 2.8 The java.util.function Package  Interfaces like Integratable widely used  So, Java 8 should build in many common cases  Java.util.function defines many simple functional(SAM) interfaces  Named according to arguments and return values Ex: replace my Integratable with builtin DoubleUnaryOperator  Look in API for the method names Althoug lambdas don’t refer to method names, your code that uses the lambdas will need to call the methods.  Type of a method reference? Lars A. Lemos 23
  • 24. 2.8 The java.util.function Package  Types given  Samples  IntPredicate(int n, boolean out)  LongUnaryOperator(long in, long out)  DoubleBinaryOperator(two double in, double out)  Example  DoubleBinaryOperator f = (d1, d2) -> Math.cos(d1 + d2);  Genericized  There are also generic interfaces (Function<T,R> , Predicate<T>) with widespread applicability.  And concrete methods like “compose” and “negate” Lars A. Lemos 24
  • 25. 2.8 The java.util.function Package  In previous example, replace this public static double integrate(Integratable function, ...) { ... function.eval(...); ....  With this public static double integrate(DoubleUnaryOperator function, ...) { ... function.applyAsDouble(...); .... } Example: MathUtils.java  Then, omit definition of Integratable entirely  Because DoubleUnaryOperator is a functional (SAM) interface containing a method with same signature as the method of the Integratable interface. Lars A. Lemos 25
  • 26. 2.8 The java.util.function Package  General Case  If you are tempted to create an interface purely to be used as target for a lambda  Look through java.util.function and see if one of the functional (SAM) interfaces can be used instead. • DoubleUnaryOperator, IntUnaryOperator, LongUnaryOperator o double/int/long in, same type out • DoubleBinaryOpeartor, IntBinaryOperator, LongBinaryOperator o Two doubles/ints/longs in, same type out • DoublePredicate, IntPredicate, LongPredicate o double/int/long in, boolean out • DoubleConsumer, IntConsumer , LongConsumer o double/int/long in, void return type • Genericized interfaces: Function, Predicate, Consumer, etc Lars A. Lemos 26
  • 27. 2.9 Wrap Up  We have lambdas  Concise and succint  Familiar to developers that know functional programming  Fits well with new streams API  Have method references  Many functional interfaces built in  Still not real functional programming  Type is inner class that implements interface, not a function  Must create or find interface 1st, must know method name.  Cannot use mutable local variables Lars A. Lemos 27
  • 28. End of part I Part II – Streams Thank you for your patience…. larslemos@gmail.com Lars Lemos toplars Lars A. Lemos 28