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


当前回答

元素的有序列表(是否唯一) 遵循Java的名为List的接口 可以通过索引访问吗

实现使用

LinkedList ArrayList

独特元素列表: 遵循Java名为Set的接口 不能通过索引访问

实现使用

HashSet(无序) LinkedHashSet(命令) 树集(按自然顺序或按提供的比较器排序)

接口Set和List都符合Java的名为Collection的接口

其他回答

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

(<和>:

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

Set <和>:

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

Java中List和Set之间的一些值得注意的区别如下:

1) Java中的List和Set之间的基本区别是允许重复元素。Java中的List允许重复,而Set不允许任何重复。如果在Set中插入duplicate,它将替换旧的值。Java中Set的任何实现都只包含唯一的元素。

2) Another significant difference between List and Set in Java is order. List is an Ordered Collection while Set is an unordered Collection. List maintains insertion order of elements, means any element which is inserted before will go on lower index than any element which is inserted after. Set in Java doesn't maintain any order. Though Set provide another alternative called SortedSet which can store Set elements in specific Sorting order defined by Comparable and Comparator methods of Objects stored in Set.

3) Java中比较流行的List接口实现有ArrayList, Vector和LinkedList。而流行的Set接口实现包括HashSet, TreeSet和LinkedHashSet。

很明显,如果你需要维护插入顺序或对象,你的集合可以包含重复的列表是一种方法。另一方面,如果您的要求是保持唯一的集合,没有任何重复,那么Set是最好的方法。

集合是由不同对象组成的无序组——不允许有重复的对象。它通常使用被插入对象的哈希代码来实现。(特定的实现可能会添加排序,但Set接口本身没有。)

列表是一组有序的对象,其中可能包含重复项。它可以用数组列表、链表等来实现。

主题名称:列表VS集

我刚刚介绍了Java中最重要的主题——集合框架。我想和你分享我关于收藏的一点知识。其中最重要的是列表、集合、映射。让我们从List和Set开始。

List与Set的区别:

List is a collection class which extends AbstractList class where as Set is a collection class which extends AbstractSet class but both implements Collection interface. List interface allows duplicate values (elements) whereas Set interface does not allow duplicate values. In case of duplicate elements in Set, it replaces older values. List interface allows NULL values where as Set interface does not allow Null values. In case of using Null values in Set it gives NullPointerException. List interface maintains insertion order. That means the way we add the elements in the List in the same way we obtain it using iterator or for-each style. Whereas Set implementations do not necessarily maintain insertion order. (Although SortedSet does using TreeSet, and LinkedHashSet maintains insertion order). List interface has its own methods defined whereas Set interface does not have its own method so Set uses Collection interface methods only. List interface has one legacy class called Vector whereas Set interface does not have any legacy class Last but not the least... The listIterator() method can only be used to cycle through the elements within List Classes whereas we can use iterator() method to access Set class elements

还有什么要补充的吗?请让我知道。

谢谢。

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

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

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