Here is the table content of the article will we will cover this topic.
1. What is Method reference in java 8?
2. Reference to the static method
3. Reference to instance method from an instance
4. Reference to instance method from class type
5. Reference to constructor
What is Method reference in java 8?
As you know before JAVA 8, we are not able to pass any method as a parameter. If we want to pass a method, we must pass the object that contains the method. But onwards JAVA 8, we can pass the method by use of Lambda expressions. So, lambda expression reduces the code and support new functionality in JAVA.
The method references are the shortened notation of lambda expressions to call a specific method. It helps to refer to methods by their names. Method reference is used to refer to the method of functional interface.
As you already know lambda expression reduces the code as compared to the anonymous class. The method reference is the next step of the lambda expression. Like lambda expression the Method reference executes only a single method.
For Example: In lambda expression, we have a function interface so will print the name of the Student:
(Student s) -> s.getName()
Here s is the object of Student and getName() is the method that will print the name of Student.
By use of method reference you can write it as:
Student :: getName
NOTE: The :: operator is used in method reference to separate the class or object from the method name.
Let’s discuss with popular example forEach. It prints each element into the console.
import java.util.ArrayList; public class ExampleWithMethodReference { public static void main(String[] args) { ArrayList<String> listOfStudents = new ArrayList<String>(); listOfStudents.add("Ram"); listOfStudents.add("Sham"); listOfStudents.add("Krishana"); listOfStudents.forEach(System.out::println); } }
Output:Ram
Sham
Krishana
NOTE: In JAVA 8, Instead of using AN ANONYMOUS CLASS we can use
A LAMBDA EXPRESSION And if this just calls one method, you can use
A METHOD REFERENCE.
Types of method reference:
1. Reference to the static method
2. Reference to instance method from an instance
3. Reference to instance method from class type
4. Reference to constructor
1. Reference to the static method
By the use of method reference, we can refer to any static method. As you already know static methods are accessible by name of the class.
Syntax:
ClassName :: methodName;
We will discuss the scenario with Lambda expression and method reference:
import java.util.function.Function; public class ExampleWithMethodReference { public static void main(String[] args) { //Calling parseInt() method using lambda Function<String, Integer> lambdaExpression = (a) -> Integer.parseInt(a); System.out.println(lambdaExpression.apply("5")); //Calling parseInt() method using method reference Function<String, Integer> methodReference = Integer::parseInt; System.out.println(methodReference.apply("5")); } }
Output: 5
5
import java.util.function.BiFunction; import java.util.function.Function; class OperatorByMethodRefer { public static int add(int a, int b) { return a+b; } public static String upperCase(String a) { return a.toUpperCase(); } } public class ExampleOfMethodReference { public static void main(String[] args) { BiFunction<Integer, Integer, Integer> addInt = OperatorByMethodRefer::add; Function<String, String> upperString = OperatorByMethodRefer::upperCase; System.out.println(addInt.apply(5, 8)); System.out.println(upperString.apply("javagoal")); } }
Output: 13
JAVAGOAL
2. Reference to instance method from an instance
By use of instance, we can refer to the instance method:
Object :: methodName;
import java.util.function.Function; public class ExampleWithMethodRefer { public static void main(String[] args) { //Calling toLowerCase() method using lambda Function<String, String> lambdaFunction = (String s) -> s.toLowerCase(); System.out.println(lambdaFunction.apply("JAVAGOAL")); //Calling toLowerCase() method using method reference Function<String, String> referenceFunction = String::toLowerCase; System.out.println(referenceFunction.apply("JAVAGOAL")); } }
Output: javagoal
javagoal
3. Reference to instance method from class type
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class ExampleWithMethodRefer { public static void main(String[] args) { List<String> names = new ArrayList<String>(); names.add("Ram"); names.add("Sham"); names.add("Krishan"); names.add("Nony"); List<String> sorted = names.stream() .sorted((s1, s2) -> s1.compareTo(s2)) .collect(Collectors.toList()); System.out.println(sorted); List<String> sortedAlt = names.stream() .sorted(String::compareTo) .collect(Collectors.toList()); System.out.println(sortedAlt); } }
Output: [Krishan, Nony, Ram, Sham]
[Krishan, Nony, Ram, Sham]
4. Reference to constructor
interface Information { Show getInfo(String msg); } class Show{ Show(String msg) { System.out.print(msg); } } public class ExampleOfMethodReferences { public static void main(String[] args) { Information info = Show::new; info.getInfo("JAVAGOAL"); } }
Output: JAVAGOAL