Java के Generic Methods
In this page:
public <T> returnType methodName(T parameter) {
// body
}
methodName(argument);
What is a Generic Method?
एक generic method अपने ही signature में अपने खुद के अलग type parameters declare करता है, enclosing class के किसी type parameters से अलग। यह एक single method को किसी भी data type को accept और उसके साथ काम करने देता है, भले ही एक class के अंदर जो generic बिल्कुल नहीं है।
उदाहरण: What is a Generic Method?
// Define the class Main
public class Main {
static <T> void show(T item) {
// Print a line to the console
System.out.println("Value: " + item);
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
show("text");
show(42);
}
}
Login to try C/C++/Java/PHP code in the editor
Declaring Type Parameters
एक generic method define करने के लिए, declaration line में method के return type से ठीक पहले अपने type parameters को angle brackets में रखें, जैसे <T>। यह compiler को बताता है कि parameter सिर्फ उस एक method call तक सीमित है, पूरी class तक नहीं।
उदाहरण: Declaring Type Parameters
// Define the class Main
public class Main {
static <T> T identity(T value) {
// Send value back to the caller
return value;
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println(identity("scoped to this method"));
}
}
Login to try C/C++/Java/PHP code in the editor
Bounded Type Methods
आप bounded types का उपयोग करके किसी generic method के parameters को सीमित कर सकते हैं, यह सुनिश्चित करते हुए कि method सिर्फ किसी specific class की subclasses या किसी specific interface के implementers को accept करे। यह खासतौर पर तब उपयोगी है जब method के body को किसी ऐसे method को call करना हो जो सिर्फ उस bound पर मौजूद हो, जैसे Comparable पर compareTo()।
उदाहरण: Bounded Type Methods
// Define the class Main
public class Main {
static <T extends Comparable<T>> T max(T a, T b) {
// Send a.compareTo(b) > 0 ? a : b back to the caller
return a.compareTo(b) > 0 ? a : b;
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println(max(5, 9));
}
}
Login to try C/C++/Java/PHP code in the editor
Multiple Method Parameters
Generic methods एक साथ कई type parameters declare कर सकती हैं, angle brackets के अंदर commas से अलग करते हुए, जैसे <T, U>, जब method को दो अलग-अलग input types को एक-दूसरे से जोड़ना हो। एक classic उदाहरण है ऐसा method जो एक typed list से elements को एक अलग-typed destination list में copy करता है।
उदाहरण: Multiple Method Parameters
// Import java.util.List so it can be used by its short name
import java.util.List;
// Import java.util.ArrayList so it can be used by its short name
import java.util.ArrayList;
// Define the class Main
public class Main {
static <T, U> void copyLabeled(List<T> source, List<U> dest, U label) {
for (T item : source) dest.add(label);
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
List<Integer> nums = List.of(1, 2, 3);
// Create a new ArrayList object and store it in labels
List<String> labels = new ArrayList<>();
copyLabeled(nums, labels, "n");
// Print a line to the console
System.out.println(labels);
}
}
Login to try C/C++/Java/PHP code in the editor
Type Inference
ज़्यादातर generic methods को call करते समय आपको explicitly type parameters specify करने की ज़रूरत नहीं है, क्योंकि Java compiler आपके पास किए arguments से data type को अपने आप infer कर लेता है। यह type inference ही generic method calls को साधारण method calls जितना साफ़ रखता है।
उदाहरण: Type Inference
public class Main {
static <T> T pick(T a, T b) {
return a;
}
public static void main(String[] args) {
String result = pick("first", "second"); // no <String> needed, inferred
System.out.println(result);
}
}
Login to try C/C++/Java/PHP code in the editor
- return type से पहले
<T>declare करना भूल जाना, जैसेstatic void show(T item), जोTके लिए 'cannot find symbol' error के साथ fail हो जाता है। - किसी unbounded
Tपरa.compareTo(b)call करना, जो compile नहीं होता; इसे उपयोग करने के लिए<T extends Comparable<T>>declare करें। T[]लेने वाले method कोint[]पास करना, जो काम नहीं करता क्योंकिTको reference type होना चाहिए;Integer[]का उपयोग करें।
Chapter Quiz — Complete all 4 topics to unlock
0/4 topics done
Complete these topics first: