What is the fundamental difference between the Set<E>
and List<E>
interfaces?
Set
26 个解决方案
#1
413
List
is an ordered sequence of elements whereas Set
is a distinct list of elements which is unordered (thank you, Quinn Taylor).
List是元素的有序序列,而Set是无序的元素列表(谢谢,Quinn Taylor)。
列表< E >:
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.
有序集合(也称为序列)。该接口的用户对插入的每个元素的位置有精确的控制。用户可以通过其整数索引(列表中的位置)访问元素,并搜索列表中的元素。
设置< E >:
A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.
不包含重复元素的集合。更正式地说,集合中不包含对e1和e2的元素e1和e2,比如e1 = (e2)和最多一个空元素。正如它的名字所暗示的那样,这个接口模拟了数学集合的抽象。
#2
161
╔═══════════════════╦══════════════════════╦═════════════════════════════╗
║ ║ List ║ Set ║
╠═══════════════════╬══════════════════════╬═════════════════════════════╣
║ Duplicates ║ YES ║ NO ║
╠═══════════════════╬══════════════════════╬═════════════════════════════╣
║ Order ║ ORDERED ║ DEPENDS ON IMPLEMENTATION ║
╠═══════════════════╬══════════════════════╬═════════════════════════════╣
║ Positional Access ║ YES ║ NO ║
╚═══════════════════╩══════════════════════╩═════════════════════════════╝
#3
62
Ordered lists of element (unique or not)
Conform to Java's interface named List
Can be accessed by index
元素的有序列表(唯一的或不惟一的)符合Java的接口,命名列表可以通过索引访问。
implemetented using
implemetented使用
- LinkedList
- LinkedList
- ArrayList
- ArrayList
Lists of unique elements:
Conform to Java's interface named Set
Can not be accessed by index
唯一元素列表:与Java接口命名的集合不能被索引访问。
implemetented using
implemetented使用
- HashSet (unordered)
- HashSet(无序)
- LinkedHashSet (ordered)
- LinkedHashSet(命令)
- TreeSet (sorted by natural order or by provided comparator)
- TreeSet(按自然顺序或提供比较器排序)
Both interfaces Set
and List
conform to Java's interface named Collection
两个接口设置和列表符合Java的接口命名集合。
#4
24
A Set cannot contain duplicate elements while a List can. A List (in Java) also implies order.
一个集合不能包含重复元素,而列表可以。列表(在Java中)也意味着顺序。
#5
15
- A List is an ordered grouping of items
- 列表是项的有序分组。
- A Set is an unordered grouping of items with no duplicates allowed (usually)
- 一个集合是一个无序的集合,不允许有重复项(通常)
Conceptually we usually refer to an unordered grouping that allows duplicates as a Bag and doesn't allow duplicates is a Set.
从概念上讲,我们通常指的是一个无序的分组,它允许复制作为一个包,并且不允许复制是一个集合。
#6
10
List
列表
- Is an Ordered grouping of elements.
- 是元素的有序分组。
- List is used to collection of elements with duplicates.
- 列表用于收集具有重复项的元素。
- New methods are defined inside List interface.
- 在列表接口中定义了新方法。
Set
集
- Is an Unordered grouping of elements.
- 是元素的无序分组。
- Set is used to collection of elements without duplicates.
- 集合用于收集没有重复的元素。
- No new methods are defined inside Set interface, so we have to use Collection interface methods only with Set subclasses.
- 在Set接口中没有定义新的方法,所以我们只能使用集合类的方法来使用集合接口。
#7
8
List:
List
s generally allow duplicate objects. List
s must be ordered, and are therefore accessible by index.
列表通常允许重复的对象。列表必须是有序的,因此可以通过索引访问。
Implementation classes include: ArrayList
, LinkedList
, Vector
实现类包括:ArrayList、LinkedList、Vector。
Set:
Set
s do not allow duplicate objects. Most implementations are unordered, but it is implementation specific.
设置不允许重复对象。大多数实现都是无序的,但它是特定于实现的。
Implementation classes include: HashSet
(unordered), LinkedHashSet
(ordered), TreeSet
(ordered by natural order or by provided comparator)
实现类包括:HashSet (unordered)、LinkedHashSet (ordered)、TreeSet(按自然顺序排序或提供比较器)
#8
7
As we are talking about the Java interfaces, why not look at the Javadoc ?!
当我们讨论Java接口时,为什么不查看Javadoc ?
- A
List
is an ordered collection (sequence), which typically allows duplicates - 列表是一个有序集合(序列),通常允许重复。
- A
Set
a is collection that contains no duplicate elements, iteration order may be guaranteed by the implementation - 集合A是不包含重复元素的集合,可以通过实现来保证迭代顺序。
There is NO mention about lack of order concerning Sets: it depends on the implementation.
没有提到关于集的缺乏顺序:它取决于实现。
#9
5
This might not be the answer you're looking for, but the JavaDoc of the collections classes is actually pretty descriptive. Copy/pasted:
这可能不是您想要的答案,但是集合类的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.
与设置不同,列表通常允许重复的元素。更正式地说,列表通常允许对元素e1和e2进行配对,e1 = e2 (e2),如果它们允许null元素,它们通常允许多个null元素。有人可能希望实现一个禁止重复的列表,在用户试图插入时抛出运行时异常,但我们希望这种用法非常少见,这并不是不可思议的。
#10
5
A set is an unordered group of distinct objects — no duplicate objects are allowed. It is generally implemented using the hash code of the objects being inserted. (Specific implementations may add ordering, but the Set interface itself does not.)
集合是一组无序的对象,不允许重复的对象。它通常是使用插入对象的哈希代码实现的。(特定的实现可以添加排序,但是Set接口本身没有。)
A list is an ordered group of objects which may contain duplicates. It could be implemented with an ArrayList
, LinkedList
, etc.
列表是一个有序的对象组,其中可能包含重复的对象。它可以通过ArrayList、LinkedList等实现。
#11
3
1.List allows duplicate values and set does'nt allow duplicates
1。列表允许重复的值,设置不允许重复。
2.List maintains the order in which you inserted elements in to the list Set does'nt maintain order. 3.List is an ordered sequence of elements whereas Set is a distinct list of elements which is unordered.
2。列表维护您将元素插入到列表中的顺序,而不是维护顺序。3所示。List是元素的有序序列,而Set是无序的元素列表。
#12
2
Ordering... a list has an order, a set does not.
订购…一个列表有一个顺序,一个集合没有。
#13
2
All of the List
classes maintain the order of insertion. They use different implementations based on performance and other characteristics (e.g. ArrayList
for speed of access of a specific index, LinkedList
for simply maintaining order). Since there is no key, duplicates are allowed.
所有的列表类都维护插入的顺序。他们根据性能和其他特性使用不同的实现(例如,ArrayList为了获得特定索引的访问速度,LinkedList只是为了维护订单)。因为没有密钥,所以允许重复。
The Set
classes do not maintain insertion order. They may optionally impose a specific order (as with SortedSet
), but typically have an implementation-defined order based on some hash function (as with HashSet
). Since Set
s are accessed by key, duplicates are not allowed.
设置类不维护插入顺序。它们可以选择性地强制执行特定的顺序(如SortedSet),但通常基于一些散列函数(和HashSet一样)具有实现定义的顺序。因为设置是通过密钥访问的,所以不允许重复。
#14
2
List Vs Set
列表和设置
1) Set does not allow duplicates. List allows duplicate. Based on the implementation of Set, It also maintains the insertion Order .
设置不允许重复。允许复制列表。基于Set的实现,它也维护了插入顺序。
eg : LinkedHashSet
. It maintains the insertion order.Please refer click here
LinkedHashSet。它维护插入顺序。请点击这里
2) contains method. By nature of the Set it will give better performance to access. Best case its o(1). But List has performance issue to invoke contains
.
2)包含方法。根据设置的性质,它将提供更好的访问性能。最好的情况下其o(1)。但是List具有调用包含的性能问题。
#15
1
Few note worthy differences between List and Set in Java are given as following :
在Java中列出的列表和集合之间没有什么值得注意的区别,如下所示:
1) Fundamental difference between List and Set in Java is allowing duplicate elements. List in Java allows duplicates while Set doesn't allow any duplicate. If you insert duplicate in Set it will replace the older value. Any implementation of Set in Java will only contains unique elements.
1)Java中列表和Set之间的根本区别在于允许重复元素。在Java中列表允许重复,而Set不允许重复。如果在设置中插入重复,它将替换旧值。在Java中任何设置的实现都只包含独特的元素。
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.
2)Java中的List和Set之间的另一个显著区别是order。List是一个有序集合,而Set是一个无序集合。列表维护元素的插入顺序,意味着在插入之前插入的元素会比插入后的任何元素都更低。在Java中设置不维护任何顺序。虽然Set提供了另一种选择,称为SortedSet,它可以根据存储在集合中的对象的可比较和比较器方法定义的特定排序顺序存储集合元素。
3) Popular implementation of List interface in Java includes ArrayList, Vector and LinkedList. While popular implementation of Set interface includes HashSet, TreeSet and LinkedHashSet.
3)在Java中流行的列表接口的实现包括ArrayList、Vector和LinkedList。虽然流行的Set接口实现包括HashSet、TreeSet和LinkedHashSet。
Its pretty clear that if you need to maintain insertion order or object and you collection can contain duplicates than List is a way to go. On the other hand if your requirement is to maintain unique collection without any duplicates than Set is the way to go.
很明显,如果您需要维护插入顺序或对象,并且您的集合可以包含副本,而不是列表,这是一种方法。另一方面,如果您的要求是保持惟一的集合,而不需要任何副本,那么就可以使用Set。
#16
1
List:
List allows duplicate elements and null values. Easy to search using the corresponding index of the elements and also it will display elements in insertion order. Example:(linkedlist)
列表:列表允许重复元素和空值。使用相应的元素索引容易搜索,也会显示插入顺序中的元素。例如:(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);
}
}
}
Output:
输出:
1
1
555
333
888
555
null
null
Value:1
Value:555
Value:333
Value:888
Value:555
Value:null
Value:null
555值:555值:333值:888值:555值:空值:null。
Set:
Set isn't allow any duplicate elements and it allow single null value.It will not maintain any order to display elements.Only TreeSet
will display in ascending order.
Set: Set不允许任何重复元素,它允许单个空值。它不会维护显示元素的任何顺序。只有TreeSet将按升序显示。
Example:(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");
}
}
}
Output:
输出:
all
hello
welcome
world
java.lang.NullPointerException
Set doesn't allow null value and duplicate value
大家好,欢迎世界java.lang。NullPointerException设置不允许null值和重复值。
#17
0
Set<E>
and List<E>
are both used to store elements of type E
. The difference is that Set
is stored in unordered way and does not allow duplicate values. List
is used to store elements in ordered way and it does allow duplicate values.
Set
Set
elements cannot be accessed by an index position, and List
elements can be accessed with an index position.
设置元素不能被索引位置访问,并且可以使用索引位置访问列表元素。
#18
0
List:
列表:
- Allowed duplicates.
- 允许重复。
- Ordered in grouping elements.(In other words having definite order.No need to sorted in ascending order)
- 命令在分组元素。(换句话说,有明确的顺序。不需要按升序排序)
Set:
设置:
- Not allowed duplicates.
- 不允许重复。
- Unordered in grouping elements.(In other words having no definite order.It might or might not arranged in ascending order )
- 在分组元素无序。(换句话说,没有明确的顺序。它可能或可能没有按升序排列)
#19
0
Duplicity
表里不一
Set doesn’t allow duplicates. Set and all of the classes which implements Set interface should have unique elements. List allows duplicate elements. Any number of duplicate elements can be inserted into the list without affecting the same existing values and their indexes.
不允许重复。Set和所有实现Set接口的类都应该具有独特的元素。允许重复的元素列表。可以将任意数量的重复元素插入到列表中,而不影响相同的现有值及其索引。
Null values
空值
List allows any number of null values.
Set allows single null value at most
Order
订单
List and all of its implementation classes maintains the insertion order. Set doesn’t maintain any order still few of its classes sort the elements in an order such as LinkedHashSet maintains the elements in insertion order and TreeSet(the elements maintain the ascending order by default)
列表及其所有实现类维护插入顺序。Set不维护任何顺序,因为它的类中的元素按顺序排序,如LinkedHashSet在插入顺序和TreeSet中维护元素(默认情况下元素保持升序)
class implementations
类实现
List: ArrayList, LinkedList
Set: HashSet, LinkedHashSet, TreeSet
#20
0
Difference based on following points
差异基于以下几点。
1) Duplicity: List allows duplicate elements. Any number of duplicate elements can be inserted into the list without affecting the same existing values and their indexes. Set doesn’t allow duplicates. Set and all of the classes which implements Set interface should have unique elements.
1)表里不一:列表允许重复元素。可以将任意数量的重复元素插入到列表中,而不影响相同的现有值及其索引。不允许重复。Set和所有实现Set接口的类都应该具有独特的元素。
2) Null values: List allows any number of null values. Set allows single null value at most.
2)空值:List允许任意数量的空值。设置允许最多为空值。
3) Order: List and all of its implementation classes maintains the insertion order. Set doesn’t maintain any order; still few of its classes sort the elements in an order such as LinkedHashSet maintains the elements in insertion order.
3)顺序:列表及其所有实现类维护插入顺序。设置不维护任何顺序;它的类中仍然很少对元素进行排序,比如LinkedHashSet以插入顺序维护元素。
#21
0
Hi So many answers are already given..Let me point out some points which are not mentioned so far:
嗨,这么多的答案已经给出了。让我指出一些目前还没有提到的观点:
- Most of the List implementations (ArrayList,Vector) implement
RandomAccess
interface which is a marker interface for faster access. None of the List implementations do that. - 大多数列表实现(ArrayList,Vector)都实现了随机访问接口,这是一个用于更快访问的标记接口。所有的列表实现都不这样做。
- List uses one special Iterator called
ListIterator
which supports iteration in both directions. Set uses Iterator which supports only 1 way iteration - 列表使用一个名为ListIterator的特殊迭代器,它支持两个方向上的迭代。Set使用迭代器,该迭代器只支持单向迭代。
- HashSet takes 5.5 times more memory than ArrayList for storing same number of elements.
- HashSet比ArrayList存储相同数量的元素的内存要多5.5倍。
#22
-1
Here ist a clear example with groovy. i create a set and a list. then i try to store 20 randomly generated value within each list. the generated value can be in range 0 to 5
这里是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
The result :
结果:
random Numbers: 4, 1, 4, 0, 1, 2, 4, 0, 0, 3, 4, 3, 2, 0, 4, 0, 1, 3, 1, 3
随机数字:4、1、4、0、1、2、4、0、0、3、4、3、2、0、4、0、1、3、1、3。
Set : [4, 1, 0, 2, 3]
集合:[4,1,0,2,3]
list : [4, 1, 4, 0, 1, 2, 4, 0, 0, 3, 4, 3, 2, 0, 4, 0, 1, 3, 1, 3]
列表:[4 1 4 0,1,2,4,0,0,3、4、3、2 0 4 0,1,3,1,3)
You can see that the difference is that:
你可以看到区别是:
- Set does not allow duplicate values.
- Set不允许重复的值。
- List allow duplicate values.
- 允许重复的值列表。
#23
-1
Like the answer as SET don't have duplicate value and List can. Of course, order is another one thing to different them apart.
如设置的答案一样,没有重复的值和列表可以。当然,秩序是另一种不同的东西。
#24
-1
Set: A Set cannot have Duplicate elements in its collections. it is also an unordered collection. To access the data from Set, it is required to use Iterator only and index based retrieve is not possible for it. It is mainly used whenever required uniqueness collection.
集合:集合中不能有重复的元素。它也是一个无序的集合。要从Set中访问数据,需要使用迭代器,而基于索引的检索是不可能的。它主要用于任何需要的唯一性集合。
List: A List can have duplicate elements, with the natural ordered as it is inserted. Thus, it can be retrieved data based on index or iterator. It is widely used to store collection which needs to access based on index.
列表:一个列表可以有重复的元素,在插入的时候自然排序。因此,可以基于索引或迭代器检索数据。它被广泛用于存储需要根据索引访问的集合。
#25
-2
TOPIC Name: List VS Set
主题名称:列表VS设置。
I have just gone through Java's most important topic called Collections Framework. I thought to share my little knowledge about Collections with you. List, Set, Map are the most important topic of it. So let's start with List and Set.
我刚刚经历了Java最重要的主题——集合框架。我想和你分享我的收藏知识。列表,设置,地图是最重要的主题。我们从列表开始。
Difference between List and Set:
列表与集合的区别:
-
List is a collection class which extends
AbstractList
class where as Set is a collection class which extendsAbstractSet
class but both implements Collection interface.List是一个扩展类,它扩展了AbstractList类,其中集合是扩展AbstractSet类的集合类,但都实现了集合接口。
-
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.
列表接口允许重复的值(元素),而Set接口不允许重复的值。如果设置了重复元素,它将替换旧的值。
-
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
.列表接口允许NULL值,其中设置接口不允许空值。如果在设置中使用Null值,它会给出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. (AlthoughSortedSet
does usingTreeSet
, andLinkedHashSet
maintains insertion order).列表接口维护插入顺序。这意味着我们在列表中添加元素的方式与使用迭代器或for-each样式获得的方式相同。而设置实现并不一定要维护插入顺序。(尽管SortedSet使用TreeSet,而LinkedHashSet维护插入顺序)。
-
List interface has its own methods defined whereas Set interface does not have its own method so Set uses Collection interface methods only.
List接口有自己定义的方法,而Set接口没有自己的方法,所以Set只使用集合接口方法。
-
List interface has one legacy class called
Vector
whereas Set interface does not have any legacy class列表接口有一个名为Vector的遗留类,而Set接口没有任何遗留类。
-
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最后但不是最不重要的……listIterator()方法只能用于遍历列表类中的元素,而我们可以使用iterator()方法来访问Set类元素。
Anything else can we add? Please let me know.
还有什么要补充的吗?请让我知道。
Thanks.
谢谢。
#26
-3
Set:
设置:
Cannot have duplicate values Ordering depends on implementation. By default it is not ordered Cannot have access by index
不能有重复的值排序依赖于实现。默认情况下,它不是命令不能通过索引访问。
List:
列表:
Can have duplicate values Ordered by default Can have access by index
默认情况下,可以通过索引访问重复的值吗?
#1
413
List
is an ordered sequence of elements whereas Set
is a distinct list of elements which is unordered (thank you, Quinn Taylor).
List是元素的有序序列,而Set是无序的元素列表(谢谢,Quinn Taylor)。
列表< E >:
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.
有序集合(也称为序列)。该接口的用户对插入的每个元素的位置有精确的控制。用户可以通过其整数索引(列表中的位置)访问元素,并搜索列表中的元素。
设置< E >:
A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.
不包含重复元素的集合。更正式地说,集合中不包含对e1和e2的元素e1和e2,比如e1 = (e2)和最多一个空元素。正如它的名字所暗示的那样,这个接口模拟了数学集合的抽象。
#2
161
╔═══════════════════╦══════════════════════╦═════════════════════════════╗
║ ║ List ║ Set ║
╠═══════════════════╬══════════════════════╬═════════════════════════════╣
║ Duplicates ║ YES ║ NO ║
╠═══════════════════╬══════════════════════╬═════════════════════════════╣
║ Order ║ ORDERED ║ DEPENDS ON IMPLEMENTATION ║
╠═══════════════════╬══════════════════════╬═════════════════════════════╣
║ Positional Access ║ YES ║ NO ║
╚═══════════════════╩══════════════════════╩═════════════════════════════╝
#3
62
Ordered lists of element (unique or not)
Conform to Java's interface named List
Can be accessed by index
元素的有序列表(唯一的或不惟一的)符合Java的接口,命名列表可以通过索引访问。
implemetented using
implemetented使用
- LinkedList
- LinkedList
- ArrayList
- ArrayList
Lists of unique elements:
Conform to Java's interface named Set
Can not be accessed by index
唯一元素列表:与Java接口命名的集合不能被索引访问。
implemetented using
implemetented使用
- HashSet (unordered)
- HashSet(无序)
- LinkedHashSet (ordered)
- LinkedHashSet(命令)
- TreeSet (sorted by natural order or by provided comparator)
- TreeSet(按自然顺序或提供比较器排序)
Both interfaces Set
and List
conform to Java's interface named Collection
两个接口设置和列表符合Java的接口命名集合。
#4
24
A Set cannot contain duplicate elements while a List can. A List (in Java) also implies order.
一个集合不能包含重复元素,而列表可以。列表(在Java中)也意味着顺序。
#5
15
- A List is an ordered grouping of items
- 列表是项的有序分组。
- A Set is an unordered grouping of items with no duplicates allowed (usually)
- 一个集合是一个无序的集合,不允许有重复项(通常)
Conceptually we usually refer to an unordered grouping that allows duplicates as a Bag and doesn't allow duplicates is a Set.
从概念上讲,我们通常指的是一个无序的分组,它允许复制作为一个包,并且不允许复制是一个集合。
#6
10
List
列表
- Is an Ordered grouping of elements.
- 是元素的有序分组。
- List is used to collection of elements with duplicates.
- 列表用于收集具有重复项的元素。
- New methods are defined inside List interface.
- 在列表接口中定义了新方法。
Set
集
- Is an Unordered grouping of elements.
- 是元素的无序分组。
- Set is used to collection of elements without duplicates.
- 集合用于收集没有重复的元素。
- No new methods are defined inside Set interface, so we have to use Collection interface methods only with Set subclasses.
- 在Set接口中没有定义新的方法,所以我们只能使用集合类的方法来使用集合接口。
#7
8
List:
List
s generally allow duplicate objects. List
s must be ordered, and are therefore accessible by index.
列表通常允许重复的对象。列表必须是有序的,因此可以通过索引访问。
Implementation classes include: ArrayList
, LinkedList
, Vector
实现类包括:ArrayList、LinkedList、Vector。
Set:
Set
s do not allow duplicate objects. Most implementations are unordered, but it is implementation specific.
设置不允许重复对象。大多数实现都是无序的,但它是特定于实现的。
Implementation classes include: HashSet
(unordered), LinkedHashSet
(ordered), TreeSet
(ordered by natural order or by provided comparator)
实现类包括:HashSet (unordered)、LinkedHashSet (ordered)、TreeSet(按自然顺序排序或提供比较器)
#8
7
As we are talking about the Java interfaces, why not look at the Javadoc ?!
当我们讨论Java接口时,为什么不查看Javadoc ?
- A
List
is an ordered collection (sequence), which typically allows duplicates - 列表是一个有序集合(序列),通常允许重复。
- A
Set
a is collection that contains no duplicate elements, iteration order may be guaranteed by the implementation - 集合A是不包含重复元素的集合,可以通过实现来保证迭代顺序。
There is NO mention about lack of order concerning Sets: it depends on the implementation.
没有提到关于集的缺乏顺序:它取决于实现。
#9
5
This might not be the answer you're looking for, but the JavaDoc of the collections classes is actually pretty descriptive. Copy/pasted:
这可能不是您想要的答案,但是集合类的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.
与设置不同,列表通常允许重复的元素。更正式地说,列表通常允许对元素e1和e2进行配对,e1 = e2 (e2),如果它们允许null元素,它们通常允许多个null元素。有人可能希望实现一个禁止重复的列表,在用户试图插入时抛出运行时异常,但我们希望这种用法非常少见,这并不是不可思议的。
#10
5
A set is an unordered group of distinct objects — no duplicate objects are allowed. It is generally implemented using the hash code of the objects being inserted. (Specific implementations may add ordering, but the Set interface itself does not.)
集合是一组无序的对象,不允许重复的对象。它通常是使用插入对象的哈希代码实现的。(特定的实现可以添加排序,但是Set接口本身没有。)
A list is an ordered group of objects which may contain duplicates. It could be implemented with an ArrayList
, LinkedList
, etc.
列表是一个有序的对象组,其中可能包含重复的对象。它可以通过ArrayList、LinkedList等实现。
#11
3
1.List allows duplicate values and set does'nt allow duplicates
1。列表允许重复的值,设置不允许重复。
2.List maintains the order in which you inserted elements in to the list Set does'nt maintain order. 3.List is an ordered sequence of elements whereas Set is a distinct list of elements which is unordered.
2。列表维护您将元素插入到列表中的顺序,而不是维护顺序。3所示。List是元素的有序序列,而Set是无序的元素列表。
#12
2
Ordering... a list has an order, a set does not.
订购…一个列表有一个顺序,一个集合没有。
#13
2
All of the List
classes maintain the order of insertion. They use different implementations based on performance and other characteristics (e.g. ArrayList
for speed of access of a specific index, LinkedList
for simply maintaining order). Since there is no key, duplicates are allowed.
所有的列表类都维护插入的顺序。他们根据性能和其他特性使用不同的实现(例如,ArrayList为了获得特定索引的访问速度,LinkedList只是为了维护订单)。因为没有密钥,所以允许重复。
The Set
classes do not maintain insertion order. They may optionally impose a specific order (as with SortedSet
), but typically have an implementation-defined order based on some hash function (as with HashSet
). Since Set
s are accessed by key, duplicates are not allowed.
设置类不维护插入顺序。它们可以选择性地强制执行特定的顺序(如SortedSet),但通常基于一些散列函数(和HashSet一样)具有实现定义的顺序。因为设置是通过密钥访问的,所以不允许重复。
#14
2
List Vs Set
列表和设置
1) Set does not allow duplicates. List allows duplicate. Based on the implementation of Set, It also maintains the insertion Order .
设置不允许重复。允许复制列表。基于Set的实现,它也维护了插入顺序。
eg : LinkedHashSet
. It maintains the insertion order.Please refer click here
LinkedHashSet。它维护插入顺序。请点击这里
2) contains method. By nature of the Set it will give better performance to access. Best case its o(1). But List has performance issue to invoke contains
.
2)包含方法。根据设置的性质,它将提供更好的访问性能。最好的情况下其o(1)。但是List具有调用包含的性能问题。
#15
1
Few note worthy differences between List and Set in Java are given as following :
在Java中列出的列表和集合之间没有什么值得注意的区别,如下所示:
1) Fundamental difference between List and Set in Java is allowing duplicate elements. List in Java allows duplicates while Set doesn't allow any duplicate. If you insert duplicate in Set it will replace the older value. Any implementation of Set in Java will only contains unique elements.
1)Java中列表和Set之间的根本区别在于允许重复元素。在Java中列表允许重复,而Set不允许重复。如果在设置中插入重复,它将替换旧值。在Java中任何设置的实现都只包含独特的元素。
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.
2)Java中的List和Set之间的另一个显著区别是order。List是一个有序集合,而Set是一个无序集合。列表维护元素的插入顺序,意味着在插入之前插入的元素会比插入后的任何元素都更低。在Java中设置不维护任何顺序。虽然Set提供了另一种选择,称为SortedSet,它可以根据存储在集合中的对象的可比较和比较器方法定义的特定排序顺序存储集合元素。
3) Popular implementation of List interface in Java includes ArrayList, Vector and LinkedList. While popular implementation of Set interface includes HashSet, TreeSet and LinkedHashSet.
3)在Java中流行的列表接口的实现包括ArrayList、Vector和LinkedList。虽然流行的Set接口实现包括HashSet、TreeSet和LinkedHashSet。
Its pretty clear that if you need to maintain insertion order or object and you collection can contain duplicates than List is a way to go. On the other hand if your requirement is to maintain unique collection without any duplicates than Set is the way to go.
很明显,如果您需要维护插入顺序或对象,并且您的集合可以包含副本,而不是列表,这是一种方法。另一方面,如果您的要求是保持惟一的集合,而不需要任何副本,那么就可以使用Set。
#16
1
List:
List allows duplicate elements and null values. Easy to search using the corresponding index of the elements and also it will display elements in insertion order. Example:(linkedlist)
列表:列表允许重复元素和空值。使用相应的元素索引容易搜索,也会显示插入顺序中的元素。例如:(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);
}
}
}
Output:
输出:
1
1
555
333
888
555
null
null
Value:1
Value:555
Value:333
Value:888
Value:555
Value:null
Value:null
555值:555值:333值:888值:555值:空值:null。
Set:
Set isn't allow any duplicate elements and it allow single null value.It will not maintain any order to display elements.Only TreeSet
will display in ascending order.
Set: Set不允许任何重复元素,它允许单个空值。它不会维护显示元素的任何顺序。只有TreeSet将按升序显示。
Example:(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");
}
}
}
Output:
输出:
all
hello
welcome
world
java.lang.NullPointerException
Set doesn't allow null value and duplicate value
大家好,欢迎世界java.lang。NullPointerException设置不允许null值和重复值。
#17
0
Set<E>
and List<E>
are both used to store elements of type E
. The difference is that Set
is stored in unordered way and does not allow duplicate values. List
is used to store elements in ordered way and it does allow duplicate values.
Set
Set
elements cannot be accessed by an index position, and List
elements can be accessed with an index position.
设置元素不能被索引位置访问,并且可以使用索引位置访问列表元素。
#18
0
List:
列表:
- Allowed duplicates.
- 允许重复。
- Ordered in grouping elements.(In other words having definite order.No need to sorted in ascending order)
- 命令在分组元素。(换句话说,有明确的顺序。不需要按升序排序)
Set:
设置:
- Not allowed duplicates.
- 不允许重复。
- Unordered in grouping elements.(In other words having no definite order.It might or might not arranged in ascending order )
- 在分组元素无序。(换句话说,没有明确的顺序。它可能或可能没有按升序排列)
#19
0
Duplicity
表里不一
Set doesn’t allow duplicates. Set and all of the classes which implements Set interface should have unique elements. List allows duplicate elements. Any number of duplicate elements can be inserted into the list without affecting the same existing values and their indexes.
不允许重复。Set和所有实现Set接口的类都应该具有独特的元素。允许重复的元素列表。可以将任意数量的重复元素插入到列表中,而不影响相同的现有值及其索引。
Null values
空值
List allows any number of null values.
Set allows single null value at most
Order
订单
List and all of its implementation classes maintains the insertion order. Set doesn’t maintain any order still few of its classes sort the elements in an order such as LinkedHashSet maintains the elements in insertion order and TreeSet(the elements maintain the ascending order by default)
列表及其所有实现类维护插入顺序。Set不维护任何顺序,因为它的类中的元素按顺序排序,如LinkedHashSet在插入顺序和TreeSet中维护元素(默认情况下元素保持升序)
class implementations
类实现
List: ArrayList, LinkedList
Set: HashSet, LinkedHashSet, TreeSet
#20
0
Difference based on following points
差异基于以下几点。
1) Duplicity: List allows duplicate elements. Any number of duplicate elements can be inserted into the list without affecting the same existing values and their indexes. Set doesn’t allow duplicates. Set and all of the classes which implements Set interface should have unique elements.
1)表里不一:列表允许重复元素。可以将任意数量的重复元素插入到列表中,而不影响相同的现有值及其索引。不允许重复。Set和所有实现Set接口的类都应该具有独特的元素。
2) Null values: List allows any number of null values. Set allows single null value at most.
2)空值:List允许任意数量的空值。设置允许最多为空值。
3) Order: List and all of its implementation classes maintains the insertion order. Set doesn’t maintain any order; still few of its classes sort the elements in an order such as LinkedHashSet maintains the elements in insertion order.
3)顺序:列表及其所有实现类维护插入顺序。设置不维护任何顺序;它的类中仍然很少对元素进行排序,比如LinkedHashSet以插入顺序维护元素。
#21
0
Hi So many answers are already given..Let me point out some points which are not mentioned so far:
嗨,这么多的答案已经给出了。让我指出一些目前还没有提到的观点:
- Most of the List implementations (ArrayList,Vector) implement
RandomAccess
interface which is a marker interface for faster access. None of the List implementations do that. - 大多数列表实现(ArrayList,Vector)都实现了随机访问接口,这是一个用于更快访问的标记接口。所有的列表实现都不这样做。
- List uses one special Iterator called
ListIterator
which supports iteration in both directions. Set uses Iterator which supports only 1 way iteration - 列表使用一个名为ListIterator的特殊迭代器,它支持两个方向上的迭代。Set使用迭代器,该迭代器只支持单向迭代。
- HashSet takes 5.5 times more memory than ArrayList for storing same number of elements.
- HashSet比ArrayList存储相同数量的元素的内存要多5.5倍。
#22
-1
Here ist a clear example with groovy. i create a set and a list. then i try to store 20 randomly generated value within each list. the generated value can be in range 0 to 5
这里是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
The result :
结果:
random Numbers: 4, 1, 4, 0, 1, 2, 4, 0, 0, 3, 4, 3, 2, 0, 4, 0, 1, 3, 1, 3
随机数字:4、1、4、0、1、2、4、0、0、3、4、3、2、0、4、0、1、3、1、3。
Set : [4, 1, 0, 2, 3]
集合:[4,1,0,2,3]
list : [4, 1, 4, 0, 1, 2, 4, 0, 0, 3, 4, 3, 2, 0, 4, 0, 1, 3, 1, 3]
列表:[4 1 4 0,1,2,4,0,0,3、4、3、2 0 4 0,1,3,1,3)
You can see that the difference is that:
你可以看到区别是:
- Set does not allow duplicate values.
- Set不允许重复的值。
- List allow duplicate values.
- 允许重复的值列表。
#23
-1
Like the answer as SET don't have duplicate value and List can. Of course, order is another one thing to different them apart.
如设置的答案一样,没有重复的值和列表可以。当然,秩序是另一种不同的东西。
#24
-1
Set: A Set cannot have Duplicate elements in its collections. it is also an unordered collection. To access the data from Set, it is required to use Iterator only and index based retrieve is not possible for it. It is mainly used whenever required uniqueness collection.
集合:集合中不能有重复的元素。它也是一个无序的集合。要从Set中访问数据,需要使用迭代器,而基于索引的检索是不可能的。它主要用于任何需要的唯一性集合。
List: A List can have duplicate elements, with the natural ordered as it is inserted. Thus, it can be retrieved data based on index or iterator. It is widely used to store collection which needs to access based on index.
列表:一个列表可以有重复的元素,在插入的时候自然排序。因此,可以基于索引或迭代器检索数据。它被广泛用于存储需要根据索引访问的集合。
#25
-2
TOPIC Name: List VS Set
主题名称:列表VS设置。
I have just gone through Java's most important topic called Collections Framework. I thought to share my little knowledge about Collections with you. List, Set, Map are the most important topic of it. So let's start with List and Set.
我刚刚经历了Java最重要的主题——集合框架。我想和你分享我的收藏知识。列表,设置,地图是最重要的主题。我们从列表开始。
Difference between List and Set:
列表与集合的区别:
-
List is a collection class which extends
AbstractList
class where as Set is a collection class which extendsAbstractSet
class but both implements Collection interface.List是一个扩展类,它扩展了AbstractList类,其中集合是扩展AbstractSet类的集合类,但都实现了集合接口。
-
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.
列表接口允许重复的值(元素),而Set接口不允许重复的值。如果设置了重复元素,它将替换旧的值。
-
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
.列表接口允许NULL值,其中设置接口不允许空值。如果在设置中使用Null值,它会给出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. (AlthoughSortedSet
does usingTreeSet
, andLinkedHashSet
maintains insertion order).列表接口维护插入顺序。这意味着我们在列表中添加元素的方式与使用迭代器或for-each样式获得的方式相同。而设置实现并不一定要维护插入顺序。(尽管SortedSet使用TreeSet,而LinkedHashSet维护插入顺序)。
-
List interface has its own methods defined whereas Set interface does not have its own method so Set uses Collection interface methods only.
List接口有自己定义的方法,而Set接口没有自己的方法,所以Set只使用集合接口方法。
-
List interface has one legacy class called
Vector
whereas Set interface does not have any legacy class列表接口有一个名为Vector的遗留类,而Set接口没有任何遗留类。
-
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最后但不是最不重要的……listIterator()方法只能用于遍历列表类中的元素,而我们可以使用iterator()方法来访问Set类元素。
Anything else can we add? Please let me know.
还有什么要补充的吗?请让我知道。
Thanks.
谢谢。
#26
-3
Set:
设置:
Cannot have duplicate values Ordering depends on implementation. By default it is not ordered Cannot have access by index
不能有重复的值排序依赖于实现。默认情况下,它不是命令不能通过索引访问。
List:
列表:
Can have duplicate values Ordered by default Can have access by index
默认情况下,可以通过索引访问重复的值吗?