Java Set 和 List 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1035008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between Set and List?
提问by Johanna
What is the fundamental difference between the Set<E>
and List<E>
interfaces?
Set<E>
和List<E>
接口之间的根本区别是什么?
采纳答案by Andrew Hare
List
is an ordered sequence of elements whereas Set
is a distinct list of elements which is unordered (thank you, Quinn Taylor).
List
是一个有序的元素序列,而Set
是一个独特的无序元素列表(谢谢你,奎因泰勒)。
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.
有序集合(也称为序列)。此界面的用户可以精确控制每个元素在列表中的插入位置。用户可以通过它们的整数索引(在列表中的位置)访问元素,并在列表中搜索元素。
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.equals(e2),并且最多包含一个空元素。正如其名称所暗示的那样,该接口对数学集合抽象进行建模。
回答by Hardwareguy
- A List is an ordered grouping of items
- A Set is an unordered grouping of items with no duplicates allowed (usually)
- 列表是项目的有序分组
- Set 是无序的项目分组,不允许重复(通常)
Conceptually we usually refer to an unordered grouping that allows duplicates as a Bag and doesn't allow duplicates is a Set.
从概念上讲,我们通常将允许重复的无序分组称为 Bag,不允许重复的分组是 Set。
回答by Ricardo Marimon
Ordering... a list has an order, a set does not.
排序...列表有顺序,集合没有。
回答by Peter
A Set cannot contain duplicate elements while a List can. A List (in Java) also implies order.
Set 不能包含重复元素,而 List 可以。列表(在 Java 中)也意味着顺序。
回答by lavinio
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.
所有的List
类都保持插入的顺序。它们根据性能和其他特性(例如,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.
这些Set
类不维护插入顺序。它们可以选择性地强加特定顺序(如SortedSet
),但通常具有基于某些散列函数的实现定义的顺序(如HashSet
)。由于Set
s 是通过键访问的,因此不允许重复。
回答by Quinn Taylor
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
等等。
回答by ivan_ivanovich_ivanoff
Ordered lists of element (unique or not)
Conform to Java's interface named List
Can be accessed by index
元素的有序列表(是否唯一)
符合 Java 的命名接口List
可以通过索引访问
implemetented using
使用
- LinkedList
- ArrayList
- 链表
- 数组列表
Lists of unique elements:
Conform to Java's interface named Set
Can notbe accessed by index
独特的元素列表:
顺应名为Java的接口Set
可以不通过索引访问
implemetented using
使用
- HashSet (unordered)
- LinkedHashSet (ordered)
- TreeSet (sorted by natural order or by provided comparator)
- HashSet(无序)
- LinkedHashSet(有序)
- TreeSet(按自然顺序或提供的比较器排序)
Both interfaces Set
and List
conform to Java's interface named Collection
这两种接口Set
和List
符合Java的接口命名Collection
回答by Jeroen van Bergen
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.equals(e2),并且如果它们完全允许 null 元素,它们通常允许多个 null 元素。有人可能希望通过在用户尝试插入它们时抛出运行时异常来实现一个禁止重复的列表,这并非不可想象,但我们希望这种用法很少见。
回答by csds
List:
列表:
List
s generally allow duplicate objects.
List
s must be ordered, and are therefore accessible by index.
List
s 通常允许重复的对象。
List
s 必须是有序的,因此可以通过索引访问。
Implementation classes include: ArrayList
, LinkedList
, Vector
实现类包括:ArrayList
, LinkedList
,Vector
Set:
放:
Set
s do notallow duplicate objects.
Most implementations are unordered, but it is implementation specific.
Set
就做不允许重复的对象。大多数实现都是无序的,但它是特定于实现的。
Implementation classes include:
HashSet
(unordered),
LinkedHashSet
(ordered),
TreeSet
(ordered by natural order or by provided comparator)
实现类包括:(
HashSet
无序)、
LinkedHashSet
(有序)、
TreeSet
(按自然顺序或提供的比较器排序)
回答by Rakesh
1.List allows duplicate values and set does'nt allow duplicates
1.List允许重复值,set不允许重复
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.List 维护您将元素插入到列表中的顺序 Set 不维护顺序。3.List 是一个有序的元素序列,而 Set 是一个独特的无序元素列表。