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.
其他回答
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 |
List是一个有序的元素序列,而Set是一个无序的元素列表(谢谢你,Quinn Taylor)。
(<和>:
有序集合(也称为 序列)。本界面的用户 有精确的控制在哪里 列出插入的每个元素。的 用户可以通过它们来访问元素 整数索引(在列表中的位置), 并搜索列表中的元素。
Set <和>:
不包含 重复的元素。更正式, 集合不包含元素e1对 e2使得e1。等于(e2) at 最多一个空元素。正如所暗示的 的名称,此接口模拟 数学集合抽象。
List是项的有序分组 Set是一个无序的项目分组,不允许重复(通常)
从概念上讲,我们通常将允许重复的无序分组称为Bag,而不允许重复的无序分组称为Set。
订购…列表有顺序,集合没有。
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是最好的方法。
推荐文章
- 如何格式化Joda-Time DateTime仅为mm/dd/yyyy?
- 如何在POM.xml中引用环境变量?
- 如何在android中复制一个文件?
- Python中方括号括起来的列表和圆括号括起来的列表有什么区别?
- 将整数转换为字符串,以逗号表示千
- 接口方法的最终参数-有什么意义?
- Java中的@UniqueConstraint注释
- 如何在清洁模式下运行eclipse ?如果我们这样做会发生什么?
- 获取java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory异常
- Java中的正则表达式命名组
- c#和Java的主要区别是什么?
- 什么是NullPointerException,我如何修复它?
- 在Java中使用“final”修饰符
- 无法在Flutter上找到捆绑的Java版本
- 如何在Kotlin解析JSON ?