Category : Java | Sub Category : Java8 Features | By Prasad Bonam Last updated: 2023-11-13 05:23:34 Viewed : 235
In Java, there are several types of method references, each corresponding to a specific situation where a method is invoked. Method references provide a more concise syntax when working with lambda expressions. The main types of method references are:
Refers to a static method.
Syntax: ClassName::staticMethodName
java// Lambda expression
Function<Integer, String> toStringLambda = (num) -> String.valueOf(num);
// Static method reference
Function<Integer, String> toStringReference = String::valueOf;
Refers to an instance method of a particular object.
Syntax: instance::instanceMethodName
java// Lambda expression
Predicate<String> isEmptyLambda = (str) -> str.isEmpty();
// Instance method reference on a particular instance
String sampleString = "Hello, World!";
Predicate<String> isEmptyReference = sampleString::isEmpty;
Refers to an instance method of the first parameter as an input.
Syntax: ClassName::instanceMethodName
java// Lambda expression
BiFunction<String, String, Boolean> startsWithLambda = (str, prefix) -> str.startsWith(prefix);
// Instance method reference on an arbitrary instance
BiFunction<String, String, Boolean> startsWithReference = String::startsWith;
Refers to a constructor.
Syntax:
ClassName::new
ClassName::new
java// Lambda expression
Supplier<List<String>> listSupplierLambda = () -> new ArrayList<>();
// Constructor reference
Supplier<List<String>> listSupplierReference = ArrayList::new;
Refers to an instance method of an arbitrary object of a particular type.
Syntax: ClassName::instanceMethodName
java// Lambda expression
BiFunction<String, String, Boolean> endsWithLambda = (str, suffix) -> str.endsWith(suffix);
// Instance method reference on an arbitrary instance of a particular type
BiFunction<String, String, Boolean> endsWithReference = String::endsWith;
javaimport java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
public class MethodReferencesExample {
public static void main(String[] args) {
// Static method reference
Function<Integer, String> staticMethodRef = String::valueOf;
// Instance method reference on a particular instance
Predicate<String> instanceMethodRef1 = "Hello"::equals;
// Instance method reference on an arbitrary instance
Predicate<String> instanceMethodRef2 = String::isEmpty;
// Constructor reference
Supplier<List<String>> constructorRef = ArrayList::new;
// Reference to an instance method of an arbitrary object of a particular type
BiFunction<String, String, Boolean> arbitraryInstanceMethodRef = String::endsWith;
// Print results
System.out.println(staticMethodRef.apply(42)); // "42"
System.out.println(instanceMethodRef1.test("Hello")); // true
System.out.println(instanceMethodRef2.test("")); // true
System.out.println(constructorRef.get()); // []
System.out.println(arbitraryInstanceMethodRef.apply("Hello", "lo")); // true
}
}
In this example, various types of method references are used, demonstrating the syntax for static methods, instance methods on particular instances, instance methods on arbitrary instances, constructor references, and references to instance methods of arbitrary objects of a particular type.