Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java Collections Map<Key,?> What does "?" refer to?

In Java Collections I saw something like this: Map<Key,?>. I don't know how it is working, can anyone help me out with this or provide an example?

like image 495
Gowtham Murugesan Avatar asked Mar 19 '15 14:03

Gowtham Murugesan


People also ask

What is a map key 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 does Map () do in Java?

A Map is an object that maps keys to values. A map cannot contain duplicate keys: Each key can map to at most one value. It models the mathematical function abstraction.

What is a key in HashMap?

The key of a value determines where in the table the value will be stored, by way of some hash function. They key is used in a hash in the same way that an index is used in an array: array[index] => some_value hash{key} => some_value.

What is key and value in HashMap?

The HashMap class of the Java collections framework provides the functionality of the hash table data structure. It stores elements in key/value pairs. Here, keys are unique identifiers used to associate each value on a map. The HashMap class implements the Map interface.


2 Answers

The question mark (?) represents an unknown type.

In your example, Map<Key, ?>, it means that it will match a map containing values of any type. It does not mean you can create a Map<Key, ?> and insert values of any type in it.

Quoting from the documentation:

In generic code, the question mark (?), called the wildcard, represents an unknown type. The wildcard can be used in a variety of situations: as the type of a parameter, field, or local variable; sometimes as a return type (though it is better programming practice to be more specific). The wildcard is never used as a type argument for a generic method invocation, a generic class instance creation, or a supertype.

For example, say you want to create a function that will print the values of any map, regardless of the value types:

static void printMapValues(Map<String, ?> myMap) {
    for (Object value : myMap.values()) {
        System.out.print(value + " ");
    }
}

Then call this function passing a Map<String, Integer> as argument:

Map<String, Integer> myIntMap = new HashMap<>();
myIntMap.put("a", 1);
myIntMap.put("b", 2);
printMapValues(myIntMap);

And you would get:

1 2

The wildcard allows you to call the same function passing a Map<String, String>, or any other value type, as argument:

Map<String, String> myStrMap = new HashMap<>();
myStrMap.put("a", "one");
myStrMap.put("b", "two");
printMapValues(myStrMap);

Result:

one two

This wildcard is called unbounded, since it gives no information about the type. There are a couple of scenarios where you may want to use the unbounded wildcard:

  • If you're calling no methods except those defined in the Object class.
  • When you're using methods that don't depend on the the type parameter, such as Map.size() or List.clear().

A wildcard can be unbounded, upper bounded, or lower bounded:

  • List<?> is an example of an unbounded wildcard. It represents a list of elements of unknown type.

  • List<? extends Number> is an example of an upper bounded wildcard. It matches a List of type Number, as well as its subtypes, such as Integer or Double.

  • List<? super Integer> is an example of a lower bounded wildcard. It matches a List of type Integer, as well as its supertypes, Number and Object.

like image 71
Anderson Vieira Avatar answered Sep 20 '22 16:09

Anderson Vieira


The Unknown Wildcard

? can be any dataType

List<?> means a list typed to an unknown type , This could be a List<Integer>, a List<Boolean>, a List<String> etc.

Now coming to your example Map<Key,?> means Value which is to be inserted in this map can be of any data Type.

like image 21
Neeraj Jain Avatar answered Sep 18 '22 16:09

Neeraj Jain