Set<E>接口和List<E>接口的根本区别是什么?


当前回答

List Vs Set

1) Set不允许重复。列表允许重复。基于Set的实现,它还维护插入顺序。

LinkedHashSet。它维护插入顺序。请参考此处

2)包含方法。根据集合的性质,它将为访问提供更好的性能。最好的情况是o(1)但是List有调用contains的性能问题。

其他回答

List

是元素的有序分组。 List用于收集重复的元素。 新方法在List中定义 接口。

Set

是元素的无序分组。 Set用于收集没有重复项的元素。 在Set接口中没有定义任何新方法,因此我们只能对Set子类使用Collection接口方法。

List是一个有序的元素序列,而Set是一个无序的元素列表(谢谢你,Quinn Taylor)。

(<和>:

有序集合(也称为 序列)。本界面的用户 有精确的控制在哪里 列出插入的每个元素。的 用户可以通过它们来访问元素 整数索引(在列表中的位置), 并搜索列表中的元素。

Set <和>:

不包含 重复的元素。更正式, 集合不包含元素e1对 e2使得e1。等于(e2) at 最多一个空元素。正如所暗示的 的名称,此接口模拟 数学集合抽象。

1.List允许重复值,set不允许重复值

2.List维护您在列表中插入元素的顺序 Set不能维持秩序。 3.List是一个有序的元素序列,而Set是一个无序的元素列表。

这可能不是您想要的答案,但是集合类的JavaDoc实际上非常具有描述性。复制/粘贴:

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.

Set:

不能有重复值 排序取决于实现。默认情况下,它不是有序的 不能通过索引进行访问

列表:

可以有重复的值 默认订购 可以通过索引访问吗