Set<E>接口和List<E>接口的根本区别是什么?


List是项的有序分组 Set是一个无序的项目分组,不允许重复(通常)

从概念上讲,我们通常将允许重复的无序分组称为Bag,而不允许重复的无序分组称为Set。

订购…列表有顺序,集合没有。

List是一个有序的元素序列,而Set是一个无序的元素列表(谢谢你,Quinn Taylor)。

(<和>:

有序集合(也称为 序列)。本界面的用户 有精确的控制在哪里 列出插入的每个元素。的 用户可以通过它们来访问元素 整数索引(在列表中的位置), 并搜索列表中的元素。

Set <和>:

不包含 重复的元素。更正式, 集合不包含元素e1对 e2使得e1。等于(e2) at 最多一个空元素。正如所暗示的 的名称,此接口模拟 数学集合抽象。

Set不能包含重复的元素,而List可以。List(在Java中)也意味着顺序。

所有List类都保持插入的顺序。它们基于性能和其他特性使用不同的实现(例如ArrayList用于特定索引的访问速度,LinkedList用于简单地维护顺序)。因为没有密钥,所以允许复制。

Set类不维护插入顺序。它们可以选择性地施加特定的顺序(如SortedSet),但通常具有基于一些散列函数的实现定义的顺序(如HashSet)。由于set是通过键访问的,因此不允许重复。

集合是由不同对象组成的无序组——不允许有重复的对象。它通常使用被插入对象的哈希代码来实现。(特定的实现可能会添加排序,但Set接口本身没有。)

列表是一组有序的对象,其中可能包含重复项。它可以用数组列表、链表等来实现。

元素的有序列表(是否唯一) 遵循Java的名为List的接口 可以通过索引访问吗

实现使用

LinkedList ArrayList

独特元素列表: 遵循Java名为Set的接口 不能通过索引访问

实现使用

HashSet(无序) LinkedHashSet(命令) 树集(按自然顺序或按提供的比较器排序)

接口Set和List都符合Java的名为Collection的接口

这可能不是您想要的答案,但是集合类的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.

列表:

列表通常允许重复对象。 列表必须是有序的,因此可以通过索引访问。

实现类包括:ArrayList, LinkedList, Vector

Set:

集合不允许重复对象。 大多数实现是无序的,但它是特定于实现的。

实现类包括: HashSet(无序) LinkedHashSet(命令), 树集(按自然顺序或按提供的比较器排序)

1.List允许重复值,set不允许重复值

2.List维护您在列表中插入元素的顺序 Set不能维持秩序。 3.List是一个有序的元素序列,而Set是一个无序的元素列表。

List

是元素的有序分组。 List用于收集重复的元素。 新方法在List中定义 接口。

Set

是元素的无序分组。 Set用于收集没有重复项的元素。 在Set接口中没有定义任何新方法,因此我们只能对Set子类使用Collection接口方法。

Set<E>和List<E>都用于存储E类型的元素。区别在于Set是以无序方式存储的,不允许重复值。List用于以有序的方式存储元素,并且允许重复值。

Set元素不能通过索引位置访问,而List元素可以通过索引位置访问。

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是最好的方法。

这里有一个关于groovy的明确例子。我创建了一个集合和一个列表。 然后我尝试在每个列表中存储20个随机生成的值。生成的值范围为0 ~ 5

s = [] as Set
l = []

max = 5
print "random Numbers :"
20.times{
e = (int)Math.random()*max
s << e
l << e
print "$e, "
}


println "\n"
println "Set : $s "
println "list : $l

结果:

随机数:4,1,4,0,1,2,4,0,0,0,3,4,2,0,4,4,0,4,0,0,1,3,1,3

集合:[4,1,0,2,3]

列表:[4 1 4 0,1,2,4,0,0,3、4、3、2 0 4 0,1,3,1,3)

你可以看到区别在于:

Set不允许重复值。 列表允许重复值。

当我们讨论Java接口时,为什么不看看Javadoc呢?!

List是一个有序的集合(序列),通常允许 重复的 集合是不包含重复元素的集合,迭代 顺序可以由实现来保证

这里没有提到关于集合的缺乏顺序:这取决于实现。

列表:

允许重复。 有序地将元素分组。(换句话说,有明确的顺序。不需要按升序排序)

Set:

不允许复制。 对元素进行无序分组。(换句话说,没有明确的顺序。它可能是也可能不是按升序排列的)

List Set
Duplicates Yes No
Order Ordered Depends on implementation
Position Access Yes No

主题名称:列表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:

不能有重复值 排序取决于实现。默认情况下,它不是有序的 不能通过索引进行访问

列表:

可以有重复的值 默认订购 可以通过索引访问吗

List Vs Set

1) Set不允许重复。列表允许重复。基于Set的实现,它还维护插入顺序。

LinkedHashSet。它维护插入顺序。请参考此处

2)包含方法。根据集合的性质,它将为访问提供更好的性能。最好的情况是o(1)但是List有调用contains的性能问题。

列表: 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不允许空值和重复值

设置: Set的集合中不能有Duplicate元素。它也是一个无序集合。要从Set中访问数据,只需要使用Iterator,基于索引的检索是不可能的。它主要用于需要唯一性收集的时候。

列表: List可以有重复的元素,插入时使用自然顺序。 因此,它可以基于索引或迭代器检索数据。它广泛用于存储需要基于索引进行访问的集合。

嗨,已经给出了这么多答案,让我指出一些到目前为止没有提到的地方:

大多数的List实现(ArrayList,Vector)实现了RandomAccess接口,这是一个快速访问的标记接口。Set的实现都没有这样做。 List使用一个特殊的迭代器,称为ListIterator,它支持双向迭代。Set使用只支持单向迭代的迭代器 HashSet占用的内存是ArrayList的5.5倍 相同数量的元素。

最大的不同在于基本概念。

从设置和列表界面。集合是数学概念。设置方法扩展集合。但是没有添加新的方法。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.

所以本质的区别在于概念。集合是数学集合的概念。列表是序列的概念。

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都是接口。它们都扩展了Collection接口。set和list之间的重要区别是:

复制对象

List和Set之间的主要区别是List允许重复,而Set不允许重复。

订单

List是一个有序的集合,它维护插入顺序,这意味着在显示列表内容时,它将以它们插入到列表中的相同顺序显示元素。

Set是一个无序的集合,它不维护任何顺序。很少有维护顺序的Set实现,例如LinkedHashSet(它按照插入顺序维护元素)。

空元素

List允许任意数量的空元素。Set最多只能有一个null元素。