gbaswath / effective-java-programming

Effective Java - 3rd Edition Source Code & its Documentation with Test Cases
MIT License
1 stars 0 forks source link

Item 43: Prefer method references to lambdas #2

Closed gbaswath closed 2 years ago

gbaswath commented 2 years ago

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

  1. MethodReferenceExample.java
  2. MethodReferenceExampleTest.java
gbaswath commented 2 years ago

Lambda expression as function objects

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

gbaswath commented 2 years ago

Method Reference as Function Objects

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

gbaswath commented 2 years ago

Identity Function

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

gbaswath commented 2 years ago

Method Reference Types

There are five types of method references available.

  1. Static method reference.
  2. Bound method reference (Instance Method).
  3. Unbound method reference (Instance Method).
  4. Class Constructor method reference
  5. Array Constructor method reference

Static Method Reference

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)

Bound Method Reference

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

Unbound Method Reference

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

Class Constructor Method Reference

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

Array Constructor Method Reference

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

gbaswath commented 2 years ago

Disadvantages

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())

gbaswath commented 2 years ago

Summary

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.