What does Map<?, ?>
mean in Java?
I've looked online but can't seem to find any articles on it.
edit : I found this on MP3 Duration Java
Java Map Interface. A map contains values on the basis of key, i.e. key and value pair. Each key and value pair is known as an entry. A Map contains unique keys. A Map is useful if you have to search, update or delete elements on the basis of a key.
Map. entrySet() method in Java is used to create a set out of the same elements contained in the map. It basically returns a set view of the map or we can create a new set and store the map elements into them. Syntax: map.entrySet()
The get() method of Map interface in Java is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. It returns NULL when the map contains no such mapping for the key.
HashMap. values() method of HashMap class in Java is used to create a collection out of the values of the map. It basically returns a Collection view of the values in the HashMap. Syntax: Hash_Map.values()
Map<?,?>
means that at compile time, you do not know what the class type of the key and value object of the Map is going to be.
Its a wildcard type. http://download.oracle.com/javase/tutorial/extra/generics/wildcards.html
?
indicates a placeholder in whose value you are not interested in (a wildcard):
HashMap<?, ?> foo = new HashMap<Integer, String>();
And since ?
is a wildcard, you can skip it and still get the same result:
HashMap foo = new HashMap<Integer, String>();
But they can be used to specify or subset the generics to be used. In this example, the first generic must implement the Serializable interface.
// would fail because HttpServletRequest does not implement Serializable
HashMap<? extends Serializable, ?> foo = new HashMap<HttpServletRequest, String>();
But it's always better to use concrete classes instead of these wildcards. You should only use ?
if you know what your are doing :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With