Java के Wildcards
In this page:
List<?> list // unknown type
List<? extends UpperBound> list // read-only producer
List<? super LowerBound> list // consumer
What is a Wildcard?
Generic code में, question mark (?) को wildcard कहा जाता है, और यह एक fixed type parameter के बजाय एक अज्ञात type parameter को represent करता है। Wildcards एक single method signature को callers को एक exact type में बंद करने के बजाय अलग-अलग, संबंधित parameter types की collections accept करने देते हैं।
उदाहरण: What is a Wildcard?
// Import java.util.List so it can be used by its short name
import java.util.List;
// Define the class Main
public class Main {
static void printSize(List<?> list) {
// Print a line to the console
System.out.println("Size: " + list.size());
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
printSize(List.of("a", "b", "c"));
printSize(List.of(1, 2));
}
}
Login to try C/C++/Java/PHP code in the editor
Unbounded Wildcards
एक unbounded wildcard एक single question mark से लिखा जाता है, जैसे List<?>, और यह किसी भी type parameter को represent करता है। यह उन methods के लिए सबसे उपयोगी है जिन्हें सिर्फ किसी collection के ज्ञात Object-level API से पढ़ना हो, जैसे उसका size print करना, elements असल में क्या हैं इसकी परवाह किए बिना।
उदाहरण: Unbounded Wildcards
// Import java.util.List so it can be used by its short name
import java.util.List;
// Define the class Main
public class Main {
static void printAll(List<?> list) {
// Print a line to the console
System.out.println("Items: " + list.size());
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
printAll(List.of(1, 2, 3));
}
}
Login to try C/C++/Java/PHP code in the editor
Upper Bounded Wildcards
एक upper bounded wildcard type parameter को किसी specific class या उसकी subclasses तक सीमित करता है, जिसे extends keyword से लिखा जाता है, उदाहरण के लिए <? extends Number>। यह किसी method को उस collection से values को उस upper-bound type के रूप में सुरक्षित रूप से पढ़ने देता है, क्योंकि हर संभव element की गारंटी है कि वह कम से कम उस type का होगा।
उदाहरण: Upper Bounded Wildcards
// Import java.util.List so it can be used by its short name
import java.util.List;
// Define the class Main
public class Main {
static double sum(List<? extends Number> nums) {
// Declare total and set it to 0
double total = 0;
for (Number n : nums) total += n.doubleValue();
// Send total back to the caller
return total;
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println(sum(List.of(1, 2, 3)));
}
}
Login to try C/C++/Java/PHP code in the editor
Lower Bounded Wildcards
एक lower bounded wildcard type parameter को किसी specific class या उसकी superclasses तक सीमित करता है, जिसे super keyword से लिखा जाता है, उदाहरण के लिए <? super Integer>। यह किसी method को उस type की values को collection में सुरक्षित रूप से लिखने देता है, क्योंकि किसी भी superclass container की गारंटी है कि वह उन्हें रख सकेगा।
उदाहरण: Lower Bounded Wildcards
// 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 void addNumbers(List<? super Integer> list) {
list.add(10);
list.add(20);
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new ArrayList object and store it in numbers
List<Number> numbers = new ArrayList<>();
addNumbers(numbers);
// Print a line to the console
System.out.println(numbers);
}
}
Login to try C/C++/Java/PHP code in the editor
Wildcard Guidelines (PECS)
PECS mnemonic संक्षेप में बताता है कि हर एक का उपयोग कब करें: Producer Extends, Consumer Super। एक extends wildcard का उपयोग करें जब कोई collection सिर्फ वे values produce करती हो जिन्हें आप पढ़ रहे हैं, और एक super wildcard का उपयोग करें जब कोई collection सिर्फ वे values consume करती हो जिन्हें आप उसमें डाल रहे हैं।
उदाहरण: Wildcard Guidelines (PECS)
import java.util.List;
public class Main {
static double sum(List<? extends Number> producer) { // Producer Extends
double total = 0;
for (Number n : producer) total += n.doubleValue();
return total;
}
static void fill(List<? super Integer> consumer) { // Consumer Super
consumer.add(1);
}
public static void main(String[] args) {
System.out.println(sum(List.of(1, 2, 3)));
}
}
Login to try C/C++/Java/PHP code in the editor
- किसी
List<?>याList<? extends Number>परlist.add(5)call करने की कोशिश करना, जो compile नहीं होता क्योंकि compiler exact element type नहीं जान सकता (सिर्फnullजोड़ा जा सकता है)। extendsऔरsuperको मिला देना: किसी collection से पढ़ने के लिए? extends Tऔर उसमें लिखने के लिए? super T(PECS) का उपयोग करें।- यह सोचना कि
List<?>औरList<Object>एक जैसे हैं, जबकि एकList<String>कोList<?>में पास किया जा सकता है लेकिनList<Object>में नहीं।
- Generics type safety बनाए रखते हुए classes और methods को अलग-अलग types के साथ काम करने देते हैं।
- Generic classes और generic methods एक बार लिखे जाते हैं और कई types के लिए reuse होते हैं।
- Wildcards तब flexibility देते हैं जब किसी method को कई संबंधित generic types accept करने होते हैं।
Chapter Quiz — Complete all 4 topics to unlock
0/4 topics done
Complete these topics first: