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


当前回答

List和Set都是接口。它们都扩展了Collection接口。set和list之间的重要区别是:

复制对象

List和Set之间的主要区别是List允许重复,而Set不允许重复。

订单

List是一个有序的集合,它维护插入顺序,这意味着在显示列表内容时,它将以它们插入到列表中的相同顺序显示元素。

Set是一个无序的集合,它不维护任何顺序。很少有维护顺序的Set实现,例如LinkedHashSet(它按照插入顺序维护元素)。

空元素

List允许任意数量的空元素。Set最多只能有一个null元素。

其他回答

Factor List Set
Is ordered grouping elements? YES NO
Provides positional access by index? YES NO
Can store duplicate elements? YES NO
Can store multiple null elements? YES NO
Childs: ArrayList, LinkedList, Vector, and Stack HashSet and LinkedHashSet

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是最好的方法。

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

实现使用

LinkedList ArrayList

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

实现使用

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

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

List

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

Set

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

订购…列表有顺序,集合没有。