xxxxxxxxxx
interface demoLambda{
void sayHello();
}
public class main(String[]args){
demoLambda(()->System.out.println("hello"));
}
public void demoLambda(demoLambda d){
d.sayhello();
}
xxxxxxxxxx
A lambda expression is a short block
of code which takes in parameters
and returns a value. Lambda expressions
are similar to methods, but they do
not need a name and they can be
implemented right in the body of a method.
parameter -> expression
To use more than one parameter, wrap them in parentheses:
(parameter1, parameter2) -> expression
Example
Use a lamba expression in the ArrayList's
forEach() method to print every item in the list:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(5);
numbers.add(9);
numbers.add(8);
numbers.add(1);
numbers.forEach( (n) -> { System.out.println(n); } );
}
}
xxxxxxxxxx
There are few functional interfaces namely Consumer, Supplier, Predicate and Functions
- Consumer:
+ 1 input, 0 output
+ java.util.function.Consumer
+ example: Consumer<List<String>> printConsumer = list -> list.stream().forEach(System.out::println);
- Supplier:
+ 0 input, 1 output
+ java.util.function.Supplier
+ example: Supplier<Double> doubleSupplier1 = () -> Math.random();
- Predicate:
+ 1 input, 1 boolean output
+ java.util.function.Predicate
+ example: Predicate<String> nameStartsWithS = str -> str.startsWith("S");
- Function:
+ 1 input, 1 output
+ java.util.function.Function
+ example: Function<String, Integer> nameMappingFunction = String::length;
xxxxxxxxxx
//* No Parameter Syntax
() -> {
//Body of no parameter lambda
}
//* One Parameter Syntax
(p1) -> {
//Body of single parameter lambda
}
xxxxxxxxxx
//The:
Runnable r = ()-> System.out.print("Run method");
//is equivalent to:
Runnable r = new Runnable() {
@Override
public void run() {
System.out.print("Run method");
}
};
xxxxxxxxxx
public class TestLambda {
public static void main(String args[]) {
List<String> lowerCaseStringsList = Arrays.asList("a","b","c");
// the :: notation is the lambda expression
// it's the same as the anonymous function s -> s.toUpperCase()
List<String> upperCaseStringsList = lowerCaseStringsList.stream().
map(String::toUpperCase).
collect(Collectors.toList());
}
}
xxxxxxxxxx
// Concept of lambda is based on functional interface
// lambda can replace the implementation of interfaces that only has one method to override
interface MyFunctionalInterface {
void myMethod(String s);
}
public class LambdaExample {
// Assume this method exists in the class
static void someMethod(String s) {
System.out.println("Custom Method: " + s);
}
public static void main(String[] args) {
// Traditional way using an anonymous class
MyFunctionalInterface anonymousClass = new MyFunctionalInterface() {
@Override
public void myMethod(String s) {
System.out.println("Hello, " + s);
}
};
anonymousClass.myMethod("World");
// Using a lambda expression
MyFunctionalInterface lambdaExpression = (s) -> someMethod(s);
lambdaExpression.myMethod("Lambda World");
// Using a method reference
MyFunctionalInterface methodReference = LambdaExample::someMethod;
methodReference.myMethod("Method Reference World");
}
}
/* ----------------- OUTPUT ----------------
Hello, World
Custom Method: Lambda World
Custom Method: Method Reference World
*/
xxxxxxxxxx
StateOwner stateOwner = new StateOwner();
stateOwner.addStateListener(
(oldState, newState) -> System.out.println("State changed")
);