What is the fundamental difference between the Set<E>
and List<E>
interfaces?
views:
1570answers:
9- 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.
List
is an ordered sequence of elements whereas Set
is a distinct list of elements which is unordered (thank you, Quinn Taylor).
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.
A Set cannot contain duplicate elements while a List can. A List (in Java) also implies order.
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 Set
s are accessed by key, duplicates are not allowed.
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.
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
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.
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.