Java中集合和列表有什么区别?
Java中Collection
和List
有什么区别?我什么时候应该使用哪个?
What is the difference between Collection
and List
in Java? When should I use which?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
首先:
List
< /a> 是一个集合
。然而,它是一个专门的Collection
。Collection
就是:项目的集合。您可以添加内容、删除内容、迭代内容以及查询其中有多少内容。List
将有关已定义的内容序列的信息添加到其中:您可以在位置 n 处获取元素,您可以在位置 n 处添加元素em>,您可以删除位置n处的元素。在
Collection
中,您不能这样做:“此集合中的第五个元素”未定义,因为没有定义的顺序。还有其他专门的集合,例如
Set
添加了一个功能,即它永远不会两次包含相同的元素。First off: a
List
is aCollection
. It is a specializedCollection
, however.A
Collection
is just that: a collection of items. You can add stuff, remove stuff, iterate over stuff and query how much stuff is in there.A
List
adds the information about a defined sequence of stuff to it: You can get the element at position n, you can add an element at position n, you can remove the element at position n.In a
Collection
you can't do that: "the 5th element in this collection" isn't defined, because there is no defined order.There are other specialized Collections as well, for example a
Set
which adds the feature that it will never contain the same element twice.集合
是java Collections 层次结构的根接口。列表
是一个子接口定义了一个有序的集合,其他子接口是Queue
通常会存储准备处理的元素(例如 堆栈)。下图演示了不同 java 集合类型之间的关系:
Collection
is the root interface to the java Collections hierarchy.List
is one sub interface which defines an ordered Collection, other sub interfaces areQueue
which typically will store elements ready for processing (e.g. stack).The following diagram demonstrates the relationship between the different java collection types:
Java API 是回答这个
集合的
列表(扩展集合)
Java API is the best to answer this
Collection
List (extends Collection)
List 和 Set 是 Collection 的两个子类。
在List中,数据是按特定顺序排列的。
在Set中,它不能两次包含相同的数据。
在Collection中,它只存储没有特定顺序的数据,并且可以包含重复数据。
List and Set are two subclasses of Collection.
In List, data is in particular order.
In Set, it can not contain the same data twice.
In Collection, it just stores data with no particular order and can contain duplicate data.
集合 是 List 的超级接口,因此每个 Java 列表也是集合的一个实例。集合只能按顺序迭代(没有特定的顺序),而列表允许通过 get(int index) 方法访问特定位置的元素。
Collection is the Super interface of List so every Java list is as well an instance of collection. Collections are only iterable sequentially (and in no particular order) whereas a List allows access to an element at a certain position via the
get(int index)
method.Collection是Java集合层次结构的主接口,List(Sequence)是定义有序集合的子接口之一。
Collection is the main interface of Java Collections hierarchy and List(Sequence) is one of the sub interfaces that defines an ordered collection.
Collection是一个高级接口,描述可以包含其他对象集合的Java对象。关于如何访问它们、同一对象的多个副本是否可以存在于同一集合中,或者顺序是否重要,这些都不是很具体。 List 具体来说是一个有序对象集合。如果您按特定顺序将对象放入列表中,它们将保持该顺序。
决定在哪里使用这两个接口远不如决定使用的具体实现重要。这将对程序的时间和空间性能产生影响。例如,如果您想要一个列表,您可以使用 ArrayList 或 LinkedList,其中每一个都会对应用程序产生影响。对于其他集合类型(例如集合),也适用类似的注意事项。
Collection is a high-level interface describing Java objects that can contain collections of other objects. It's not very specific about how they are accessed, whether multiple copies of the same object can exist in the same collection, or whether the order is important. List is specifically an ordered collection of objects. If you put objects into a List in a particular order, they will stay in that order.
And deciding where to use these two interfaces is much less important than deciding what the concrete implementation you use is. This will have implications for the time and space performance of your program. For example, if you want a list, you could use an ArrayList or a LinkedList, each of which is going to have implications for the application. For other collection types (e.g. Sets), similar considerations apply.