Using method references Java In Hindi
जावा प्रोग्रामिंग लैंग्वेज में, मेथड रेफरेंस Java 8 वर्जन में लांच किया गया एक बेस्ट फीचर है। यह जावा डेवलपर को एक कस्टम यूजर डिफाइन लैम्ब्डा एक्सप्रेशन क्रिएट करने का एक छोटा और ज़्यादा इजीली आर्डर में रीड किया जा सकने वाला एक बेहतर सिंटेक्स या मेथड प्रोवाइड करता है. जब एक कस्टम लैम्ब्डा एक्सप्रेशन सिर्फ़ किसी मौजूदा मेथड को कॉल करता है।

तो उस लैम्ब्डा एक्सप्रेशन मे इस :: ऑपरेटर का यूज़ किया जाता है।
Syntax of a basic Java lambda method expression.
ClassName::methodName
For example, instead of Java user.
comp_name.forEach(comp_name -> System.out.println(comp_name));
We could also write it like this.
comp_name.forEach(System.out::println);
ये दोनों एक ही समान प्रोग्राम ऑपरेशन लॉजिक टास्क को परफॉर्म करते हैं।
Static method reference in Java.
Syntax of a static method.
ClassName::staticMethod
Example of a Java static method class.
import java.util.List;
import java.util.Arrays;
public class Main {
static void preview(String software) {
System.out.println(software);
}
public static void main(String[] args) {
List<String> softwares =
Arrays.asList(“Windows 11”, “Apple Mac”, “Linux”, “Tally”, “MS Word 2021”);
softwares.forEach(Main::preview);
}
}
In this example,
Main::preview
Here is a reference to the static preview() function method.
Equivalent lambda expression.
softwares.forEach(software -> Main.display(software));
Reference to an instance method of an object in Java.
Syntax of an instance method of an object.
object::instanceMethod
Example of an instance method of an object.
import java.util.List;
import java.util.Arrays;
class Msoffice {
void print(String text) {
System.out.println(text);
}
}
public class Main {
public static void main(String[] args) {
Msoffice msoffice = new Msoffice();
List<String> applications =
Arrays.asList(“Excel”, “Word”, “Powerpoint”, “Outlook”, “Visio”, “One Note”);
applications.forEach(msoffice::print);
}
}
Explain of above program.
class Msoffice {
void print(String text) {
System.out.println(text);
}
}
Using Objects in Java.
Msoffice msoffice= new Msoffice();
List<String> applications =
Arrays.asList(“Excel”, “Word”, “Powerpoint”, “Outlook”, “Visio”, “One Note”);
applications.forEach(msoffice::print);
The equivalent lambda expression is.
applications.forEach(application -> printer.print(application));
A reference to a class’s instance method in Java.
जावा प्रोग्राम में इंस्टैंस मेथड रेफ़्रेन्स का यूज़ तब किया जाता है, जब यूजर डिफाइन क्लास मेथड को हर ऑब्जेक्ट पर कॉल किया जाता है।
syntax for an instance method reference is.
ClassName::instanceMethod
Example of instance method reference.
List<String> city =
import java.util.List;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
List<String> city =
Arrays.asList(“Mumbai”, “Kolkata”, “Bengaluru”, “Chennai”, “Hyderabad”);
city.stream()
.map(String::toUpperCase)
.forEach(System.out::println);
}
}
Its output.
MUMBAI
KOLKATA
BENGALURU
CHENNAI
HYDERABAD
The equivalent lambda expression.
city.stream()
.map(city -> city.toUpperCase())
.forEach(city -> System.out.println(city));
A reference to a constructor in Java.
जावा प्रोग्राम में मेथड रेफरेंस का यूज़ कंस्ट्रक्टर को कॉल करने के लिए भी किया जा सकता है।
Syntax of a reference to a constructor.
ClassName::new
An example of a reference to a constructor.
List<String> country =
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<String> country =
Arrays.asList(“India”, “Switzerland”, “Denmark”, “Sweden”, “Germany”, “Netherlands”);
List<String> output =
country.stream()
.map(StringBuilder::new)
.map(StringBuilder::toString)
.collect(Collectors.toList());
output.forEach(System.out::println);
}
}
Here in this example.
StringBuilder::new
यह एक कंस्ट्रक्टर रेफरेंस मेथड है।
इसके बराबर लैम्ब्डा एक्सप्रेशन है.
country -> new StringBuilder(country)
Method reference with a comparator in Java.
जावा प्रोग्राम में मेथड रेफरेंस सामान्य रूप से सॉर्टिंग डाटा एलिमेंट मेथड के साथ यूज़ किए जाते हैं।
An example of Method reference with a comparator.
List<String> names =
import java.util.Arrays;
import java.util.List;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
List<String> spokenlanguage =
Arrays.asList(“English”, “Mandarin Chinese”, “Hindi”, “Spanish”, “French”, “Arabic”, “Bengali”);
// here it Sort alphabetically all spokenlanguage word
spokenlanguage.sort(String::compareTo);
System.out.println(“Ascending Alphabet sequence = ” + spokenlanguage);
// here it Sort by string length by each spokenlanguage word
spokenlanguage.sort(Comparator.comparingInt(String::length));
System.out.println(“Language Sorted by their length = ” + spokenlanguage);
}
}
Main Four Types of Java Method References
| # | Java Method Type | Type of Each Method Syntax | Detail Example |
| 1. | Static method | We use it for Static method – ClassName::staticMethod | Ex: Math::abs |
| 2. | Instance method of object | We use it for Instance method of object – object::method | Ex: System.out::println |
| 3. | Instance method of class | We use it for Instance method of class -ClassName::method | Ex: String::toUpperCase |
| 4. | Constructor | We use it for Constructor – ClassName::new | Ex: ArrayList::new |
Lambda vs. method reference expressions in Java.
Lambda expression.
numbers.forEach(n -> System.out.println(n));
Java Method Reference.
numbers.forEach(System.out::println);
Lambda expression.
country.stream()
.map(country -> country.toUpperCase())
.forEach(System.out::println);
Java Method Reference.
country.stream()
.map(String::toUpperCase)
.forEach(System.out::println);
Conclusion of Using method references.
जावा प्रोग्राम में मेथड रेफरेंस कोई नया मेथड नहीं क्रिएट करता है। यह बस किसी मौजूदा यूजर डिफाइन एक कस्टम मेथड को रेफर करने का एक स्माल मेथड प्रोवाइड करता है।
Lambda expression.
p -> object.method(p)
Java Method Reference.
object::method
जावा प्रोग्राम में मेथड रेफ़्रेन्स का यूज़ मुख्य रूप से Java 8 फंक्शनल प्रोग्रामिंग और स्ट्रीम कोड को स्माल करने और आसानी से रीड करने लायक बनाने में किया जाता है।

