The collection is extended by the List and Set interface. They both keep up the accumulation of things or elements. However, the key distinction between both is that a List is a collection of ordered elements, and that the elements are added, removed, or accessed with the use of an index variable.
A set, on the other hand, is a collection of things that forbids the presence of duplicate elements. Let's use the comparison table below to look at some additional differences between the List and Set interfaces.
The List interface broadens the collection interface. An organized group of components or objects is referred to as a list. A list, as opposed to a Set, can have duplicate elements. The methods described by Collection List are not the only ones defined; the index-based get() and set() methods are also defined.
The add() and remove() methods, which are derived from Collection, add or remove the given element from the index given in the method argument. A list is a type of array whose size increases as more members are added.
The list doesn't specify any operations that can be performed on a list's range of indexes. It includes a sublist() method that returns a list of sublists within a given range from the original list. The modifications you make to the sublist are reflected in the original list as well. ArrayList, LinkedList, CopyOnWriteArrayList, Vector, and Stack are several implementations of the List interface.
The Set interface enlarges the Collection interface. A group or collection of objects without any duplicates is called a set interface. Accordingly, it follows that there cannot be two references to Null, one reference to Null, or two references to the same object. It is not crucial what order the elements are in. Set, but it doesn't stop the ordered set from happening.
The method defined in Collection is the only method that the Set interface defines. Instead, it limits the ability of the collection's add() and addall() functions to add any duplicate object. Using the add() method of Collection to add any duplicate objects causes incorrect results. If not, it gives back true. HashSet, LinkedHashSet, EnumSet, TreeSet, and CopyOnWriteArraySet are among the set implementations.
Key Java Interface Differences Between List and Set
- While Set does not preserve the order of the elements, there is an exception, List maintains the order of the elements/objects in a collection. The insertion order is maintained by LinkedHashSet.
- Lists are allowed to contain duplicate elements since each element is uniquely identified by its index, while Set is not allowed to have duplicate elements because it lacks the ability to uniquely identify each object in a collection.
- In addition to the methods described in the Collection, List also defines some of its own methods. Set does not specify any methods of its own, although it does prohibit the addition of duplicate elements by Collection's methods.
- The list interface is implemented by the interfaces ArrayList, LinkedList, CopyOnWriteArrayList, Vector, and Stack. As opposed to this, Set is implemented by the interfaces for HashSet, LinkedHashSet, EnumSet, TreeSet, and CopyOnWriteArraySet.
The necessity determines how the List and the Set interface is used. If the arrangement of the objects or elements matters, you must utilize the List interface. Use the Set interface if you don't need any duplicate elements in your collection.