9

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

Community
  • 1
  • 1
David
  • 19,577
  • 28
  • 108
  • 128

5 Answers5

12

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

Kal
  • 24,724
  • 7
  • 65
  • 65
  • Sorry, I'm not that good at generics but what are the differeces between `Map,?>` and `Map` ? – Eng.Fouad Jul 21 '11 at 14:53
  • 1
    `ClassName` is what you would write to "define" a class or interface. `ClassName>` is what you'd write to "use" the generic class in your program with an unknown (any) type. – Kainsin Jul 21 '11 at 14:56
9

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

Jasper
  • 2,166
  • 4
  • 30
  • 50
mana
  • 6,347
  • 6
  • 50
  • 70
  • 3
    The Raw type is NOT the same result as a wildcard! not at all! – Affe Jul 21 '11 at 17:10
  • Oh really? I did't know that. How do these first two cases differ from each other? – mana Jul 22 '11 at 10:27
  • 3
    The wild card means 'This is a Map that has some specific type of constrained thing in it, but I don't know what it is.' The raw type is just the raw type, there is nothing known about it, no constraints, there could be anything in there. The compiler won't let you add things to a Map,?>, but you can put anything you want in a raw Map. – Affe Jul 22 '11 at 16:03
1

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

The notation means that the Map you are creating, will accept an object of class A as a key and an object of class B as a value.

This helps you as a developer so you wont cast anymore an Object to the correct class. So you wont be able to use the map with keys other than A and objets other than B. Avoiding ugly casts and providing compile time constraints

marcelog
  • 7,062
  • 1
  • 33
  • 46
1

Map<?,?> tells you that you can use every object for key and value in your map.

But usually it is more useful to use generics like that:

Map<String, YourCustomObject> map

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

See this tutorial on generics.

powerMicha
  • 2,753
  • 1
  • 24
  • 31
0

The ? is the wildcard in generics. Sun...er...Oracle has a nice tutorial on generics here. There's a separate section on wildcards.

You'd probably get a better answer if you posted more context to your question, such as where you saw Map<?, ?>.

Paul
  • 19,704
  • 14
  • 78
  • 96