views:

518

answers:

7

What does List<?> mean, does it mean simply a list of objects of unspecified type?

Googling for the string <?> returns nothing useful (:

+8  A: 

The keyword you need to get more information is Wildcards

Chris
Or "tutorial" or "book".
Tom Hawtin - tackline
+1  A: 

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.

Tom Duckering
I don't see the connection btw `List<String> listOfStrings = new ArrayList<String>();` and `List<?>`
non sequitor
A: 

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.

Joy Dutta
You have to instantiate something that implements `List`, like `ArrayList` or `Vector`.
Donnie
Just to clarify, Java uses generics, not templates.
Graphics Noob
A: 

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.

Jonathon
+8  A: 

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:

  • Wildcards are discussed here in the Java tutorial.
  • There's a good -- if verbose -- tutorial on generics in general by Angelika Langer available here.
  • And there's another good overview here (PDF) by Gilad Bracha; check out pages 5-7.
  • Finally, if you can get your hands on Effective Java by Josh Bloch, it's got a great section on generics and the cases in which you can, can't, should and shouldn't use wildcards (chapter 5, pages 109-146 in the second edition).

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).

-Google help page

(EDIT: I must have been really tired when I wrote this last night. Cleaned up formatting/added a little info.)

Lord Torgamus
http://google.com/codesearch?q=%5C%3C%5C%3F%5C%3E+lang%3Ajava
jleedev
+1  A: 

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
non sequitor
A: 

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.

chk dis pdf

Summy