What does List<?> mean in java generics?
JavaGenericsBounded WildcardJava Problem Overview
What does List<?>
mean, does it mean simply a list of objects of unspecified type?
Googling for the string <?>
returns nothing useful (:
Java Solutions
Solution 1 - Java
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).
(EDIT: I must have been really tired when I wrote this last night. Cleaned up formatting/added a little info.)
Solution 2 - Java
The keyword you need to get more information is Wildcards
Solution 3 - Java
To answer this question I need to explain Unbounded Wildcards and Bounded Wildcards.
The content of this post has been assembled from java documentation.
1. Unbounded Wildcards
>The unbounded wildcard type is specified using the wildcard character (?
), for example, List<?>
. This is called a list of unknown type. There are two scenarios where an unbounded wildcard is a useful approach:
>
> * If you are writing a method that can be implemented using functionality provided in the Object class.
>
> * When the code is using methods in the generic class that don't depend on the type parameter. For example, List.size
or List.clear
. In fact, Class<?>
is so often used because most of the methods in Class<T>
do not depend on T
.
2. Bounded Wildcards
>Consider a simple drawing application that can draw shapes such as rectangles and circles. To represent these shapes within the program, you could define a class hierarchy such as this: >
public abstract class Shape {
public abstract void draw(Canvas c);
}
public class Circle extends Shape {
private int x, y, radius;
public void draw(Canvas c) {
...
}
}
public class Rectangle extends Shape {
private int x, y, width, height;
public void draw(Canvas c) {
...
}
}
>These classes can be drawn on a canvas:
public class Canvas {
public void draw(Shape s) {
s.draw(this);
}
}
>Any drawing will typically contain a number of shapes. Assuming that they are represented as a list, it would be convenient to have a method in Canvas that draws them all:
public void drawAll(List<Shape> shapes) {
for (Shape s: shapes) {
s.draw(this);
}
}
>Now, the type rules say that drawAll()
can only be called on lists of exactly Shape: it cannot, for instance, be called on a List<Circle>
. That is unfortunate, since all the method does is read shapes from the list, so it could just as well be called on a List<Circle>
. What we really want is for the method to accept a list of any kind of shape:
public void drawAll(List shapes) {
...
}
>There is a small but very important difference here: we have replaced the type List<Shape>
with List<? extends Shape>
. Now drawAll()
will accept lists of any subclass of Shape
, so we can now call it on a List<Circle>
if we want.
>List<? extends Shape>
is an example of a bounded wildcard. The ?
stands for an unknown type, however, in this case, we know that this unknown type is in fact a subtype of Shape. (Note: It could be Shape itself, or some subclass; it need not literally extend Shape.) We say that Shape is the upper bound of the wildcard.
Similarly, the syntax ? super T
, which is a bounded wildcard, denotes an unknown type that is a supertype of T.
A ArrayedHeap280<? super Integer>
, for example, includes ArrayedHeap280<Integer>
, ArrayedHeap280<Number>
, and ArrayedHeap280<Object>
.
As you can see in the java documentation for Integer class, Integer is a subclass of Number that in turn is a subclass of Object.
Solution 4 - Java
List
: There is no type restriction and assignment restriction at all.List<Object>
: It seems to be used the same asList
, but a compilation error will occur when accepting other generic assignments.List<?>
: It is a generic type. Before assignment, it means that it can accept any type of set assignment, but after assignment, you can'tadd
elements to it, but you canremove
andclear
, not animmutable set
.List<?>
is generally used as a parameter to receive an external collection, or return a collection of specific element types, also known as awildcard collection
.
The test code and result as followed:
List a1 = new ArrayList();
a1.add(new Object());
a1.add(new Integer(10));
a1.add(new String("string"));
System.out.println("List is : " + a1);
List<?> a4 = a1;
a4.remove(0);
System.out.println("List is : " + a4);
System.out.println("List is : " + a4.get(0));
a4.clear();
System.out.println("List is : " + a4);
The result is :
List is : [java.lang.Object@2a139a55, 10, string]
List is : [10, string]
List is : 10
List is : []
Solution 5 - Java
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.
Solution 6 - Java
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.
Solution 7 - Java
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
Solution 8 - Java
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.
Solution 9 - Java
? is nothing but Wildcard in Generics
There are 3 different kind of Wildcards in Generics
-
Upper Bounded Wildcards: Uses extends key word
eg: List
-
Lower Bounded Wildcards
eg:Uses Super key word List
-
Unbounded Wildcard
List list
Solution 10 - Java
List is equivalent to List
> The wildcard ? extends Object is equivalent to the unbounded wildcard ?
>https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#:~:text=The%20wildcard%20?%20extends%20Object%20is%20equivalent%20to%20the%20unbounded%20wildcard%20?"><?></a> in java specification
Generics in java specification
Solution 11 - Java
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.