Java के Method References
In this page:
ClassName::staticMethod
object::instanceMethod
ClassName::instanceMethod
ClassName::new
Introduction to Method References
Method references सिर्फ किसी मौजूदा method को call करने वाले simple lambda expressions लिखने का एक shorthand syntax हैं। वे उस method को नाम से refer करने के लिए double colon (::) operator का उपयोग करते हैं बिना इसे तुरंत call किए।
उदाहरण: Introduction to Method References
import java.util.function.Consumer;
public class Main {
public static void main(String[] args) {
Consumer<String> printer = System.out::println; // shorthand for s -> System.out.println(s)
printer.accept("Using ::");
}
}
Login to try C/C++/Java/PHP code in the editor
Reference to an Instance Method of a Particular Object
आप किसी एक specific, पहले से मौजूद object से संबंधित instance method को 'instance::instanceMethod' format का उपयोग करके refer कर सकते हैं। यह call को सीधे उसी particular object से बांध देता है, इसलिए reference का हर invocation उसी instance पर operate करता है।
उदाहरण: Reference to an Instance Method of a Particular Object
import java.util.function.Supplier;
public class Main {
public static void main(String[] args) {
String greeting = "Hello";
Supplier<Integer> lengthGetter = greeting::length; // bound to this specific instance
System.out.println(lengthGetter.get());
}
}
Login to try C/C++/Java/PHP code in the editor
Reference to an Instance Method of an Arbitrary Object
आप किसी दिए गए type के किसी arbitrary object के instance method को 'Type::instanceMethod' format का उपयोग करके भी refer कर सकते हैं। इस style में, functional interface को दिया गया पहला argument वह object बन जाता है जिस पर method असल में call होता है, बजाय किसी fixed instance के जिसे आपने पहले नाम दिया हो।
उदाहरण: Reference to an Instance Method of an Arbitrary Object
import java.util.function.Function;
public class Main {
public static void main(String[] args) {
Function<String, Integer> toLength = String::length; // first argument becomes the receiver
System.out.println(toLength.apply("arbitrary"));
}
}
Login to try C/C++/Java/PHP code in the editor
Constructor References
आप 'Class::new' format का उपयोग करके constructors के references लिख सकते हैं, जो आपको objects को dynamically instantiate करने का एक साफ़, reusable तरीका देता है, उदाहरण के लिए किसी stream के collect() या map() call में पास किए गए Supplier के रूप में।
उदाहरण: Constructor References
// Import java.util.function.Supplier so it can be used by its short name
import java.util.function.Supplier;
// Import java.util.ArrayList so it can be used by its short name
import java.util.ArrayList;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
Supplier<ArrayList<String>> listMaker = ArrayList::new;
ArrayList<String> list = listMaker.get();
list.add("created via constructor reference");
// Print a line to the console
System.out.println(list);
}
}
Login to try C/C++/Java/PHP code in the editor
Method References vs Lambdas
जब भी आप बिना बदले किसी मौजूदा method को सिर्फ forward कर रहे हों तो method references lambdas से ज़्यादा संक्षिप्त रूप से पढ़े जाते हैं। लेकिन अगर आपको arguments transform करने हों, अतिरिक्त logic जोड़ना हो, या कई steps साथ combine करने हों, तो एक standard lambda expression बेहतर, ज़्यादा flexible choice है।
उदाहरण: Method References vs Lambdas
import java.util.function.Function;
public class Main {
public static void main(String[] args) {
Function<String, Integer> ref = String::length; // simple forward, reads cleanly
Function<String, Integer> lambda = s -> s.length() + 1; // extra logic needs a lambda
System.out.println(ref.apply("abc") + " " + lambda.apply("abc"));
}
}
Login to try C/C++/Java/PHP code in the editor
- parentheses के साथ
String::length()लिखना, जो एक syntax error है; किसी method reference में कोई parentheses या arguments नहीं होते। - ऐसे method reference का उपयोग करना जिसके parameters functional interface से मेल न खाएं, जो 'invalid method reference' जैसा compile error देता है।
- किसी
Function<Integer, String>के लिएInteger::toStringका उपयोग करना, जो static और instance versions के बीच अस्पष्ट है और compile नहीं होता; इसके बजाय एक lambda का उपयोग करें।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: