What is the difference between Collection<?> and Collection<T>

1.8k views Asked by At

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

5

There are 5 answers

1
meriton On BEST ANSWER

Collection<?> is a collection of unknown type parameter.

As far as the caller is concerned, there is no difference between

void printCollection(Collection<?> c) { ... }

and

<T> void printCollection(Collection<T> c) { ... }

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:

List<Set<?>> sets = new ArrayList<>();
sets.add(new HashSet<String>());
sets.add(new HashSet<Integer>());

If I were to replace ? by some type parameter T, all sets in sets 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:

class C<T extends String> {
    List<Set<T>> sets = new ArrayList<>();

    public C() {
        sets.add(new HashSet<String>()); // does not compile
        sets.add(new HashSet<Integer>()); // does not compile
    }
}
2
Óscar López On

The declaration Collection<?> (pronounced "collection of unknown") is a collection whose element type matches anything, whereas Collection<T> stands for a collection of type T.

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:

The unbounded wildcard looks like " ? " and stands for the family of all types. The unbounded wildcard is used as argument for instantiations of generic types. The unbounded wildcard is useful in situations where no knowledge about the type argument of a parameterized type is needed

For further technical details, check out the section §4.5.1 Type Arguments and Wildcards of the Java Language Specification, which states that:

Type arguments may be either reference types or wildcards. Wildcards are useful in situations where only partial knowledge about the type parameter is required.

2
Don Roby On

With Collection<T> you could do

void printCollection(Collection<T> c) {
    for (T e : c) {
        System.out.println(e);
    }
}

With Collection<?> you only know the collection contains objects.

0
Edwin Dalorzo On

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:

<T> void printCollection(Collection<T> c) {
    List<T> copy = new ArrayList<T>();
    synchronized(c) {
        copy.addAll(c);
    }
    for (T e : copy) {
        System.out.println(e);
    }
}

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 is T, 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<?>()).

void printCollection(Collection<?> c) {
    List<Object> copy = new ArrayList<Object>();
    synchronized(c) {
        copy.addAll(c);
    }
    for (Object e : copy) {
        System.out.println(e);
    }
}
0
My note Note pro On

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 is T, 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<?>()).