List and Set interface extends Collection. Both of them maintains the collection of elements or objects. But, the major difference that distinguishes them from each other is List is a collection of ordered element, the elements are added or remove or accessed with the help of an index variable.
On the other hand, Set is a collection of objects where the collection does not allow duplicate elements in it. Let us study some more differences between List and Set interfaces with the help of comparison chart shown below.
Content: List Vs Set Interface
Comparison Chart
Basis for Comparison | List | Set |
---|---|---|
Basic | List maintains the sequence of the elements stored in a list. | Set does not particularly maintain insertion order but, Linked HashSet maintains the insertion order. |
Duplication | List may have duplicate elements in it. | The add() method returns false if you try to insert the duplicate elements. |
Methods | In addition to methods defined in Collection, List defines some of its own methods. | Set does not define any additional method. |
Implementation | List is implemented by ArrayList, LinkedList, CopyOnWriteArrayList, Vector, Stack. | Set is implemented by HashSet, LinkedHashSet, EnumSet, TreeSet, CopyOnWriteArraySet. |
Definition of List
List interface extends Collection interface. A List is an ordered collection of elements or objects. Unlike Set, List may contain duplicate elements. In addition to the methods defined in Collection List defines some methods of its own like index-based get() and set() method.
The add() and remove() methods inherited from Collection that adds or removes the specified element from the index specified in the method argument. List is a kind of array whose size grow as we add elements to the list.
List does not define any method to operate on the range of indexes in a list. It defines a sublist() method which returns a sublist from the original list of a specified range. The changes that you do to the sublist also appear in the original list. List interface is implemented by ArrayList, LinkedList, CopyOnWriteArrayList, Vector, Stack.
Definition of Set
Set interface extends the Collection interface. Set interface is a collection or a group of objects that does not any duplicate object in it. That means two references can not refer one object, or one reference can not refer to two objects, or there can not two references referring to Null. The order or sequence of the element is not important Set, but it is not that it prohibits the ordered set.
Set interface does not define any method in addition to the method defined in Collection. Instead, it restricts the add() and addall() methods of collection to add any duplicate object in a collection. If you try to add any duplicate object in a collection using add() method of Collection it returns false. Otherwise, it returns true. Set interface is implemented by HashSet, LinkedHashSet, EnumSet, TreeSet, CopyOnWriteArraySet.
Key Differences Between List and Set Interface in Java
- The sequence of elements /object in a collection is maintained in List whereas, Set does not maintain the order of the elements but there is an exception LinkedHashSet maintain the insertion order.
- List can have duplicate elements as it identifies any element with its index but, Set does not allow any duplicate elements as it does not have any index kind of element to identify any object in a collection.
- List define some methods by its own, in addition to the methods defined in Collection. On the other hand, Set does not define any method of its own, but it restricts the methods of Collection to add any duplicate elements.
- List is implemented by ArrayList, LinkedList, CopyOnWriteArrayList, Vector, Stack interfaces. On the other hand, Set is implemented by HashSet, LinkedHashSet, EnumSet, TreeSet, CopyOnWriteArraySet interfaces.
Conclusion
The use of List and Set interface depends on upon the requirement. If the order of objects/elements is important, then you must use List interface. If you do not require any duplicate elements in your collection, then you must use Set interface
Leave a Reply