What is the difference between Set and List?

2019-01-01 06:02发布

What is the fundamental difference between the Set<E> and List<E> interfaces?

标签: java list set
26条回答
听够珍惜
2楼-- · 2019-01-01 06:37

List:

Lists generally allow duplicate objects. Lists must be ordered, and are therefore accessible by index.

Implementation classes include: ArrayList, LinkedList, Vector

Set:

Sets 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)

查看更多
几人难应
3楼-- · 2019-01-01 06:37

1.List allows duplicate values and set does'nt allow duplicates

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.

查看更多
怪性笑人.
4楼-- · 2019-01-01 06:38

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

查看更多
看风景的人
5楼-- · 2019-01-01 06:39

List is an ordered sequence of elements whereas Set is a distinct list of elements which is unordered (thank you, Quinn Taylor).

List<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.

Set<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.

查看更多
流年柔荑漫光年
6楼-- · 2019-01-01 06:39

A Set cannot contain duplicate elements while a List can. A List (in Java) also implies order.

查看更多
旧时光的记忆
7楼-- · 2019-01-01 06:40

Ordering... a list has an order, a set does not.

查看更多
登录 后发表回答