The List interface allows storing the ordered collection. It is a child interface of Collection. It is an ordered collection of objects in which duplicate values are allowed to store. List preserves the insertion order, it allows positional access and insertion of elements.
public abstract interface List extends Collection
The set interface in the java.util package and extends Collection interface is an unordered collection of objects in which duplicate values cannot be stored. It is an interface that implements the maths set. This interface contains the methods inherited from the Collection interface and adds a feature that restricts to insert the duplicate elements.
Declaration: The Set interface is declared as:
public interface Set extends Collection
Input : Add Elements = [1, 2, 3, 1] Output: Set = [1, 2, 3] List = [1, 2, 3, 1] Input : Add Elements = [a, b, d, b] Output: Set = [a, b, d] List = [a, b, d, b]
Below is the illustration of Set and List :
List = [5, 6, 3, 5, 4] Set = [3, 4, 5, 6]
Difference between List and Set:
List Set 1. The List is an ordered sequence. 1. The Set is an unordered sequence. 2. List allows duplicate elements 2. Set doesn’t allow duplicate elements. 3. Elements by their position can be accessed. 3. Position access to elements is not allowed. 4. Multiple null elements can be stored. 4. Null element can store only once. 5. List implementations are ArrayList, LinkedList, Vector, Stack 5. Set implementations are HashSet, LinkedHashSet.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.