Category : Java | Sub Category : Java8 Features | By Prasad Bonam Last updated: 2023-11-13 05:26:16 Viewed : 245
Method references in Java provide a concise syntax for creating lambda expressions that directly invoke methods or constructors. Knowing when and how to use method references can lead to cleaner and more readable code. Here are some guidelines on when and how to use method references:
Existing Methods:
javaList<String> names = Arrays.asList("Alice", "Bob", "Charlie");
// Lambda expression
names.forEach(name -> System.out.println(name));
// Method reference
names.forEach(System.out::println);
Instance Methods on Particular Instances:
javaString sampleString = "Hello, World!";
// Lambda expression
Predicate<String> isEmptyLambda = (str) -> str.isEmpty();
// Method reference
Predicate<String> isEmptyReference = sampleString::isEmpty;
Instance Methods on Arbitrary Instances:
java// Lambda expression
BiFunction<String, String, Boolean> startsWithLambda = (str, prefix) -> str.startsWith(prefix);
// Method reference
BiFunction<String, String, Boolean> startsWithReference = String::startsWith;
Constructors:
java// Lambda expression
Supplier<List<String>> listSupplierLambda = () -> new ArrayList<>();
// Constructor reference
Supplier<List<String>> listSupplierReference = ArrayList::new;
Functional Interfaces:
java// Lambda expression
Function<Integer, String> toStringLambda = (num) -> String.valueOf(num);
// Method reference
Function<Integer, String> toStringReference = String::valueOf;
Static Method References:
ClassName::staticMethodName
javaFunction<Integer, String> staticMethodRef = String::valueOf;
Instance Method References on a Particular Instance:
instance::instanceMethodName
javaPredicate<String> instanceMethodRef1 = "Hello"::equals;
Instance Method References on an Arbitrary Instance:
ClassName::instanceMethodName
javaBiFunction<String, String, Boolean> arbitraryInstanceMethodRef = String::endsWith;
Constructor References:
ClassName::new
ClassName::new
javaSupplier<List<String>> constructorRef = ArrayList::new;
Reference to an Instance Method of an Arbitrary Object of a Particular Type:
ClassName::instanceMethodName
javaBiFunction<String, String, Boolean> arbitraryInstanceMethodRef = String::startsWith;
Readability:
Existing Methods:
Functional Interfaces:
Constructor References:
Instance Methods on Particular Instances:
Instance Methods on Arbitrary Instances:
javaimport java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
public class MethodReferencesExample {
public static void main(String[] args) {
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
// Method reference for static method
names.forEach(System.out::println);
// Method reference for instance method on a particular instance
Predicate<String> isEmptyReference = "Hello"::equals;
// Method reference for instance method on an arbitrary instance
Function<String, Boolean> startsWithReference = String::startsWith;
// Constructor reference
Supplier<List<String>> listSupplierReference = ArrayList::new;
// Functional interface with method reference
Function<Integer, String> toStringReference = String::valueOf;
}
}
In this example, various types of method references are used to demonstrate when and how to apply them. The code focuses on readability and concise expression of method invocations.