Java List 和 List
java中List
和List
What is the difference between List
and List<Object>
in java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
java中List
和List
What is the difference between List
and List<Object>
in java?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
假设您有两个列表:
List
和一个原始类型:
List rawList = new ArrayList();
现在让我们做这样的事情:
但是在某个地方还有一个列表其中包含字符串对象:
List; strList = new ArrayList()
。当我们尝试调用
strList.addAll(mainList)
时,我们收到编译错误:
The method addAll(Collection) in the type List;不适用于参数 (List)。
但是,如果我们尝试调用
strList.addAll(rawList)
,我们只会收到一条警告。
因此,如果您在应用程序中仅使用对象列表,那么 List 和 List 之间实际上没有区别。但从 Java 1.5 开始,使用泛型为应用程序提供编译时类型安全是一个好主意。
Suppose you've got two lists:
List<Object> mainList = new ArrayList<Object>();
and one with a raw type:
List rawList = new ArrayList();
Now let's do something like this:
But there is somewhere one more list which contains String objects:
List<String> strList = new ArrayList<String>()
.When we try to call
strList.addAll(mainList)
we get a Compilation Error :
The method addAll(Collection<? extends String>) in the type List<String> is not applicable for the arguments (List<Object>)
.But if we try to call
strList.addAll(rawList)
we get just a warning.
So if you are using only lists of objects in your application it's really no difference between List and List. But since Java 1.5 it's a good idea to use generics for providing compile-time type safety for your application.
原始类型(例如,没有
的
List
)的一个特殊危险是原始类型甚至在其自己的声明之外也禁用检查参考:Java显示原始类型危险的谜题。 (从第13页开始) 简短版本:因为在语句中使用了原始类型,所以编译器没有评估其他地方的泛型类型,因此发生了奇怪的运行时异常。
简短的回答:这是一个使用原始类型的警告,这是有充分理由的。使用无界通配符
List
优先于原始类型List
One particular danger of raw types (e.g.
List
without the<?>
) is that raw types disable checking even outside of their own declarationsRef: a Java Puzzler that shows the danger of raw types. (starts at page 13) Short version: because a raw type was used in a statement, a generic type elsewhere was not evaluated by the compiler, and thus bizarre runtime exceptions happened.
Short answer: it's a warning to use a raw type, for good reason. Use an unbounded wildcard
List<?>
in strong preference to a raw typeList
区别在于:
List与任何其他参数的
List
不兼容。但是,原始List
与所有参数的List
兼容。The difference is this:
List<Object>
is not compatible with aList
of any other parameter. However, a rawList
is compatible withList
s of all parameters.差异非常微妙。两者中更明显的是
List它是任意对象的列表(因为每个类都将 Object 作为超类)。现在与
List
的区别在于List
是无类型的,因此根本不执行类型检查,这最终会导致某些警告并可能导致奇怪的运行时行为。虽然 Java 知道List是一个可能包含任何内容的列表,但它不知道
List
的情况。因为如果您考虑“旧式”Java,您所知道的List
过去仅被称为List
。这就是这两者之间的区别...List被定义为故意包含各种对象,而
List
则不是't。我希望这能澄清问题。如果没有,请告诉我们还有哪些不清楚的地方。 :)
The difference is quite subtle. The more obivous of the two is
List<Object>
which ist a list of arbitrary objects (as every class has Object as super class). The difference toList
now is thatList
is untyped and therefore no type checks at all are performed, which ultimately leads to certain warnings and can lead to weird runtime behavior. While Java knows thatList<Object>
is a list that might contain anything, it doesn't know that about aList
. Because if you consider "old style" Java, what you know would callList<String>
used to be called onlyList
. And that's the difference between those two...List<Object>
is defined to contain a wide spectrum of objects on purpose, whileList
isn't.I hope that sort of clarifies the matter. If not, just tell us what's still unclear. :)
Java 中的泛型对于预先(在编译时)进行更好的错误检查非常重要,因此,在运行时检查和调试期间花费的时间更少。
因此,没有直接的性能提升,但您很快就能捕获错误。
底线:它有助于加快开发速度。
我强烈建议您阅读以下内容: Java 编程语言中的泛型 [pdf ]
Generics in Java is important for better error checking before hand (at compile time) due to which, less time is spent during runtime checking and debugging.
So, there is no direct performance gains, but you are quickly able to catch errors.
Bottom line: It helps in speed up development.
I strongly recommend you to go through this: Generics in the Java Programming Language [pdf]
对于原始类型有一些特殊的处理;没有动力将它们全部枚举,因为无论如何都不鼓励使用原始类型。
让我们讨论一个更狭窄的问题,这可能是最相关的:给定一个对象,我们主要关心我们可以在它上面调用什么方法;为此,如果其类型是
List
或List,有什么区别吗?
List的方法是
List
的方法,其中E替换为Object;原始类型List
的方法是List
方法的擦除。所以我们他们的方法大部分都是一样的!从这一点来看,这两种类型很难区分。
There are quiet some special treatments about raw types; there's no incentive to enumerate them all, since raw types are discouraged anyway.
Let's talk about a narrower problem, which is probably most pertinent: given an object, we mostly care about what methods we can invoke on it; for that purpose, is there any difference if its type is
List
orList<Object>
?The methods of
List<Object>
are methods ofList<E>
with E substituted with Object; The methods of raw typeList
is the erasure of methods ofList<E>
. So we havemost of their methods are the same! In that regard, the two types are hard to distinguish.