Difference between List and Set interface in Java
This post will discuss the difference between the List
and Set
interface in Java.
Two of the most widely used types of collections in Java are List
and Set
, which both extend the Collection
interface. However, List
and Set
also have some significant differences and trade-offs that make them suitable for different scenarios and use cases.
1. Overview of List
interface in Java
The List interface is one of the most commonly used interfaces in Java and belongs to the java.util
package that extends the Collection
interface. It is an ordered collection of elements that allows duplicates. It offers many benefits:
- The
List
interface allows fast access to elements by using the index of the elements. It uses an array to store the elements. When we insert or retrieve an element from theList
, we use the index to find the position of the element in the array. - The
List
interface maintains the order of the elements in the collection. This means that the elements in aList
are stored in a sequential order according to their insertion order. - The
List
interface allows null and duplicate elements in the collection. This means that we can insert, retrieve, or remove null or duplicate values from aList
without any problem. - The
List
interface provides additional methods to manipulate the collection, such asadd()
,remove()
,get()
,set()
,indexOf()
,lastIndexOf()
,subList()
,sort()
, etc. These methods allow us to add, update, delete, or check elements in the collection.
Let’s see an example of how to create and use a List in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import java.util.ArrayList; import java.util.List; class Main { public static void main(String[] args) { // Create a List object List<String> list = new ArrayList<>(); // Add some elements to the list list.add("Aaron"); list.add("Bella"); list.add("Casey"); list.add("Aaron"); // Print the list System.out.println("List: " + list); // [Aaron, Bella, Casey, Aaron] // Get the element at index 1 System.out.println("Element at index 1: " + list.get(1)); // Bella // Set the element at index 2 list.set(2, "David"); // Print the updated list System.out.println("List: " + list); // [Aaron, Bella, David, Aaron] // Remove the element at index 3 list.remove(3); // Print the updated list System.out.println("List: " + list); // [Aaron, Bella, David] // Check if an element exists in the list System.out.println("Contains Aaron: " + list.contains("Aaron")); // true // Add a null element to the list list.add(null); // Print the updated list System.out.println("List: " + list); // [Aaron, Bella, David, null] } } |
2. Overview of Set
interface in Java
The Set
interface is another commonly used interface in Java that belongs to the java.util
package and extends the Collection
interface. It can be either ordered or unordered, depending on the implementation. For example, HashSet
implementation is unordered, LinkedHashSet
implementation is ordered, and TreeSet
implementation is ordered by natural order or by provided comparator. The elements in a Set
can be of any type, and it offers some benefits:
- The
Set
interface allows fast access to elements. Depending upon the implementation, aSet
uses an array or a tree structure to store the elements, where each element is stored as a node in the array or tree. When we insert or retrieve an element from theSet
, we use the element to calculate its hash code or compare with other nodes and find its position in the array or tree. - The
Set
interface does not maintain any order of the elements in the collection. This means that the elements in aSet
are stored based on their hash code or natural ordering or by a providedComparator
, depending upon the implementation. - The
Set
interface does not allow duplicate elements in the collection. However, it allows a single null element in the collection. - The
Set
interface provides additional methods to manipulate the collection, such asadd()
,remove()
,contains()
,size()
,clear()
, etc. These methods allow us to add, delete, or check elements in the collection.
Let’s see an example of how to create and use a Set in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import java.util.HashSet; import java.util.Set; class Main { public static void main(String[] args) { // Create a Set object Set<String> set = new HashSet<String>(); // Add some elements to the set set.add("Aaron"); set.add("Bella"); set.add("Casey"); // Print the set System.out.println("Set: " + set); // [Bella, Aaron, Casey] // Try to add a duplicate element to the set set.add("Aaron"); // Print the set System.out.println("Set: " + set); // [Bella, Aaron, Casey] // Remove an element from the set set.remove("Bella"); // Print the updated set System.out.println("Set: " + set); // [Aaron, Casey] // Check if an element exists in the set System.out.println("Contains Aaron: " + set.contains("Aaron")); // true // Add a null element to the set set.add(null); // Print the updated set System.out.println("Set: " + set); // [null, Aaron, Casey] } } |
3. Difference between List
and Set
interface in Java
Here are some of the main differences between List
and Set
interface in Java:
- The
List
interface is an ordered sequence of elements, whereas theSet
represents a distinct collection of elements that can be either ordered or unordered, depending on the implementation. - The
List
interface maintains the order of the elements in the collection according to their insertion order, whereas theSet
interface does not maintain any order of the elements in the collection according to their hash code or natural ordering or by a providedComparator
. - The
List
interface allows duplicate elements, whereas theSet
can contains only distinct elements. - The
List
interface allows null elements in the collection, whereas theSet
interface allows at most one null elements in the collection. - Several
List
implementations implement theRandomAccess
interface, a marker interface to support constant-time random access. Whereas, none of theSet
implementations can implement theRandomAccess
interface. - The
List
interface provides a special iterator ListIterator to facilitate bidirectional access which is not available forSet
interface. - Popular implementation of
List
interface includes ArrayList and LinkedList, while that ofSet
interface includes HashSet, TreeSet, and LinkedHashSet.
4. What to use and when?
As we have seen, List
and Set
interface are both useful interfaces in Java. Here are some general recommendations on how to choose between them:
- If you need to store a collection of elements where duplicates are allowed and order is maintained, we can use
List
interface. - If you need to store a collection of elements where duplicates are not allowed and order is also not important, we should use
Set
interface. - If you need to store a collection of elements where duplicates are not allowed and order is important, we should use
SortedSet
instead. This interface will ensure that the collection is sorted and unique and prevent any data inconsistency or concurrency issues.
In general, a List
should be used when the insertion order of elements needs to be maintained. Use a set if we need to maintain a collection that contains no duplicate elements.
That’s all about the differences between the List
and Set
interface in Java.
Difference between HashSet, TreeSet, and LinkedHashSet in Java
Difference between Enumeration and Iterator interface in Java
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)