Closed gbaswath closed 2 years ago
Lambda expressions are used to create function objects. In Java 8, merge
API was introduced to update keys which are found already with values returned from remapping function and removes if null is obtained. Lambda expression is used to supply remapping function.
public V merge(K arg0, V arg1, java.util.function.BiFunction<? super V,? super V,? extends V> arg2)
Refer - efa13d1
Method reference make function objects creation in most succinct way. In Java 8, many static methods are introduced in boxed primitive types which can be used as method references. For example : Integer
class has sum
method to sum up to numbers.
This reduces boilerplate code which was written earlier for addition of two numbers. It provides readability whereas which was missed during our lambda definition. In addition to this, We can declare function and declare it lambda and subsequently it can used converted to method reference..
Refer - 7dd058e
Function Interface provides generic static method Function.identity()
to return identity function. This can be used in case of representing same lambda for input and output such as x -> x
. Identity function simply allow chaining of function calls in functional programming. It is always better to use lambda when compared to identity function.
Refer - afa69ec
There are five types of method references available.
This is like calling static method in a class using Classname such as Integer::sum
which refers to
public static int sum(int arg1, int arg1)
This is used when an object is already declared before defining lambda expression such as expr::instanceMethod
which refers to arg0 -> expr.instanceMethod(arg0)
Ex:
Instant t->Instant.now().isAfter(t) can be replaced by Instant.now()::isAfter
This is similar to static method reference and the difference is it is applied to instance method defined using classname such as
(String s) -> s.toUpperCase()
can be written as String::toUpperCase
This is used when to create new instance through lambda expression such as in supplier interface. These serve as factory objects. Ex:
() -> new TreeMap<K, V>()
can be written as TreeMap<K,V>::new
These serve as factory objects for creating arrays using new keyword. It can be used in consumer functional interface where in we need to supply length argument.
(length) -> new int[length]
can be written as int[]::new
Refer - d633d09
There are cases where we need to skip using method references if they are not succinct. Ex:
expr.invoke(ClassNameIsMoreHumoungous::methodReference)
can be replaced by lambda as expr.invoke(() -> methodReference())
Method references often provide a more succinct alternative to lambdas. The rule is
if method reference are shorter and cleaner then use it, else stick to lambda.
Highlights
The primary advantage of lambda over anonymous class in creating function object is that it is more succinct. Java 8 also provides a way to generate function object even more succinct through method references.
Reference