Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Map<?, ?> mean in Java?

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

like image 408
david99world Avatar asked Jul 21 '11 14:07

david99world


People also ask

What is map <? ?> In 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.

What is map entrySet in Java?

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()

What is MAP key return?

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.

What is map values in Java?

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()


2 Answers

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

like image 171
Kal Avatar answered Sep 26 '22 06:09

Kal


? 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 :)

like image 20
mana Avatar answered Sep 25 '22 06:09

mana