Java Method References: Streamline Your Code with Lambda Expressions
Discover Java method references, introduced in Java 8, a cleaner and more readable alternative to lambda expressions. Learn how to use method references to refer to methods by their names without specifying arguments, improving code efficiency and clarity. The "::" symbol is used to represent method references in Java functional interfaces.
Java - Method References
Method references were introduced in Java 8. They are a special type of lambda expression that helps to make code more readable and cleaner. Method references can be used wherever we have a lambda expression that calls a method of a functional interface.
Method references allow us to refer to methods by their names without specifying arguments, as arguments are passed by the lambda expression. The "::" symbol is used to represent method references.
Types of Java Method References
Method references can be used to point to the following types of methods:
- Static methods
- Instance methods
- Constructors using the
new
operator (e.g.,TreeSet::new
)
Method Reference for Static Method
A static method can be referred to using the "::" symbol without needing to create an instance of the class. You just need the class name.
Syntax
ClassName::methodName
Example: Reference to a Static Method
In this example, we will reference a static method to print elements in different ways: using a for loop, a lambda expression, and a method reference.
Code Example
package com.tutorialsarena;
import java.util.ArrayList;
import java.util.List;
public class Tester {
public static void main(String args[]) {
List names = new ArrayList<>();
names.add("Amit");
names.add("Rahul");
names.add("Sonia");
names.add("Vikas");
names.add("Reena");
System.out.println("Printing using for each loop");
for (String name: names) {
System.out.println(name);
}
System.out.println("Printing using lambda expression");
names.forEach(s -> System.out.println(s));
System.out.println("Printing using method reference");
names.forEach(System.out::println);
}
}
Output
Printing using for each loop
Amit
Rahul
Sonia
Vikas
Reena
Printing using lambda expression
Amit
Rahul
Sonia
Vikas
Reena
Printing using method reference
Amit
Rahul
Sonia
Vikas
Reena
Method Reference for Instance Method
Similar to static methods, we can refer to instance methods using method references.
Syntax
objectName::methodName
Example: Reference to an Instance Method
This example shows how to reference an instance method of the Integer class to compare elements, using both a lambda expression and a method reference.
Code Example
package com.tutorialsarena;
import java.util.Arrays;
import java.util.List;
public class Tester {
public static void main(String args[]) {
List numbers = Arrays.asList(6, 3, 9, 2, 8);
System.out.println("Sorted using lambda expression");
numbers = numbers.stream().sorted((a, b) -> a.compareTo(b)).toList();
System.out.println(numbers);
numbers = Arrays.asList(6, 3, 9, 2, 8);
System.out.println("Sorted using method reference");
numbers = numbers.stream().sorted(Integer::compareTo).toList();
System.out.println(numbers);
}
}
Output
Sorted using lambda expression
[2, 3, 6, 8, 9]
Sorted using method reference
[2, 3, 6, 8, 9]
Method Reference for Constructor
Method references can also be used to invoke constructors. This is similar to static method invocation, but we use new
instead of a method name.
Syntax
ClassName::new
Example: Reference to a Constructor
In this example, we will create new Student objects based on a list of student names.
Code Example
package com.tutorialsarena;
import java.util.Arrays;
import java.util.List;
public class Tester {
public static void main(String args[]) {
List studentNames = Arrays.asList("Amit", "Rahul", "Sonia", "Vikas", "Reena");
Student[] students = studentNames.stream().map(Student::new).toArray(Student[]::new);
List list = Arrays.asList(students);
System.out.println(list);
}
}
class Student {
String name;
Student(String name) {
this.name = name;
}
public String toString() {
return this.name;
}
}
Output
[Amit, Rahul, Sonia, Vikas, Reena]