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

What does Map<?, ?> Mean in Java?
I looked online, but could not find articles on it.

edit: I found this on MP3 Duration Java

+6
source share
6 answers

? indicates a placeholder whose value you are not interested in (wildcard):

 HashMap<?, ?> foo = new HashMap<Integer, String>(); 

And since ? is a wildcard, you can skip it and get the same result:

 HashMap foo = new HashMap<Integer, String>(); 

But they can be used to indicate or a subset of the generics to be used. In this example, the first family tree should implement the Serializable interface.

 // would fail because HttpServletRequest does not implement Serializable HashMap<? extends Serializable, ?> foo = new HashMap<HttpServletRequest, String>(); 

But it is always better to use specific classes instead of these wildcards. Should you use ? if you know what you are doing :)

+3
source

Map<?,?> Means that at compile time you do not know what the class type of the key and value Map object will be.

Its a wildcard type. http://download.oracle.com/javase/tutorial/extra/generics/wildcards.html

+10
source

Since Java5, Generics are provided with a language. http://download.oracle.com/javase/tutorial/java/generics/index.html

The designation means that the Map you create will accept a Class A object as a key and a Class B object as a value.

This will help you as a developer, so you no longer put the object in the correct class. Thus, you cannot use a card with keys other than A and objects other than B. Avoid ugly throws and provide compilation time limits.

+1
source

Map<?,?> Tells you that you can use each object for key and value on your map.

But it is usually useful to use such generics:

 Map<String, YourCustomObject> map 

So, on this map, you can only use put a String as a key and YourCustomObject as a value.

Take a look at the tutorial on generics.

+1
source

What? is a wildcard in generics. Sun ... er ... Oracle has a good generic tutorial here . There is a separate section about wildcards.

You will probably get a better answer if you put more context for your question, for example, where did you see Map<?, ?> .

0
source

Source: https://habr.com/ru/post/893280/


All Articles