Lambdas
Lambdas
51037
Adam Gerber, PhD, SCJP
[email protected]
Lecture 02 Agenda:
2
Lambda Function
4
How did Java deal with “functional”
programming prior to Java8. Anon
(inner) classes!
Urma/behaviorParam/_01TheProblemDefined
Another example of anon class
Our first Lambda expression
Our first Lambda expression
Urma/behaviorParam/_02BehaviorP4
The “Type” of Lambda Expression
• The java ‘Type’ of a lamba expression is a “Functional
Interface”
• Functional Interface is an interface with exactly ONE
non-default, non-overriding method.
• A Lambda can NOT use reflection as it is NOT an
object.
• A lambda can not use the “this” keyword as it is NOT
an object.
• Much like instance methods, a lambda does not live
on the heap.
Location of Functional Interfaces
• You may define your own “Functional
Interfaces”
• Java8 defines many in: java.util.function.*
• 43 interfaces in 4 categories, such as Function,
Supplier, Consumer, Predicate.
Urma/behaviorParam/PrimableDriver.java
Can I store a Lamba Expression in
a variable in Java?
• Yes!
• Wherever a method requires an
anonymous inner class (with one method),
you may put a lamba expression.
• For example: Collections.sort(list, compL);
• You may also use lambda expressions
with Streams
Is a Lambda expression an Object?
• Not really. It is an ‘object without identity’.
• It does not inherit from Object and so you
can’t call the .equals(), .hashcode(), etc.
• There is no ‘new’ keyword in lamba, so
there is far less overhead both at compile-
and run-time.
• You can't use 'this' keyword to identify it
using reflection.
Syntax of Java 8 Lambdas
• A Java 8 lambda is basically a method in Java without a declaration usually written
as (parameters) -> { body }. Examples,
2. x -> x * x
3. ( ) -> x
• A lambda can have zero or more parameters separated by commas and their type
can be explicitly declared or inferred from the context.
17
Example 1:
Print a list of integers with a lambda
intSeq.forEach(x -> {
x += 2;
System.out.println(x);
});
intSeq.forEach(x -> {
int y = x * 2;
System.out.println(y);
});
intSeq.forEach((Integer x -> {
x += 2;
System.out.println(x);
});
Functional Interfaces:
Examine the SDK java.util.function
Consumers
Predicates
Suppliers
Functions
Numeric Performance Functional Interfaces
Operators
Urma/behaviorParam/UsingConsumers.java
22
4 categories of Functional
Interfaces
Functional Interface Example used in
archetypes
Consumer forEach(Consumer),
peek(Consumer)
Predicate filter(Predicate)
Function map(Function)
Supplier reduce(Supplier)
collect(Supplier)
See java.util.function.*
ConsumerMain
23
Functional Interfaces
• Design decision: Java 8 lambdas are assigned to functional
interfaces.
25
Variable Capture
26
Local Variable Capture Example
}
Urma/_var_capture/
27
Static Variable Capture Example
28
Method References
29
Conciseness with Method
References
We can rewrite the statement
intSeq.forEach(System.out::println);
package urma._method_references;
30
Streams
Declarative— More concise and readable
Composable— Greater flexibility
Parallelizable— Better performance
What is a stream?
A stream is “a sequence of elements from a source that
supports data processing operations.” Internal
iteration.
map(Dish::getName)
Examine the Collections Interface
Stream is defined in the collections interface. All
these new methods are grandfathered-in
starting with Java8 using default.
Streams are infinite
Streams are consumed
Urma/spent_iterator.
Intermediate versus Terminal ops
http://viralpatel.net/blogs/lambda-expressions-java-tutorial/
Intermediary operation
• Intermediary operation: A method that
takes a Stream and returns a Stream.
• They are lazily loaded and will only be
executed if you include a terminal
operation at the end.
– The peek() method
– The map() method
– The filter() method
Terminal operation
• Terminal operation: A method that takes a
Stream<T> and returns void.
• They are eagerly loaded and will cause the
entire pipeline to be executed.
• A terminal operation will “spend” the stream.
– The forEach() method
– The count() method
– The max() method
– The collect() method
– The reduce() method
Stream Example
int sum = widgets.stream()
.sum();
• Lambda Expressions,
http://docs.oracle.com/javase/tutorial/java/javaOO/lambdae
xpressions.html