What is the difference between ? and Object in Java generics?

JavaGenerics

Java Problem Overview


I'm using Eclipse to help me clean up some code to use Java generics properly. Most of the time it's doing an excellent job of inferring types, but there are some cases where the inferred type has to be as generic as possible: Object. But Eclipse seems to be giving me an option to choose between a type of Object and a type of '?'.

So what's the difference between:

HashMap<String, ?> hash1;

and

HashMap<String, Object> hash2;

Java Solutions


Solution 1 - Java

An instance of HashMap<String, String> matches Map<String, ?> but not Map<String, Object>. Say you want to write a method that accepts maps from Strings to anything: If you would write

public void foobar(Map<String, Object> ms) {
    ...
}

you can't supply a HashMap<String, String>. If you write

public void foobar(Map<String, ?> ms) {
    ...
}

it works!

A thing sometimes misunderstood in Java's generics is that List<String> is not a subtype of List<Object>. (But String[] is in fact a subtype of Object[], that's one of the reasons why generics and arrays don't mix well. (arrays in Java are covariant, generics are not, they are invariant)).

Sample: If you'd like to write a method that accepts Lists of InputStreams and subtypes of InputStream, you'd write

public void foobar(List<? extends InputStream> ms) {
    ...
}

By the way: Joshua Bloch's Effective Java is an excellent resource when you'd like to understand the not so simple things in Java. (Your question above is also covered very well in the book.)

Solution 2 - Java

Another way to think about this problem is that

HashMap<String, ?> hash1;

is equivalent to

HashMap<String, ? extends Object> hash1;

Couple this knowledge with the "Get and Put Principle" in section (2.4) from Java Generics and Collections:

> The Get and Put Principle: use an > extends wildcard when you only get > values out of a structure, use super > wildcard when you only put values into > a structure, and don't use a wildcard > when you both get and put.

and the wild card may start making more sense, hopefully.

Solution 3 - Java

It's easy to understand if you remember that Collection<Object> is just a generic collection that contains objects of type Object, but Collection<?> is a super type of all types of collections.

Solution 4 - Java

The answers above covariance cover most cases but miss one thing:

"?" is inclusive of "Object" in the class hierarchy. You could say that String is a type of Object and Object is a type of ?. Not everything matches Object, but everything matches ?.

int test1(List<?> l) {
  return l.size();
}

int test2(List<Object> l) {
  return l.size();
}

List<?> l1 = Lists.newArrayList();
List<Object> l2 = Lists.newArrayList();
test1(l1);  // compiles because any list will work
test1(l2);  // compiles because any list will work
test2(l1);  // fails because a ? might not be an Object
test2(l2);  // compiled because Object matches Object

Solution 5 - Java

You can't safely put anything into Map<String, ?>, because you don't know what type the values are supposed to be.

You can put any object into a Map<String, Object>, because the value is known to be an Object.

Solution 6 - Java

Declaring hash1 as a HashMap<String, ?> dictates that the variable hash1 can hold any HashMap that has a key of String and any type of value.

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

All of the above is valid, because the variable map can store any of those hash maps. That variable doesn't care what the Value type is, of the hashmap it holds.

Having a wildcard does not, however, let you put any type of object into your map. as a matter of fact, with the hash map above, you can't put anything into it using the map variable:

map.put("A", new Integer(0));
map.put("B", new Object());
map.put("C", "Some String");

All of the above method calls will result in a compile-time error because Java doesn't know what the Value type of the HashMap inside map is.

You can still get a value out of the hash map. Although you "don't know the value's type," (because you don't know what type of hash map is inside your variable), you can say that everything is a subclass of Object and, so, whatever you get out of the map will be of the type Object:

HashMap<String, Integer> myMap = new HashMap<>();// This variable is used to put things into the map.

myMap.put("ABC", 10);

HashMap<String, ?> map = myMap;
Object output = map.get("ABC");// Valid code; Object is the superclass of everything, (including whatever is stored our hash map).

System.out.println(output);

The above block of code will print 10 to the console.


So, to finish off, use a HashMap with wildcards when you do not care (i.e., it does not matter) what the types of the HashMap are, for example:

public static void printHashMapSize(Map<?, ?> anyMap) {
	// This code doesn't care what type of HashMap is inside anyMap.
	System.out.println(anyMap.size());
}

Otherwise, specify the types that you need:

public void printAThroughZ(Map<Character, ?> anyCharacterMap) {
	for (int i = 'A'; i <= 'Z'; i++)
		System.out.println(anyCharacterMap.get((char) i));
}

In the above method, we'd need to know that the Map's key is a Character, otherwise, we wouldn't know what type to use to get values from it. All objects have a toString() method, however, so the map can have any type of object for its values. We can still print the values.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionskiphoppyView Question on Stackoverflow
Solution 1 - JavaJohannes WeissView Answer on Stackoverflow
Solution 2 - JavaJulien ChastangView Answer on Stackoverflow
Solution 3 - JavatopchefView Answer on Stackoverflow
Solution 4 - JavaEyalView Answer on Stackoverflow
Solution 5 - JavaericksonView Answer on Stackoverflow
Solution 6 - JavaKröwView Answer on Stackoverflow