Please enable JavaScript.
Coggle requires JavaScript to display documents.
Lambda (UnaryOperator (receives a value of a certain type (like Integer)…
Lambda
UnaryOperator
-
UnaryOperator<Integer> operator = v -> v * 100;
//same as below
Function<Integer, Integer> function = v -> v * 100;
//add 10 to each element in the ArrayList using UnaryOperator
list.replaceAll(element -> element + 10);
Function
- Left = Parameters
- Right = Return expression
- apply() = Applies the return expression to the given argument.
Function<Integer, Integer> func = x -> x * 2;
int result = func.apply(10);
//result = 20
Supplier
- Receives no arguments
- Uses an empty argument list to specify a lambda expression with no arguments.
- get() = Retrieve its value
void display(Supplier<Integer> arg) {
System.out.println(arg.get());
}
void anotherMethod() {
this.display(() -> 10); // 10
this.display(() -> 100); //100
this.display(() -> (int) (Math.Pi() * 100)); //314.159
-
Predicate
-
ArrayList<String> list = new ArrayList<>();
list.add("cat");
list.add("dog");
list.add("cheetah");
list.removeIf(element ->element.startsWith("c"))
//only dog left
BiConsumer
Similar to Consumer, but accept two parameters
// Display the keys and values in the HashMap
aMap.forEach((string1, string2) -> System.out.println(string1 + "..." + string2 + ", " + string1.length()));
-