2015 Marty Hall
Lambda Expressions
in Java 8: Part 1 Basics
Originals of slides and source code for examples: http://www.coreservlets.com/java-8-tutorial/
Also see the general Java programming tutorial http://courses.coreservlets.com/Course-Materials/java.html
and customized Java training courses (onsite or at public venues) http://courses.coreservlets.com/java-training.html
Customized Java EE Training: http://courses.coreservlets.com/
Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
2015 Marty Hall
For customized Java-related training at
your organization, email hall@coreservlets.com
Marty is also available for consulting and development support
Taught by lead author of Core Servlets & JSP, co-author of
Core JSF (4th Ed), & this tutorial. Available at public venues, or
customized versions can be held on-site at your organization.
Courses developed and taught by Marty Hall
JSF 2.2, PrimeFaces, servlets/JSP, Ajax, JavaScript, jQuery, Android, Java 7 or 8 programming, GWT, custom mix of topics
CoursesCustomized
available in any state
or country.
Maryland/DC area
companies can also choose afternoon/evening courses.
Java
EE Training:
http://courses.coreservlets.com/
Courses
and taught Android,
by coreservlets.com
expertsSpring
(editedMVC,
by Marty)
Java
7, Java developed
8, JSF 2, PrimeFaces,
JSP, Ajax, jQuery,
RESTful Web Services, GWT, Hadoop.
Spring MVC, Core Spring, Hibernate/JPA, Hadoop, HTML5, RESTful Web Services
Developed and taught by well-known
and developer. At for
public
venues or onsite at your location.
Contactauthor
hall@coreservlets.com
details
Topics in This Section
Intro
Motivation
Quick summary of big idea
New option: lambdas
Interpretation
Most basic form
Type inferencing
Expression for body
Omitting parens
Comparing lambda approaches to alternatives
Examples
Numerical integration
Timing utilities
4
2015 Marty Hall
Motivation and
Overview
Customized Java EE Training: http://courses.coreservlets.com/
Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Many Languages Let You Pass
Functions Around
Dynamically (and usually weakly) typed
JavaScript, Lisp, Scheme, etc.
Strongly typed
Ruby, Scala, Clojure, ML, etc.
Functional approach proven concise,
flexible, and parallelizable
JavaScript sorting
var testStrings = ["one", "two", "three", "four"];
testStrings.sort(function(s1, s2) {
return(s1.length - s2.length);});
testStrings.sort(function(s1, s2) {
return(s1.charCodeAt(s1.length - 1)
s2.charCodeAt(s2.length - 1));});
Why Lambdas in Java Now?
Concise syntax
More succinct and clear than anonymous inner classes
Deficiencies with anonymous inner classes
Bulky, confusion re this and naming in general,
no access to non-final local vars, hard to optimize
Convenient for new streams library
shapes.forEach(s -> s.setColor(Color.RED));
Programmers are used to the approach
Callbacks, closures, map/reduce idiom
Surface Advantage of Lambdas:
Concise and Expressive
Old
button.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
doSomethingWith(e);
}
});
New
button.addActionListener(e -> doSomethingWith(e));
Vigorous writing is concise... This requires not that the writer make all sentences short, or avoid all details
and treat subjects only in outline, but that every word should tell. -- Strunk and White, The Elements of Style
Underlying Advantages:
Support New Way of Thinking
Encourage functional programming
When functional programming approach is used, many
classes of problems are easier to solve and result in code
that is clearer to read and simpler to maintain
Support streams
Streams are wrappers around data sources (arrays,
collections, etc.) that use lambdas, support
map/filter/reduce, use lazy evaluation, and can be made
parallel automatically by compiler.
Cannot be made parallel automatically
for(Employee e: employees) { e.giveRaise(1.15); }
Will automatically be run in parallel
employees.parallel().forEach(e -> e.giveRaise(1.15));
9
2015 Marty Hall
Lambdas:
Most Basic Form
Customized Java EE Training: http://courses.coreservlets.com/
Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Main Points
You write what looks like a function
Arrays.sort(testStrings, (s1, s2) -> s1.length() - s2.length());
taskList.execute(() -> downloadSomeFile());
someButton.addActionListener(event -> handleButtonClick());
double d = MathUtils.integrate(x -> x*x, 0, 100, 1000);
You get an instance of a class that implements
the interface that was expected in that place
The expected type must be an interface that has exactly one
(abstract) method
Called Functional Interface or Single Abstract Method (SAM)
Interface
The designation of a single ABSTRACT method is not redundant,
because in Java 8 interfaces can have concrete methods, called
default methods. Java 8 interfaces can also have static methods.
11
Simplest Form:
Syntax Summary
Replace this
new SomeInterface() {
@Override
public SomeType someMethod(args) { body }
}
With this
(args) -> { body }
12
Simplest Form: Example
Old style
Arrays.sort(testStrings, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return(s1.length() - s2.length());
}
});
New style
Arrays.sort(testStrings,
(String s1, String s2) -> { return(s1.length() s2.length()); }
);
13
Sorting Strings by Length
Old version
String[] testStrings =
{"one", "two", "three", "four"};
...
Arrays.sort(testStrings, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return(s1.length() - s2.length());
}
});
In both cases, resultant array is {"one", "two", "four", "three"}
...
New version
Arrays.sort(testStrings,
(String s1, String s2) -> { return(s1.length()
s2.length()); });
14
Sorting Strings by Last Char
Old version
Arrays.sort(testStrings, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return(s1.charAt(s1.length() - 1)
s2.charAt(s2.length() - 1));
}
});
New version
In both cases, resultant array is {"one", "three", "two", "four"}
Arrays.sort(testStrings,
(String s1, String s2) -> {
return(s1.charAt(s1.length() - 1)
s2.charAt(s2.length() - 1)); });
15
Where Can Lambdas Be Used?
Find any variable or parameter that expects an
interface that has one method
Technically 1 abstract method, but in Java 7 there was no
distinction between a 1-method interface and a 1-abstractmethod interface. These 1-method interfaces are called
functional interfaces or SAM (Single Abstract Method)
interfaces.
public interface Blah { String foo(String someString); }
Code that uses interface is the same
public void someMethod(Blah b) { b.foo() }
Code that uses the interface must still know the real method
name of the interface
Code that calls the method that expects the
interface can supply lambda
String result = someMethod(s -> s.toUpperCase() + "!");
16
2015 Marty Hall
Type Inferencing
Customized Java EE Training: http://courses.coreservlets.com/
Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Main Points
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)
Once in a while there are overloaded methods where
passing in a lambda without argument types will be
ambiguous, and thus not compile. This is rare, but will give
example later.
Basic lambda
(Type1 var1, Type2 var2 ) -> { method body }
Lambda with type inferencing
(var1, var2 ) -> { method body }
18
Syntax Summary
Replace this
new SomeInterface() {
@Override
public SomeType someMethod(T1 v1, T2 v2) {
body
}
}
With this
(v1, v2) -> { body }
19
Example
Old style
Arrays.sort(testStrings, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return(s1.length() - s2.length());
}
});
New style
Arrays.sort(testStrings,
(s1, s2) -> { return(s1.length() s2.length()); });
20
Sorting Strings by Length
Old version
String[] testStrings =
{"one", "two", "three", "four"};
...
Arrays.sort(testStrings, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return(s1.length() - s2.length());
}
});
In both cases, resultant array is {"one", "two", "four", "three"}
...
New version
Arrays.sort(testStrings,
(s1, s2) -> { return(s1.length() - s2.length()); });
21
Sorting Strings by Last Char
Old version
Arrays.sort(testStrings, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return(s1.charAt(s1.length() - 1)
s2.charAt(s2.length() - 1));
}
});
New version
In both cases, resultant array is {"one", "three", "two", "four"}
Arrays.sort(testStrings, (s1, s2) -> {
return(s1.charAt(s1.length() - 1)
s2.charAt(s2.length() - 1));
});
22
2015 Marty Hall
Expression for Body:
Implied Return Values
Customized Java EE Training: http://courses.coreservlets.com/
Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Main Points
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
24
Caution
Most lambdas use this approach
Because body of the method of the interface can usually
be represented as a single return statement
But not all
Loops, if statements, etc. cannot be cast as expressions
Examples where explicit blocks needed
25
(n1, n2) -> { if(n1<n2) {
return(n1);
} else {
return(n2); }
(nums) -> { double sum = 0;
for(double num: nums) {
sum += num)
}
return(sum); }
If body of lambda is very long, you often reconsider
using lambdas and go with a normal inner class.
Syntax Summary
Replace this
new SomeInterface() {
@Override
public SomeType someMethod(T1 v1, T2 v2) {
return(someValue);
}
}
With this
(v1, v2) -> someValue
26
Example
Old style
Arrays.sort(testStrings, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return(s1.length() - s2.length());
}
});
New style
Arrays.sort(testStrings, (s1, s2) -> s1.length() s2.length());
27
Sorting Strings by Length
Old version
String[] testStrings =
{"one", "two", "three", "four"};
...
Arrays.sort(testStrings, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return(s1.length() - s2.length());
}
});
In both cases, resultant array is {"one", "two", "four", "three"}
...
New version
Arrays.sort(testStrings,
(s1, s2) -> s1.length() - s2.length());
28
Sorting Strings by Last Char
Old version
Arrays.sort(testStrings, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return(s1.charAt(s1.length() - 1)
s2.charAt(s2.length() - 1));
}
});
New version
In both cases, resultant array is {"one", "three", "two", "four"}
Arrays.sort(testStrings,
(s1, s2) -> s1.charAt(s1.length() - 1)
s2.charAt(s2.length() - 1));
29
Thinking About Lambdas
What really is happening
I used a shortcut way of representing an instance of a
class that implements Comparator<T>. I provided the
body of the compare method after the ->.
How you usually think about it
I passed in the comparison function
Function types
Java 8 does not technically have function types, since
under the hood, lambdas become instances of classes that
implement whatever interface was expected.
Nevertheless, you normally think of lambdas as
functions.
30
2015 Marty Hall
Omitting Parens
Customized Java EE Training: http://courses.coreservlets.com/
Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Main Points
If method takes a single argument, then
parens in the argument list are optional
No type should be used: you must let Java infer the type
But omitting types is normal practice anyhow
Previous version
(varName) -> someResult()
Lambda with parentheses omitted
varName -> someResult()
32
Syntax Summary
Replace this
new SomeInterface() {
@Override
public SomeType someMethod(T1 var) {
return(someValue);
}
}
With this
var -> someValue
33
Example (Listeners for Buttons)
Old style
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
setBackground(Color.BLUE);
}
});
New style
button1.addActionListener(event -> setBackground(Color.BLUE));
34
Buttons: Old Version
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
setBackground(Color.BLUE);
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
setBackground(Color.GREEN);
}
});
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
setBackground(Color.RED);
}
});
35
Buttons: New Version
button1.addActionListener(event -> setBackground (Color.BLUE));
button2.addActionListener(event -> setBackground (Color.GREEN));
button3.addActionListener(event -> setBackground (Color.RED));
These examples do not use the ActionEvent argument (i.e., event above), but the lambda must still declare the variable so that
the method signature of the lambda matches the method signature of the method in ActionListener (i.e., actionPerformed).
36
2015 Marty Hall
Summary:
Making Lambdas
More Succinct
Customized Java EE Training: http://courses.coreservlets.com/
Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Shortening Lambda Syntax
Omit parameter types
Arrays.sort(testStrings,
(String s1, String s2) -> { return(s1.length() s2.length()); });
replaced by
Arrays.sort(testStrings,
(s1, s2) -> { return(s1.length() s2.length()); });
Use expressions instead of blocks
Arrays.sort(testStrings,
(s1, s2) -> { return(s1.length() s2.length()); });
replaced by
Arrays.sort(testStrings, (s1, s2) -> s1.length() s2.length());
Drop parens if single argument
button1.addActionListener((event) -> popUpSomeWindow());
replaced by
button1.addActionListener(event -> popUpSomeWindow());
38
Java 7 vs. Java 8
Java 7
taskList.execute(new Runnable() {
@Override
public void run() {
processSomeImage(imageName);
}
});
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
doSomething(event);
}
});
Java 8
taskList.execute(() -> processSomeImage(imageName));
button.addActionListener(event -> doSomething(event));
39
Java vs. JavaScript
Java
String[] testStrings = {"one", "two", "three", "four"};
Arrays.sort(testStrings,
(s1, s2) -> s1.length() - s2.length());
Arrays.sort(testStrings,
(s1, s2) -> s1.charAt(s1.length() - 1)
s2.charAt(s2.length() - 1));
JavaScript
var testStrings = ["one", "two", "three", "four"];
testStrings.sort(function(s1, s2) {
return(s1.length - s2.length);});
testStrings.sort(function(s1, s2) {
return(s1.charCodeAt(s1.length - 1) s2.charCodeAt(s2.length - 1));
});
40
2015 Marty Hall
Example:
Numerical Integration
Customized Java EE Training: http://courses.coreservlets.com/
Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Example: Numerical Integration
Goals
Simple numerical integration using rectangle (mid-point)
rule
Diagram from
http://en.wikipedia.org/wiki/Numerical_integration
Want to use lambdas for function to be integrated.
Convenient and succinct.
Define functional (SAM) interface with a double
eval(double x) method to specify function to be integrated
42
Interface
public interface Integrable {
double eval(double x);
}
In later sections, we will upgrade this example.
First, we will add the @FunctionalInterface annotation.
Second, we will observe that there is already a compatible interface built into the java.util.function package, and use it instead.
43
Numerical Integration Method
public static double integrate(Integrable function,
double x1, double x2,
int numSlices){
if (numSlices < 1) {
numSlices = 1;
}
double delta = (x2 - x1)/numSlices;
double start = x1 + delta/2;
double sum = 0;
for(int i=0; i<numSlices; i++) {
sum += delta * function.eval(start + delta * i);
}
return(sum);
}
Remember general lambda principle: methods that use 1-method interfaces are the same in Java 8 as in Java 7.
It is the code that calls the method that expects the 1-method interface that can use lambdas.
44
Method for Testing
public static void integrationTest(Integrable function,
double x1, double x2) {
for(int i=1; i<7; i++) {
int numSlices = (int)Math.pow(10, i);
double result =
MathUtilities.integrate(function, x1, x2, numSlices);
System.out.printf(" For numSlices =%,10d result = %,.8f%n",
numSlices, result);
}
}
Also define a simple method for printing out the expected answer
based on strings based in. Full code can be downloaded from
http://www.coreservlets.com/java-8-tutorial/
45
Testing Results
MathUtilities.integrationTest(x
MathUtilities.integrationTest(x
MathUtilities.integrationTest(x
MathUtilities.integrationTest(x
->
->
->
->
x*x, 10, 100);
Math.pow(x,3), 50, 500);
Math.sin(x), 0, Math.PI);
Math.exp(x), 2, 20);
Output
Estimating integral of x^2 from 10.000 to 100.000.
Exact answer = 100^3/3 - 10^3/3.
~= 333,000.00000000.
For numSlices =
10 result = 332,392.50000000
For numSlices =
100 result = 332,993.92500000
For numSlices =
1,000 result = 332,999.93925000
For numSlices =
10,000 result = 332,999.99939250
For numSlices =
100,000 result = 332,999.99999393
For numSlices = 1,000,000 result = 332,999.99999994
... // Similar for other three integrals
46
General Lambda Principle
Interfaces in Java 8 are same as in Java 7
Integrable was the same as it would be in Java 7, except that
you can (should) optionally use @FunctionalInterface
This annotation is covered in the next section
Code that uses interfaces is the same in Java 8
as in Java 7
I.e., the definition of integrate is exactly the same as you
would have written it in Java 7. The author of integrate must
know that the real method name is eval.
Code that calls methods that expect 1-method
interfaces can now use lambdas
MathUtilities.integrate(x -> Math.sin(x), 0, Math.PI, );
47
Instead of new Integrable() { public void eval(double x) { return(Math.sin(x)); } }
2015 Marty Hall
Making a Reusable
Timing Utility
Customized Java EE Training: http://courses.coreservlets.com/
Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Timing
Goals
Pass in a function
Run the function
Print elapsed time
Problem: Java evaluates args on the call
TimingUtils.timeOp(someSlowCalculation());
The calculation is computed before timeOp is called!
Solution: use lambdas
TimingUtils.timeOp(() -> someSlowCalculation());
timeOp can run the calculation internally
Could be done with inner classes
49
And in fact we did so in fork-join section. But, code that
called timeOp was long, cumbersome, and obtuse
The Op Interface
public interface Op {
void runOp();
}
50
The TimingUtils Class
public class TimingUtils {
private static final double ONE_BILLION =
1_000_000_000;
public static void timeOp(Op operation) {
long startTime = System.nanoTime();
operation.runOp();
long endTime = System.nanoTime();
double elapsedSeconds =
(endTime - startTime)/ONE_BILLION;
System.out.printf(" Elapsed time: %.3f seconds.%n",
elapsedSeconds);
}
}
51
Main Test Code
public class TimingTests {
public static void main(String[] args) {
for(int i=3; i<8; i++) {
int size = (int)Math.pow(10, i);
System.out.printf("Sorting array of length %,d.%n",
size);
TimingUtils.timeOp(() -> sortArray(size));
}
}
52
Supporting Test Code
public static double[] randomNums(int length) {
double[] nums = new double[length];
for(int i=0; i<length; i++) {
nums[i] = Math.random();
}
return(nums);
}
public static void sortArray(int length) {
double[] nums = randomNums(length);
Arrays.sort(nums);
}
53
2015 Marty Hall
A Few More Samples
Customized Java EE Training: http://courses.coreservlets.com/
Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
A Few More Samples
As arguments to methods
Arrays.sort(testStrings,
(s1, s2) -> s1.length() - s2.length());
taskList.execute(() -> downloadSomeFile());
button.addActionListener(event -> handleButtonClick());
double d = MathUtils.integrate(x -> x*x, 0, 100, 1000);
55
A Few More Samples
(Continued)
As variables (makes real type more obvious)
AutoCloseable c = () -> cleanupForTryWithResources();
Thread.UncaughtExceptionHandler handler =
(thread, exception) -> doSomethingAboutException();
Formattable f =
(formatter,flags,width,precision) -> makeFormattedString();
ContentHandlerFactory fact =
mimeType -> createContentHandlerForMimeType();
CookiePolicy policy =
(uri, cookie) -> decideIfCookieShouldBeAccepted();
Flushable toilet = () -> writeBufferedOutputToStream();
TextListener t = event -> respondToChangeInTextValue();
56
2015 Marty Hall
Wrap-Up
Customized Java EE Training: http://courses.coreservlets.com/
Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Summary: Big Ideas
Yay: we have lambdas
Concise and succinct
Retrofits in existing APIs
Familiar to developers that know
functional programming
Fits well with new streams API
Also have method refs and prebuilt functional interfaces
Boo: not full functional programming (?)
Type is class that implements
interface, not a real function
Must create or find interface first,
must know method name
Cannot use mutable local variables
Summary: Syntax
Most basic form
(String s1, String s2) -> { return(s1.length() - s2.length()); }
Type inferencing
(s1, s2) -> { return(s1.length() - s2.length()); }
Expressions for body
(s1, s2) -> s1.length() - s2.length()
Omitting parens
event -> doSomethingWith(event)
Defining an interface to use with lambdas
Interface itself is same as Java 7 (but one method only)
Code that calls method expecting interface type can now
use lambda
59
2015 Marty Hall
Questions?
More info:
http://courses.coreservlets.com/Course-Materials/java.html General Java programming tutorial
http://www.coreservlets.com/java-8-tutorial/ Java 8 tutorial
http://courses.coreservlets.com/java-training.html Customized Java training courses, at public venues or onsite at your organization
http://coreservlets.com/ JSF 2, PrimeFaces, Java 7 or 8, Ajax, jQuery, Hadoop, RESTful Web Services, Android, HTML5, Spring, Hibernate, Servlets, JSP, GWT, and other Java EE training
Many additional free tutorials at coreservlets.com (JSF, Android, Ajax, Hadoop, and lots more)
Customized Java EE Training: http://courses.coreservlets.com/
Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
60
Developed and taught by well-known author and developer. At public venues or onsite at your location.