List<?> 在java泛型中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1844770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does List<?> mean in java generics?
提问by corydoras
What does List<?>
mean, does it mean simply a list of objects of unspecified type?
是什么List<?>
意思,它是否只是一个未指定类型的对象列表?
Googling for the string <?>
returns nothing useful (:
谷歌搜索字符串<?>
返回任何有用的(:
回答by Tom Duckering
Sounds like you should look for some documentation on Java generics.
听起来您应该查找有关 Java 泛型的一些文档。
The List<?>
means that it is an object based on a currently unspecified type. That specification is made when the class is instantiated.
这List<?>
意味着它是一个基于当前未指定类型的对象。该规范是在实例化类时制定的。
For example:
例如:
List<String> listOfStrings = new ArrayList<String>();
is a list of String objects.
是一个字符串对象列表。
回答by Joy Dutta
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.
您可能正在查看基于模板的List
类。例如,您可以创建一个字符串列表List<String> myList = new MyList<String>();
。检查它支持的所有类型的文档。它应该支持任何对象类型,但如果有排序功能,您必须提供一些比较函数。
Note that in the example above MyList
is a concrete class that implements the List
interface in Java. It can be ArrayList
.
请注意,在上面的示例中,MyList
是一个List
在 Java中实现接口的具体类。它可以ArrayList
。
EDIT:
I assumed List
as a concrete class by mistake. Fixed the error above. Thanks Jon.
编辑:我List
错误地认为是一个具体的类。修复了上面的错误。谢谢乔恩。
回答by Jonathon Faust
List
is an interface you can implement yourself and also implemented by some of the Java collections, like Vector
.
List
是一个您可以自己实现的接口,也可以由一些 Java 集合实现,例如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.
您可以使用尖括号提供编译时类型信息。最通用的类型是Object
,也就是List<Object>
. 在<?>
你看到的是显示的一些子类的ListObject
或Object
。这就像说List<? extends Object>
, 或List<? extends Foo>
,其中List
包含某些子类的Foo
对象 或Foo
其自身的对象。
You can't instantiate a List
; it's an interface, not an implementation.
你不能实例化一个List
; 它是一个接口,而不是一个实现。
回答by Pops
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.
正如汤姆所说,?
或无界通配符表示未指定对象的类型。它可能是未知的,可能意味着多个可能的值,或者可能只是完全不相关。您的示例 ,List<?>
发音为“未知列表”。它很方便,因为它很灵活,但也存在一些缺陷,因为您无法将随机对象推入并从未知组中拉出而完全不受惩罚。
Resources:
资源:
- Wildcards are discussed herein 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 Javaby 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).
- 通配符讨论这里在Java教程。
- 有一个很好的 - 如果详细 - 由 Angelika Langer 提供的关于泛型的一般教程可以在这里找到。
- Gilad Bracha在这里有另一个很好的概述(PDF);查看第 5-7 页。
- 最后,如果你能接触到 Josh Bloch 的Effective Java,它有一个很好的部分关于泛型以及你可以、不能、应该和不应该使用通配符的情况(第 5 章,第 109-146 页)第二版)。
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.)
(编辑:我昨晚写这篇文章的时候一定很累。清理格式/添加了一些信息。)
回答by non sequitor
List<?>
stands for List<? extends Object>
so in Collection<E>
you will find containsAll(Collection<?> c)
which allows you to write
List<?>
代表List<? extends Object>
so inCollection<E>
你会发现containsAll(Collection<?> c)
它允许你写
List<Object> objs = Arrays.<Object>asList("one",2,3.14,4);
List<Integer> ints = Arrays.asList(2,4);
assert objs.containsAll(ints);//true
回答by Summy
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.
泛型为您提供了一种将集合的类型传达给编译器的方法,以便可以对其进行检查。一旦编译器知道集合的元素类型,编译器就可以检查您是否一致地使用了集合,并且可以对从集合中取出的值插入正确的转换。
回答by Farhad Maleki
To answer this question I need to explain Unbounded Wildcards and Bounded Wildcards.
The content of this post has been assembled from java documentation.
要回答这个问题,我需要解释无界通配符和有界通配符。
这篇文章的内容是从 java 文档中收集的。
1. Unbounded Wildcards
1. 无界通配符
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
orList.clear
. In fact,Class<?>
is so often used because most of the methods inClass<T>
do not depend onT
.
无界通配符类型使用通配符 (
?
)指定,例如,List<?>
。这称为未知类型的列表。在两种情况下,无界通配符是一种有用的方法:
如果您正在编写可以使用 Object 类中提供的功能实现的方法。
当代码使用不依赖于类型参数的泛型类中的方法时。例如,
List.size
或List.clear
。事实上,Class<?>
之所以经常使用,是因为中的大多数方法Class<T>
都不依赖于T
.
2. Bounded Wildcards
2. 有界通配符
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:
任何绘图通常都会包含许多形状。假设它们被表示为一个列表,那么在 Canvas 中有一个方法来绘制它们会很方便:
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 aList<Circle>
. That is unfortunate, since all the method does is read shapes from the list, so it could just as well be called on aList<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 typeList<Shape>
withList<? extends Shape>
. NowdrawAll()
will accept lists of any subclass ofShape
, so we can now call it on aList<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 boundof the wildcard.
现在,类型规则说
drawAll()
只能在完全形状的列表上调用它:例如,它不能在 a 上调用List<Circle>
。这很不幸,因为该方法所做的只是从列表中读取形状,因此也可以在List<Circle>
. 我们真正需要的是接受任何一种形状的列表的方法:public void drawAll(名单形状){...}这里有一个很小但很重要的区别:我们已更换型List<Shape>
用List<? extends Shape>
。现在drawAll()
将接受 的任何子类的列表Shape
,因此我们现在可以根据List<Circle>
需要在 a 上调用它。
List<? extends Shape>
是一个有界通配符的例子。The?
代表一个未知类型,然而,在这种情况下,我们知道这个未知类型实际上是 Shape 的一个子类型。(注意:它可以是 Shape 本身,也可以是某个子类;它不需要从字面上扩展 Shape。)我们说 Shape 是通配符的上限。
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.
同样地,语法? super T
,这是一个有界通配符,表示一个未知的类型,它是T. A的超类型ArrayedHeap280<? super Integer>
,例如,包括ArrayedHeap280<Integer>
,ArrayedHeap280<Number>
,和ArrayedHeap280<Object>
。正如您在Integer class的java 文档中看到的那样,Integer 是 Number 的子类,而 Number 又是 Object 的子类。
回答by Naresh Devarasetty
? is nothing but Wildcard in Generics
? 只不过是泛型中的通配符
There are 3 different kind of Wildcards in Generics
泛型中有 3 种不同类型的通配符
1) Upper Bounded Wildcards: Uses extends key word
1) Upper Bounded Wildcards:使用 extends 关键字
eg: List<? extends SuperClass>
2) Lower Bounded Wildcards
2) 下界通配符
eg:Uses Super key word List<? super SubClass>
3) Unbounded Wildcard
3) 无界通配符
List<?> list