Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
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.

TypeSyntaxExample
Static methodClassName::staticMethodInteger::parseInt
Instance method of a particular objectinstance::instanceMethodstr::toUpperCase (where str is a variable)
Instance method of an arbitrary objectClassName::instanceMethodString::toUpperCase
ConstructorClassName::newArrayList::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 के लिए।

TypeSyntaxउदाहरण
Static methodClassName::staticMethodInteger::parseInt
किसी particular object का instance methodinstance::instanceMethodstr::toUpperCase
Arbitrary object का instance methodClassName::instanceMethodString::toUpperCase
ConstructorClassName::newArrayList::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?