I am mainly a C# developer and I was teaching Data Structures to my friend and they use Java in their University and I saw such an expression in Java:
void printCollection(Collection<?> c) {
for (Object e : c) {
System.out.println(e);
}
}
I haven't seen such a thing in C# so I wonder what's the difference between Collection<T>
and Collection<?>
in Java?
void printCollection(Collection<T> c) {
for (Object e : c) {
System.out.println(e);
}
}
I think it could have been written in the way above too. The guy in the documentation was comparing Collection<Object>
and Collection<T>
though.
Examples are taken from http://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html
The declaration
Collection<?>
(pronounced "collection of unknown") is a collection whose element type matches anything, whereasCollection<T>
stands for a collection of typeT
.As usual Angelika Langer's FAQ on generics has an extensive discussion on the topic, a must-read for fully understanding all about generics in Java, and unbounded wildcards (the subject of this question) in particular. Quoting from the FAQ:
For further technical details, check out the section §4.5.1 Type Arguments and Wildcards of the Java Language Specification, which states that:
The one using the unbounded wildcard (?) actually means
? extends Object
(anything that extends Object).This, in Java, implies a read-only nature, namely, we are allowed to read items from the generic structure, but we are not allowed to put anything back in it, because we cannot be certain of the actual type of the elements in it.
Therefore, I daresay it is a very valid approach in the
printCollection
method under discussion, at least, until we meet a situation in which we need to assume a type.If had to choose between them two, I would say that the second one (with type parameter T) is a cleaner approach because you can at least assume that the collection has a certain type
T
, and that can prove to be useful in certain scenarios.For example, if the Collection needed to be synchronized, I could simply do:
I very easily created a new collection of type
T
, and copy all elements of the original collection into this second one. This I can do, because I can assume the type of the collection isT
, and not?
.I could, of course, do the same thing with the first method (using the ubounded wildcard), but it's not so clean, I had to make assumptions that the type of the Collection is Object, and not
?
(which cannot be validly used as type argument:).new ArrayList<?>()
Collection<?>
is a collection of unknown type parameter.As far as the caller is concerned, there is no difference between
and
However, the latter allows the implementation to refer to the collection's type parameter and is therefore often preferred.
The former syntax exists because it is not always possible to introduce a type parameter at the proper scope. For instance, consider:
If I were to replace
?
by some type parameterT
, all sets insets
would be restricted to the same component type, i.e. I can no longer put sets having different element types into the same list, as evidenced by the following attempt:With
Collection<T>
you could doWith
Collection<?>
you only know the collection contains objects.