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

发布于 2024-09-11 02:42:47 字数 71 浏览 4 评论 0原文

Java中CollectionList有什么区别?我什么时候应该使用哪个?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

魄砕の薆 2024-09-18 02:42:47

首先: List< /a> 是一个 集合 。然而,它是一个专门的Collection

Collection 就是:项目的集合。您可以添加内容、删除内容、迭代内容以及查询其中有多少内容。

List 将有关已定义的内容序列的信息添加到其中:您可以在位置 n 处获取元素,您可以在位置 n 处添加元素em>,您可以删除位置n处的元素。

Collection 中,您不能这样做:“此集合中的第五个元素”未定义,因为没有定义的顺序。

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

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.

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.

审判长 2024-09-18 02:42:47

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

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

java 集合

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

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

java collections

魔法唧唧 2024-09-18 02:42:47

Java API 是回答这个

集合

最佳选择

集合中的根接口
等级制度。一个集合代表一个
一组对象,称为它的
元素。一些集合允许
重复元素,而其他元素则不然。
有些是有序的,有些是无序的。
JDK没有提供任何直接的
该接口的实现:
提供更多的实现
特定的子接口,例如 Set 和
列表。该接口通常使用
传递集合并
最大限度地操纵它们
需要通用性。

列表(扩展集合)

有序集合(也称为
顺序)。该界面的使用者
可以精确控制其中的位置
列出每个插入的元素。这
用户可以通过其访问元素
整数索引(列表中的位置),
并在列表中搜索元素。

与集合不同,列表通常允许
重复的元素。更正式地说,
列表通常允许成对
元素 e1 和 e2 使得
e1.equals(e2),它们通常
允许多个 null 元素,如果它们
完全允许空元素。它不是
难以想象有人会希望
实施禁止清单
重复,通过抛出运行时
当用户尝试时发生异常
插入它们,但我们期望这种用法
是罕见的。

Java API is the best to answer this

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.

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.

安静被遗忘 2024-09-18 02:42:47

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.

秋日私语 2024-09-18 02:42:47

集合 是 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.

瑶笙 2024-09-18 02:42:47

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.

爱你不解释 2024-09-18 02:42:47

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文