Set<E>接口和List<E>接口的根本区别是什么?
当前回答
这可能不是您想要的答案,但是集合类的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和Set都是接口。它们都扩展了Collection接口。set和list之间的重要区别是:
复制对象
List和Set之间的主要区别是List允许重复,而Set不允许重复。
订单
List是一个有序的集合,它维护插入顺序,这意味着在显示列表内容时,它将以它们插入到列表中的相同顺序显示元素。
Set是一个无序的集合,它不维护任何顺序。很少有维护顺序的Set实现,例如LinkedHashSet(它按照插入顺序维护元素)。
空元素
List允许任意数量的空元素。Set最多只能有一个null元素。
List是一个有序的元素序列,而Set是一个无序的元素列表(谢谢你,Quinn Taylor)。
(<和>:
有序集合(也称为 序列)。本界面的用户 有精确的控制在哪里 列出插入的每个元素。的 用户可以通过它们来访问元素 整数索引(在列表中的位置), 并搜索列表中的元素。
Set <和>:
不包含 重复的元素。更正式, 集合不包含元素e1对 e2使得e1。等于(e2) at 最多一个空元素。正如所暗示的 的名称,此接口模拟 数学集合抽象。
List是项的有序分组 Set是一个无序的项目分组,不允许重复(通常)
从概念上讲,我们通常将允许重复的无序分组称为Bag,而不允许重复的无序分组称为Set。
Set不能包含重复的元素,而List可以。List(在Java中)也意味着顺序。
这可能不是您想要的答案,但是集合类的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中的空测试套件