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


当前回答

集合是由不同对象组成的无序组——不允许有重复的对象。它通常使用被插入对象的哈希代码来实现。(特定的实现可能会添加排序,但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.

当我们讨论Java接口时,为什么不看看Javadoc呢?!

List是一个有序的集合(序列),通常允许 重复的 集合是不包含重复元素的集合,迭代 顺序可以由实现来保证

这里没有提到关于集合的缺乏顺序:这取决于实现。

列表:

列表通常允许重复对象。 列表必须是有序的,因此可以通过索引访问。

实现类包括:ArrayList, LinkedList, Vector

Set:

集合不允许重复对象。 大多数实现是无序的,但它是特定于实现的。

实现类包括: HashSet(无序) LinkedHashSet(命令), 树集(按自然顺序或按提供的比较器排序)

Set不能包含重复的元素,而List可以。List(在Java中)也意味着顺序。

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

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