Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 560
Tarik Avatar asked May 27 '12 22:05

Tarik


People also ask

What are the main differences between collection and collections?

Collection is the interface where you group objects into a single unit. Collections is a utility class that has some set of operations you perform on Collection. Collection does not have all static methods in it, but Collections consist of methods that are all static.

What is difference between collection and collections class in Java?

In Java, collection is an interface. In Java, collections is a utility class. 2. It showcases a set of individual objects as a single unit.

What are the three collection interfaces?

Set, List and Map are three important interfaces of the Java collection framework, and the difference between Set, List, and Map in Java is one of the most frequently asked Java Collection interview questions.


1 Answers

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     } } 
like image 164
meriton Avatar answered Oct 30 '22 18:10

meriton