Difference Between List and Set in Java
The primary difference between list and set is that a list allows duplicate elements and maintains their order, while a set ensures element uniqueness without any guaranteed order.
Since lists are ordered, position indexing is allowed in them. However, in unordered items like sets, positional indexing is not possible.
What is List Interface?
List interface in Java is a child interface for Collection objects. As mentioned previously, lists are ordered collections of data. Their ordered nature ensures that their insertion order remains intact. They can be indexed and accessed based on their positions.
Syntax
Example
The output for the following code is:
What is Set Interface?
The set interface in java.util package extends the Collection interface. Sets in java are unordered collections of objects. These restrict duplication of values. Since they are unordered, positional indexing is not allowed in sets.
Syntax
Example
The output for the following code is:
Key Differences Between List and Set
Duplicates:
- List: Allows duplicate elements.
- Set: Does not allow duplicate elements.
Order:
- List: Maintains the order of elements.
- Set: No guaranteed order (except ordered variants like LinkedHashSet).
Index Access:
- List: Supports access by index, e.g., get(index).
- Set: Doesn’t support index-based access.
Examples:
- List: ArrayList, LinkedList.
- Set: HashSet, TreeSet.
Difference Between List and Set in Java
List Interface | Set Interface |
---|---|
Lists allow duplicate values | Sets can't have duplicate values |
Lists can be indexed and positionally accessed | Sets cannot be indexed and cannot be accessed based on their positions |
Multiple null elements can be stored | Only one null element can be stored at a time |
List implementations are ArrayList, LinkedList, Vector, Stack. | Set implementations are HashSet, LinkedHashSet. |
The insertion order is maintained by the List. | It doesn’t maintain the insertion order of elements. |
List is often used when we need to frequently access the elements using their indices. | It is used when we need to store distinct elements. |
The listiterator() is used to iterate the List elements. | The iterator() method is used to iterate Set elements. |
When to Use Sets and When to Use Lists?
Lists could be implemented in ArrayList, LinkedList, Vector, and Stack. Lists are best used when a small dataset is at hand
Sets could be implemented in HashSet, LinkedHashSet, etc. Sets are best used when a larger dataset needs to be processed.
Learn More
To learn more about collections in Java, check this out.
Conclusion
- A List interface is an ordered collection of values in which values may or may not be duplicated
- In Java, a set is a collection that does not allow duplicate elements and has no guaranteed order for its elements.
- In Java, when comparing “list vs set” a list supports ordered collections and allows duplicates, whereas a set emphasizes uniqueness without any specific order.
- Lists allow duplicate values while Sets don't.
- Lists allow positional indexing. The same is not allowed in sets.