Interview question
What are method references in Java, and what are their different types? Java में method references क्या हैं, और इनके अलग-अलग types क्या हैं?
Answer
Method references are a shorthand for lambda expressions that simply call an existing method, using the :: syntax for extra readability.
| Type | Syntax | Example |
|---|---|---|
| Static method | ClassName::staticMethod | Integer::parseInt |
| Instance method of a particular object | instance::instanceMethod | str::toUpperCase (where str is a variable) |
| Instance method of an arbitrary object | ClassName::instanceMethod | String::toUpperCase |
| Constructor | ClassName::new | ArrayList::new |
import java.util.function.*;
import java.util.stream.*;
// Static method reference
Function<String, Integer> parser1 = s -> Integer.parseInt(s); // lambda
Function<String, Integer> parser2 = Integer::parseInt; // method reference - same effect
System.out.println(parser2.apply('42')); // 42
// Instance method reference on a PARTICULAR object
String greeting = 'Hello World';
Supplier<String> upper1 = () -> greeting.toUpperCase(); // lambda
Supplier<String> upper2 = greeting::toUpperCase; // method reference
System.out.println(upper2.get()); // HELLO WORLD
// Instance method reference on an ARBITRARY object of a type
// (the object itself becomes the first argument)
List<String> names = List.of('charlie', 'alice', 'bob');
List<String> upperNames1 = names.stream()
.map(s -> s.toUpperCase()) // lambda
.collect(Collectors.toList());
List<String> upperNames2 = names.stream()
.map(String::toUpperCase) // method reference - equivalent
.collect(Collectors.toList());
// Constructor reference
Supplier<ArrayList<String>> listCreator1 = () -> new ArrayList<>(); // lambda
Supplier<ArrayList<String>> listCreator2 = ArrayList::new; // method reference
Function<String, StringBuilder> sbCreator = StringBuilder::new;
StringBuilder sb = sbCreator.apply('Hello');
// Method references with sort/forEach
names.forEach(System.out::println); // instead of s -> System.out.println(s)Method references, existing method को simply call करने वाले lambda expressions का shorthand हैं, :: syntax use करते हुए readability के लिए।
| Type | Syntax | उदाहरण |
|---|---|---|
| Static method | ClassName::staticMethod | Integer::parseInt |
| किसी particular object का instance method | instance::instanceMethod | str::toUpperCase |
| Arbitrary object का instance method | ClassName::instanceMethod | String::toUpperCase |
| Constructor | ClassName::new | ArrayList::new |
import java.util.function.*;
import java.util.stream.*;
Function<String, Integer> parser1 = s -> Integer.parseInt(s);
Function<String, Integer> parser2 = Integer::parseInt;
System.out.println(parser2.apply('42')); // 42
String greeting = 'Hello World';
Supplier<String> upper1 = () -> greeting.toUpperCase();
Supplier<String> upper2 = greeting::toUpperCase;
System.out.println(upper2.get()); // HELLO WORLD
List<String> names = List.of('charlie', 'alice', 'bob');
List<String> upperNames2 = names.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
Supplier<ArrayList<String>> listCreator2 = ArrayList::new;
Function<String, StringBuilder> sbCreator = StringBuilder::new;
StringBuilder sb = sbCreator.apply('Hello');
names.forEach(System.out::println);Was this answer clear?