What does List<?>
mean, does it mean simply a list of objects of unspecified type?
Googling for the string <?>
returns nothing useful (:
What does List<?>
mean, does it mean simply a list of objects of unspecified type?
Googling for the string <?>
returns nothing useful (:
Sounds like you should look for some documentation on Java generics.
The List<?>
means that it is an object based on a currently unspecified type. That specification is made when the class is instantiated.
For example:
List<String> listOfStrings = new ArrayList<String>();
is a list of String objects.
You are probably looking at the template based List
class. You can create a list of strings by List<String> myList = new MyList<String>();
as an example. Check the documentation for all the types it supports. It should support any object type, but if there is a sort functionality you have to supply some compare functions.
Note that in the example above MyList
is a concrete class that implements the List
interface in Java. It can be ArrayList
.
EDIT:
I assumed List
as a concrete class by mistake. Fixed the error above. Thanks Jon.
List
is an interface you can implement yourself and also implemented by some of the Java collections, like Vector
.
You can provide compile-time typing information using the angled brackets. The most generic type would be Object
, which would be List<Object>
. The <?>
you see is indicating a List of some subclass of Object
or an Object
. This is like saying List<? extends Object>
, or List<? extends Foo>
, where the List
contains objects of some subclass of Foo
or objects of Foo
itself.
You can't instantiate a List
; it's an interface, not an implementation.
As Tom said, the ?
, or unbounded wildcard, means that the type of the object is not specified. It could be unknown, could be meant for multiple possible values or might be just plain irrelevant. Your example, List<?>
, is pronounced "List of unknown." It's convenient because it's flexible, but there are also some pitfalls because you can't shove random objects in and pull them out of groups of unknown with total impunity.
Resources:
Incidentally, your Google search failed because Google doesn't truck with special characters:
With some exceptions, punctuation is ignored (that is, you can't search for @#$%^&*()=+[]\ and other special characters).
(EDIT: I must have been really tired when I wrote this last night. Cleaned up formatting/added a little info.)
List<?>
stands for List<? extends Object>
so in Collection<E>
you will find containsAll(Collection<?> c)
which allows you to write
List<Object> objs = Arrays.<Object>asList("one",2,3.14,4);
List<Integer> ints = Arrays.asList(2,4);
assert objs.containsAll(ints);//true
When you take an element out of a Collection, you must cast it to the type of element that is stored in the collection. Besides being inconvenient, this is unsafe. The compiler does not check that your cast is the same as the collection's type, so the cast can fail at run time.
Generics provides a way for you to communicate the type of a collection to the compiler, so that it can be checked. Once the compiler knows the element type of the collection, the compiler can check that you have used the collection consistently and can insert the correct casts on values being taken out of the collection.