tags:

views:

1570

answers:

9

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

+4  A: 
  • 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.

Hardwareguy
A set cannot have duplicates.
karim79
Some set implementations are ordered (such as LinkedHashSet, which maintains a LinkedList behind the scenes). But the Set ADT does not have ordering.
Michael Myers
A: 

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

rmarimon
The Set ADT does not specifiy ordering, but some Set implementations (such as LinkedHashSet) keep insertion order.
Michael Myers
However, the more important difference is that sets don't allow duplicates. A bag/multiset does.
Quinn Taylor
+10  A: 

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.

Andrew Hare
Also, sets by definition has no ordering.
Quinn Taylor
@Quinn: very good point.
Andrew Hare
For a SortedSet, there are no two elements where compareTo() == 0, as equals is not called.
Peter Lawrey
+4  A: 

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

Peter
A: 

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.

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 Sets are accessed by key, duplicates are not allowed.

lavinio
Maps store objects by key, but sets store objects using a unique value related to the object, usually its hash code. (Maps may also use hash codes to check for key uniqueness, but they are not required to.)
Quinn Taylor
+2  A: 

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 array, linked list, etc.

Quinn Taylor
+1  A: 

Ordered lists of element (unique or not)
Conform to Java's interface named List
Can be accessed by index

  • LinkedList
  • ArrayList

Lists of unique elements:
Conform to Java's interface named Set
Can not be accessed by index

  • HashSet (unordered)
  • LinkedHashSet (ordered)
  • TreeSet (sorted by natural order or by provided comparator)

Both interfaces Set and List conform to Java's interface named Collection

ivan_ivanovich_ivanoff
+1  A: 

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.

Jeroen van Bergen
A: 

List :

List allowes dups. it is ordered. by using Array,linked list. list ex's Arraylist ,linkedlist,vector.. accessed by index.

Set:

set DO'T allowes dups. it is un-ordered. by using Hash code of objects inserted. set ex's Hashset,linkedhashset....Treeset. HashSet unordered. LinkedHashSet ordered. TreeSet sorted by natural order or by provided comparator.

csds