What is the Difference Between List and Set

The main difference between List and Set is that List stores duplicate values while Set does not store duplicate values.

Java programming language supports Collections. List and Set belong to Collection hierarchy. Both are interfaces that extend the Collection interface. These interfaces provide methods such as add, remove, clear, size, etc.to perform operations on the collection.

Key Areas Covered

1. What is List
     – Definition, Functionality
2. What is Set
     – Definition, Functionality
3. What is the Difference Between List and Set
     – Comparison of Key Differences

Key Term

ArrayList, Collections, HashSet, Interface, LinkedList, List, Set, Vector

Difference Between List and Set - Comparison Summary

What is List

List is a child interface of Collection interface. It is capable of maintaining the elements in the inserted order.  Furthermore, it can also contain duplicate values. The classes ArrayList, LinkedList, and Vector implement the List interface. Programmers can create instances of each as follows.

List list1= new ArrayList();

List list2 = new LinkedList();

List list3 = new Vector();

It is also possible to store a specific type of objects using Generics. The syntax is as follows.

List<Obj> list = new List<Obj> ();

The Obj refers to the type of Objects the list can store. For example,

List<Integer> list1= new ArrayList<Integer>();

The list1 is an ArrayList which stores Integer type objects. The same theory can be applied to other list types such as LinkedList and Vector as well.

ArrayList does not provide thread safety. Therefore, accessing the same ArrayList from multiple threads can cause inconsistency in data. In LinkedList, the elements can connect to each other in forward and backward directions. Moreover, Vector is similar to an ArrayList, but it provides thread safety.

An example program is as follows.

Difference Between List and Set

Figure 1: Java program with ArrayList

Letters is an object of ArrayList. The add method helps to insert elements to the ArrayList. The iterator() returns the iterator to the beginning of the collection. The while loop calls the hasNext() method in each iteration. It will return true as long as there are elements available in the collection. Inside the loop, the next() method helps to get the next data item in the collection. The System.out.println displays the element on the console.

When observing the output, we can see that the ArrayList maintains the data inserted order. Furthermore, letter “m” is inserted twice. ArrayList contains both m. Therefore, List stores duplicate values.

What is Set

Set is a child interface of Collection interface. It does not support duplicate elements. Therefore, it maintains a unique set of elements. The HashSet, LinkedHashSet and TreeSet classes implement the Set interface. Programmers can create instances of each as follows.

Set set1= new HashSet();

Set set2 = new LinkedHashSet();

Set set3= new TreeSet();

It is also possible to allow storing specific type of objects using Generics. The syntax is as follows.

Set<Obj> set = new Set<Obj> ();

The Obj refers to the type of Objects the set can store.

For example,

Set<Integer> set1= new HashSet<Integer>();

The set1 is a HashSet which stores Integer type objects. The same theory can be applied to other set types such as LinkedHashSet and TreeSet as well.

HashSet, LinkedHashSet and TreeSet classes implement the Set interface. HashSet does not maintain the data inserted order. A LinkedHashSet maintains the data inserted order. Furthermore, TreeSet does not maintain the data inserted order, but it stores the elements in a sorted manner. An example program is as follows.

Main Difference - List vs Set

Figure 2: Java program with HashSet

The letters is an object of HashSet. The add method helps to insert elements to the HashSet. This program also contains iterator(), hasNext() and next() methods as the above program.

When observing the output, we can see that HashSet does not maintain the data inserted order. Furthermore, letter “m” is inserted twice, but it only contains one m. Therefore, Set does not store duplicate values.

Difference Between List and Set

Definition

List Interface is a sub-interface of Collection that contains methods to perform operations such as insert and delete based on the index. Whereas, Set Interface is a sub-interface of Collection that contains methods to perform operations such as insert and delete elements while maintaining unique elements. Thus, this is the fundamental difference between List and Set.

Duplication

The main difference between List and Set is that List stores duplicate values whereas Set does not store duplicate values.

Classes

ArrayList, LinkedList, and Vector implement the List interface while HashSet, LinkedHashSet and TreeSet classes implement the Set interface.

ListIterator

More to this, it is possible to use an Iterator or a ListIterator to traverse the items in the List. However, it is not possible to use the ListIterator to traverse the items in the Set. Hence, this is another difference between List and Set.

Null Values

Besides, another difference between List and Set is null values. List can have many null values while Set can only have a single null value.

Conclusion

List and Set are two interfaces in the Collection hierarchy. The main difference between List and Set is that List store duplicate values while Set does not store duplicate values. In other words, List can have the same element multiple times while a set contains only the unique elements.

Reference:

1. “List Interface in Java with Examples.” GeeksforGeeks, 26 Nov. 2018, Available here.
2. “Set in Java.” GeeksforGeeks, 11 Dec. 2018, Available here.

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply