Set<E>接口和List<E>接口的根本区别是什么?
当前回答
列表: List允许重复元素和空值。易于搜索使用相应的索引的元素,也将显示元素在插入顺序。 例如:(linkedlist)
import java.util.*;
public class ListExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Integer> l=new LinkedList<Integer>();
l.add(001);
l.add(555);
l.add(333);
l.add(888);
l.add(555);
l.add(null);
l.add(null);
Iterator<Integer> il=l.iterator();
System.out.println(l.get(0));
while(il.hasNext()){
System.out.println(il.next());
}
for(Integer str : l){
System.out.println("Value:"+str);
}
}
}
输出:
1 1 555 333 888 555 零 零 值:1 值:555 值:333 值:888 值:555 值:空 值:空
设置: Set不允许任何重复元素,它允许单个空值。它不会维护显示元素的任何顺序。只有TreeSet将按升序显示。
例如:(TreeSet)
import java.util.TreeSet;
public class SetExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeSet<String> set = new TreeSet<String>();
try {
set.add("hello");
set.add("world");
set.add("welcome");
set.add("all");
for (String num : set) {
System.out.println( num);
}
set.add(null);
} catch (NullPointerException e) {
System.out.println(e);
System.out.println("Set doesn't allow null value and duplicate value");
}
}
}
输出:
所有 你好 欢迎 世界 java.lang.NullPointerException Set不允许空值和重复值
其他回答
List是项的有序分组 Set是一个无序的项目分组,不允许重复(通常)
从概念上讲,我们通常将允许重复的无序分组称为Bag,而不允许重复的无序分组称为Set。
最大的不同在于基本概念。
从设置和列表界面。集合是数学概念。设置方法扩展集合。但是没有添加新的方法。size()表示基数(more为BitSet。基数,线性计数器,日志日志,HyperLogLog)。addAll()表示联合。retainAll()表示交集。removeAll()表示差异。
However List lack of these concepts. List add a lot of method to support sequence concept which Collection interface not supply. core concept is INDEX. like add(index,element),get(index),search(indexOf()),remove(index) element. List also provide "Collection View" subList. Set do not have view. do not have positional access. List also provide a lot of algorithms in Collections class. sort(List),binarySearch(List),reverse(List),shuffle(List),fill(List). the method params is List interface. duplicate elements are just the result of concepts. not the essential difference.
所以本质的区别在于概念。集合是数学集合的概念。列表是序列的概念。
这可能不是您想要的答案,但是集合类的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.
设置: Set的集合中不能有Duplicate元素。它也是一个无序集合。要从Set中访问数据,只需要使用Iterator,基于索引的检索是不可能的。它主要用于需要唯一性收集的时候。
列表: List可以有重复的元素,插入时使用自然顺序。 因此,它可以基于索引或迭代器检索数据。它广泛用于存储需要基于索引进行访问的集合。
订购…列表有顺序,集合没有。
推荐文章
- 如何格式化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 ?