Java中的集合和列表有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3317381/
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 is the difference between Collection and List in Java?
提问by Truong Ha
What is the difference between Collection
and List
in Java? When should I use which?
JavaCollection
和List
Java 中的区别是什么?我什么时候应该使用哪个?
采纳答案by Joachim Sauer
First off: a List
is a Collection
. It is a specialized Collection
, however.
首先: aList
是 a Collection
。Collection
然而,它是一个专门的。
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.
ACollection
就是这样:项目的集合。你可以添加东西、删除东西、迭代东西并查询里面有多少东西。
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.
AList
向其添加有关已定义内容序列的信息:您可以获得位置n的元素,您可以添加位置n的元素,您可以删除位置n的元素。
In a Collection
you can't do that: "the 5th element in this collection" isn't defined, because there is no defined order.
在 aCollection
你不能这样做:“这个集合中的第 5 个元素”没有定义,因为没有定义的顺序。
There are other specialized Collections as well, for example a Set
which adds the feature that it will never contain the same element twice.
还有其他专门的集合,例如 aSet
它添加了永远不会包含相同元素两次的功能。
回答by krock
Collection
is the root interface to the java Collections hierarchy. List
is one sub interface which defines an ordered Collection, other sub interfaces are Queue
which typically will store elements ready for processing (e.g. stack).
Collection
是 java 集合层次结构的根接口。 List
是一个定义有序集合的子接口,其他子接口 Queue
通常将存储准备处理的元素(例如stack)。
The following diagram demonstrates the relationship between the different java collection types:
下图展示了不同java集合类型之间的关系:
回答by Gian
Collectionis 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. Listis specifically an orderedcollection of objects. If you put objects into a List in a particular order, they will stay in that order.
集合是描述 Java 对象的高级接口,这些对象可以包含其他对象的集合。关于它们的访问方式,同一对象的多个副本是否可以存在于同一个集合中,或者顺序是否重要,并不是很具体。 列表特别是对象的有序集合。如果您按特定顺序将对象放入 List 中,它们将保持该顺序。
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.
决定在何处使用这两个接口远不如决定您使用的具体实现重要。这将对程序的时间和空间性能产生影响。例如,如果您想要一个列表,您可以使用一个 ArrayList 或一个 LinkedList,它们中的每一个都会对应用程序产生影响。对于其他集合类型(例如集合),类似的考虑也适用。
回答by Daff
Collectionis 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是 List 的超级接口,所以每个 Java 列表都是一个集合实例。集合只能按顺序迭代(并且没有特定的顺序),而 List 允许通过该get(int index)
方法访问某个位置的元素。
回答by Eugene Ryzhikov
Java API is the best to answer this
Java API 是回答这个问题的最佳选择
Collection
收藏
The root interface in the collection hierarchy. A collection represents a group of objects, known as its elements. Some collections allow duplicate elements and others do not. Some are ordered and others unordered. The JDK does not provide any direct implementations of this interface: it provides implementations of more specific subinterfaces like Set and List. This interface is typically used to pass collections around and manipulate them where maximum generality is desired.
集合层次结构中的根接口。一个集合代表一组对象,称为它的元素。一些集合允许重复元素,而另一些则不允许。有些是有序的,有些是无序的。JDK 不提供此接口的任何直接实现:它提供了更具体的子接口(如 Set 和 List)的实现。此接口通常用于传递集合并在需要最大通用性的地方操作它们。
List(extends Collection)
列表(扩展集合)
An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.
Unlike sets, lists typically allow duplicate elements. More formally, lists typically allow pairs of elements e1 and e2 such that e1.equals(e2), and they typically allow multiple null elements if they allow null elements at all. It is not inconceivable that someone might wish to implement a list that prohibits duplicates, by throwing runtime exceptions when the user attempts to insert them, but we expect this usage to be rare.
有序集合(也称为序列)。此界面的用户可以精确控制每个元素在列表中的插入位置。用户可以通过它们的整数索引(在列表中的位置)访问元素,并在列表中搜索元素。
与集合不同,列表通常允许重复元素。更正式地说,列表通常允许元素对 e1 和 e2,这样 e1.equals(e2),并且如果它们完全允许 null 元素,它们通常允许多个 null 元素。有人可能希望通过在用户尝试插入它们时抛出运行时异常来实现一个禁止重复的列表,这并非不可想象,但我们希望这种用法很少见。
回答by Ramya
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 Collections 层次结构的主要接口,List(Sequence) 是定义有序集合的子接口之一。
回答by Neeraj Bansal
List and Set are two subclasses of Collection.
List 和 Set 是 Collection 的两个子类。
In List, data is in particular order.
在 List 中,数据按特定顺序排列。
In Set, it can not contain the same data twice.
在 Set 中,不能两次包含相同的数据。
In Collection, it just stores data with no particular order and can contain duplicate data.
在 Collection 中,它只是存储没有特定顺序的数据,并且可以包含重复的数据。