Java中的集合和列表有什么区别?

时间:2021-03-22 13:27:29

What is the difference between Collection and List in Java? When should I use which?

Java中的集合和列表有什么区别?我应该什么时候使用它?

7 个解决方案

#1


201  

First off: a List is a Collection. It is a specialized Collection, 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.

一个列表向它添加关于已定义序列的信息:您可以在位置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.

在集合中,您不能这样做:“这个集合中的第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.

还有其他专门的集合,例如一个集合,它添加了一个特性,即它永远不会包含相同的元素两次。

#2


143  

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).

集合是java集合层次结构的根接口。List是一个定义有序集合的子接口,其他的子接口是队列,通常会存储要处理的元素(例如堆栈)。

The following diagram demonstrates the relationship between the different java collection types:

下图展示了不同java集合类型之间的关系:

Java中的集合和列表有什么区别?

#3


7  

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 = e2 (e2),如果它们允许null元素,它们通常允许多个null元素。当用户试图插入时抛出运行时异常,有人可能希望实现禁止重复的列表,这并不是不可想象的,但是我们期望这种用法很少见。

#4


2  

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.

Collection是描述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,其中每个都将对应用程序产生影响。对于其他集合类型(例如集合),也适用类似的考虑。

#5


2  

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.

集合是列表的超级接口,所以每个Java列表都是集合的实例。集合只能按顺序迭代(而且没有特定的顺序),而List允许通过get(int index)方法访问某个位置的元素。

#6


2  

Collection is the main interface of Java Collections hierarchy and List(Sequence) is one of the sub interfaces that defines an ordered collection.

集合是Java集合层次结构的主要接口,列表(Sequence)是定义有序集合的子接口之一。

#7


2  

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.

在Set中,它不能包含两次相同的数据。

In Collection, it just stores data with no particular order and can contain duplicate data.

在集合中,它只存储没有特定顺序的数据,并且可以包含重复的数据。

#1


201  

First off: a List is a Collection. It is a specialized Collection, 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.

一个列表向它添加关于已定义序列的信息:您可以在位置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.

在集合中,您不能这样做:“这个集合中的第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.

还有其他专门的集合,例如一个集合,它添加了一个特性,即它永远不会包含相同的元素两次。

#2


143  

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).

集合是java集合层次结构的根接口。List是一个定义有序集合的子接口,其他的子接口是队列,通常会存储要处理的元素(例如堆栈)。

The following diagram demonstrates the relationship between the different java collection types:

下图展示了不同java集合类型之间的关系:

Java中的集合和列表有什么区别?

#3


7  

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 = e2 (e2),如果它们允许null元素,它们通常允许多个null元素。当用户试图插入时抛出运行时异常,有人可能希望实现禁止重复的列表,这并不是不可想象的,但是我们期望这种用法很少见。

#4


2  

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.

Collection是描述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,其中每个都将对应用程序产生影响。对于其他集合类型(例如集合),也适用类似的考虑。

#5


2  

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.

集合是列表的超级接口,所以每个Java列表都是集合的实例。集合只能按顺序迭代(而且没有特定的顺序),而List允许通过get(int index)方法访问某个位置的元素。

#6


2  

Collection is the main interface of Java Collections hierarchy and List(Sequence) is one of the sub interfaces that defines an ordered collection.

集合是Java集合层次结构的主要接口,列表(Sequence)是定义有序集合的子接口之一。

#7


2  

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.

在Set中,它不能包含两次相同的数据。

In Collection, it just stores data with no particular order and can contain duplicate data.

在集合中,它只存储没有特定顺序的数据,并且可以包含重复的数据。