Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayList<T> vs ArrayList<?>

Tags:

java

generics

I know what ArrayList<T> is used for, but when should I use ArrayList<?> ? can you explain with example? thanks.

like image 659
Eng.Fouad Avatar asked Jul 21 '11 17:07

Eng.Fouad


People also ask

What is the difference between ArrayList and ArrayList <?> In Java?

The List is an interface, and the ArrayList is a class of Java Collection framework. The List creates a static array, and the ArrayList creates a dynamic array for storing the objects. So the List can not be expanded once it is created but using the ArrayList, we can expand the array when needed.

What is the difference between List <?> And List object?

means you can assign any type of List to it and List<Object> means you can store any type of object into it. That's the real difference between List<?> and List<Object> in Java.

What is the meaning of List <?> In Java?

In Java, a list interface is an ordered collection of objects in which duplicate values can be stored. Since a List preserves the insertion order, it allows positional access and insertion of elements. List interface is implemented by the following classes: ArrayList. LinkedList.


3 Answers

As far as I've been able to tell, ArrayList<?> basically tells the compiler:

Yes, I know that there is a generic version of ArrayList available to me, but I really, genuinely don't know what kind of objects I'm expecting to be in this one. So don't give me warnings that I'm not using generics the way I should be.

Update

I just learned that there is a real difference between using Raw Types (ArrayList) and generics with wildcards (ArrayList<?>) that goes beyond avoiding compiler warnings. Apparently once you declare something as a Raw type, no generics will work on any methods of that type, even if the generics weren't based on the type you omitted. See here for an example.

So while my original answer was generally correct, I thought it would be important to mention that using ArrayList<?> instead of ArrayList is more than just a matter of removing compiler warnings.

like image 68
StriplingWarrior Avatar answered Oct 16 '22 02:10

StriplingWarrior


http://download.oracle.com/javase/tutorial/java/generics/wildcards.html

Note: It's also possible to specify a lower bound by using the super keyword instead of extends. The code <? super Animal>, therefore, would be read as "an unknown type that is a supertype of Animal, possibly Animal itself". You can also specify an unknown type with an unbounded wildcard, which simply looks like <?>. An unbounded wildcard is essentially the same as saying <? extends Object>.

like image 39
padis Avatar answered Oct 16 '22 03:10

padis


ArrayList<?> indicates a collection of an unknown object, that is, it can be anything. It is possible to read from it, but you cannot write to it.

It sounds something like that:

I am a collection! I can read the unknown, but since I do not know what its type, I cannot add stuff

see this very useful tutorial by Oracle.

Also, I find these slides from an MIT Software Construction class very useful, and this generics tutorial.

like image 33
Sam Avatar answered Oct 16 '22 01:10

Sam