Set<E>接口和List<E>接口的根本区别是什么?
当前回答
List Vs Set
1) Set不允许重复。列表允许重复。基于Set的实现,它还维护插入顺序。
LinkedHashSet。它维护插入顺序。请参考此处
2)包含方法。根据集合的性质,它将为访问提供更好的性能。最好的情况是o(1)但是List有调用contains的性能问题。
其他回答
List是一个有序的元素序列,而Set是一个无序的元素列表(谢谢你,Quinn Taylor)。
(<和>:
有序集合(也称为 序列)。本界面的用户 有精确的控制在哪里 列出插入的每个元素。的 用户可以通过它们来访问元素 整数索引(在列表中的位置), 并搜索列表中的元素。
Set <和>:
不包含 重复的元素。更正式, 集合不包含元素e1对 e2使得e1。等于(e2) at 最多一个空元素。正如所暗示的 的名称,此接口模拟 数学集合抽象。
主题名称:列表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
还有什么要补充的吗?请让我知道。
谢谢。
Set<E>和List<E>都用于存储E类型的元素。区别在于Set是以无序方式存储的,不允许重复值。List用于以有序的方式存储元素,并且允许重复值。
Set元素不能通过索引位置访问,而List元素可以通过索引位置访问。
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 |
这可能不是您想要的答案,但是集合类的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.
推荐文章
- 杰克逊克服下划线,支持骆驼案
- 如何分裂()一个分隔字符串到一个列表<字符串>
- List、List<?>, List<T>, List<E>, List<Object>
- 在Java中转换字符串到日历对象
- c#对象列表,我如何得到一个属性的和
- 在java中使用wait()和notify()的简单场景
- 增强的for循环中的Null检查
- c# vs Java Enum(适合c#新手)
- 如何在不强制转换的情况下将double转换为long ?
- for循环和for-each循环在性能上有区别吗?
- 你如何比较两个版本的字符串在Java?
- 为什么在Java和。net中不能修改字符串?
- java.util.Random真的那么随机吗?我怎么能生成52!(阶乘)可能的序列?
- ZoneOffset之间的区别是什么。UTC和ZoneId.of(“UTC”)?
- 类未找到:IntelliJ中的空测试套件