Method references in Java

In Method references, we will cover following:-

1) Introduction to Method Reference
2) Types of Method Reference
3) Understanding and Using Method Reference
4) Examples of Method Reference

Introduction to Method Reference:-

Method References were introduced in Java 8.
They simplify the implementation of Functional Interfaces.

You might think that we have already done it using Lambda Expressions.
Yes, you are right, but Method References further simplify Lambda Expressions also.
So, they are like shortcuts to writing Lambda Expressions.

What is Method Reference?

Method references are shortened Lambda Expressions, used for invoking methods.
In other words, we can say that if a Lambda Expression is just calling method and doing nothing else, then we can replace it with a Method Reference.
For Example:- 
If our lambda expression is like this:-
                     str -->System.out.print(str);

Then we can replace it with a method reference like this:-
                     System.out::println 

Types of Method Reference:-

There are four types of method references:-

1) Reference to a static method

Example:- ClassName::staticMethodName

2) Reference to an instance method of a particular method

Example:- ObjectName::instanceMethodName

3) Reference to an instance method of an arbitrary object of a particular type

Example:- ContainingType::methodName

4) Reference to a Constructor

Example:- ClassName::new                                        

Where to use Method Reference?

Whenever we have a Lambda Expression which only does one thing and that is calling a method, there we can use method references.
Example:-

Using Lambda Expression:-
   Function<String, String> fn= str-->str.toUpperCase();

Using Method Reference:-
   Function<String, String> fn= String::toUpperCase();

Where we can not use Method Reference?

Whenever we are adding our own logic to a Lambda Expression then we can not use Method References even if we are calling a method.
Example:-

Using Lambda Expression:-
     Predicate <Emp> p=e-->e.getSal()>=20000;

Using method References:-
     Not Possible!

Examples of Method References:-

1) Using method reference for calling static method:-
Output:-
14

2) Using method reference for calling instance method:


Output:-
Company name: Oracle

3) Using method reference for calling instance method for arbitrary object:-

Output:-
java

4) Using method reference for calling Constructor:-

Suppose, we have a lambda expression like the following-
              (args)--> new ClassName(args);

The only thing this lambda expression does is to create and return a new object.
We can reduce it by referencing a constructor of the class with the keyword new as shown below-
             ClassName::new

Example:-
Output:-
Bhopal

Calling no arguments Constructor:-
How will you call a non parameterized constructor which creates and returns object of a particular class using Lambda Expression or Constructor reference?
For example:- how to write following statement using Lambda Expression?
          List<String> lst=new ArrayList<>();

The answer is using Supplier functional interface-

Using a Lambda Expression:-
    Supplier<List<String>> s = () -> new ArrayList<String>();
    List<String> lst = s.get(); 

Using Constructor reference:-
    Supplier<List<String>> s = ArrayList<String>::new;
    List<String> lst = s.get();

Output:-
[  ]

Calling single arguments Constructor:-
How will you call a single parameterized constructor which creates and returns object of a particular class using Lambda Expression or Constructor reference?
For example:- how to write following statement using Lambda Expression?
                  int n=100;
                  Integer obj=new Integer(n);

The answer is using Function functional interface-

Using a Lambda Expression:-
    Function<Integer, Integer> f = s -> new Integer(s);
    int n=100;
    Integer obj = f.apply(n);
    System.out.print(obj); 

Using Constructor reference:-
    Function<Integer, Integer> f = Integer::new;
    int n=100;
    Integer obj = f.apply(n);
    System.out.print(obj); 

Output:-

Post a Comment

0 Comments