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是一个无序的元素列表。
推荐文章
- 一对多、多对一、多对多的区别?
- 从枚举中选择一个随机值?
- 如何从URL获取参数与JSP
- 如何在Eclipse中生成Javadoc注释?
- 检查字符串是否包含字符串列表中的元素
- 找到java类从哪里加载
- 从集合中随机选取一个元素
- 为什么x == (x = y)和(x = y) == x不一样?
- 什么Java 8流。收集等价物可在标准Kotlin库?
- 等待未来的名单
- 如何检查JSON键是否存在?
- 为什么MongoDB Java驱动在条件中使用随机数生成器?
- 即使从未抛出异常,使用try-catch块的代价是否昂贵?
- 什么时候我们应该使用观察者和可观察对象?
- Java中的split()方法对点(.)不起作用。