What does List<?> mean in java generics?

2020-01-26 06:42发布

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

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

9条回答
▲ chillily
2楼-- · 2020-01-26 07:36

? is nothing but Wildcard in Generics

There are 3 different kind of Wildcards in Generics

1) Upper Bounded Wildcards: Uses extends key word

eg: List<? extends SuperClass>

2) Lower Bounded Wildcards

eg:Uses Super key word List<? super SubClass>

3) Unbounded Wildcard

List<?> list
查看更多
ら.Afraid
3楼-- · 2020-01-26 07:43

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.

查看更多
不美不萌又怎样
4楼-- · 2020-01-26 07:44

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.

查看更多
登录 后发表回答