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:46

This might not be the answer you're looking for, but the JavaDoc of the collections classes is actually pretty descriptive. Copy/pasted:

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.

查看更多
皆成旧梦
3楼-- · 2019-01-01 06:49
╔═══════════════════╦══════════════════════╦═════════════════════════════╗
║                   ║         List         ║            Set              ║
╠═══════════════════╬══════════════════════╬═════════════════════════════╣
║     Duplicates    ║          YES         ║            NO               ║
╠═══════════════════╬══════════════════════╬═════════════════════════════╣
║       Order       ║       ORDERED        ║  DEPENDS ON IMPLEMENTATION  ║
╠═══════════════════╬══════════════════════╬═════════════════════════════╣
║ Positional Access ║         YES          ║            NO               ║ 
╚═══════════════════╩══════════════════════╩═════════════════════════════╝
查看更多
时光乱了年华
4楼-- · 2019-01-01 06:50

TOPIC Name: List VS Set

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.

Difference between List and Set:

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

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

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

  4. 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).

  5. List interface has its own methods defined whereas Set interface does not have its own method so Set uses Collection interface methods only.

  6. List interface has one legacy class called Vector whereas Set interface does not have any legacy class

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

Anything else can we add? Please let me know.

Thanks.

查看更多
泛滥B
5楼-- · 2019-01-01 06:51

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

A list is an ordered group of objects which may contain duplicates. It could be implemented with an ArrayList, LinkedList, etc.

查看更多
其实,你不懂
6楼-- · 2019-01-01 06:53

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.
  • List uses one special Iterator called ListIterator which supports iteration in both directions. Set uses Iterator which supports only 1 way iteration
  • HashSet takes 5.5 times more memory than ArrayList for storing same number of elements.
查看更多
唯独是你
7楼-- · 2019-01-01 06:53

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.

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.

查看更多
登录 后发表回答