Compare the Difference Between Similar Terms

Difference Between List and Set

Key Difference – List vs Set
 

Most programming languages use arrays to store a set of data of the same type. One major drawback of  arrays is that, once the array size is declared, it cannot be modified. If the programmer wants to store a values exceeding the array size, then he should create a new array and copy the existing elements to the new array. In these situations, collections can be used. It is possible to add elements, delete elements and many other operations with the support of collections. There are different types of collections available in programming languages such as Java. List and Set are interfaces of collections hierarchy. The base interface for other interfaces is Collection. The key difference between List and Set is that List supports storing the same element multiple times while Set does not support storing the same element multiple times. Therefore, a Set does not allow duplication.

CONTENTS

1. Overview and Key Difference
2. What is List
3. What is Set
4. Similarities Between List and Set
5. Side by Side Comparison – List vs Set in Tabular Form
6. Summary

What is List?

The list is an interface that extends the Collection interface. There is a number of methods in Collection interface. The add method helps to add an element. The ‘remove method’ is to remove an element. There is ‘addAll method’ to add multiple elements while ‘removeAll method’ to remove the elements from the collection. The contains method helps to find whether a specific object is present in the List or not. The ‘containsAll’ is to find whether a set of objects are present in the collection. The iterator method is used to loop through the items of the list. As List extends Collection, all the methods of Collection belong to List. Other than those methods, the List has methods such as get and set. The programmer can get a value at a specific index using get method. The programmer can set a value at a specific index using the set method. The ‘indexOf’ is used to find the index of an element.

In a List, the operations can be performed according to the position. The programmer can provide the data element that is to be added to the index. So it will be added to the specific index. If the programmer does not give an index, the element will be added to the end of the List. It also maintains the inserted order. If element 1 is added and then element2 is added, then element1 will be before element2.

Figure 01: List and Set

ArrayList, LinkedList, Vector are some classes that implement List. In an ArrayList, accessing an element is fast but inserting and deleting is lower. ArrayList is not thread-safe. Accessing the same ArrayList from multiple threads might not give the same result. In a LinkedList, the elements are linked to both backward and forward. Inserting and deleting elements using a LinkedList is faster than the ArrayList. The LinkedList implements List and Queue Both. Vector is similar to ArrayList, but it is tread-safe because all the methods are synchronized.

What is Set?

Set is an interface that extends the Collection interface. As Set interface extends Collection, all the methods of Collection are also belongs to Set. A Set does not support duplication values. Therefore, the programmer cannot store the same element twice. It maintains a unique set of elements.  The SortedSet interface extends Set interface. SortedSet maintains the elements in sorted order. The NavigableSet interface extends SortedSet. The NavigableSet provides navigation methods such as lower, floor, ceiling etc.

HashSet, LinkedHashSet, and TreeSet are some classes that implement the Set interface. The HashSet implements Set interface. It does not maintain the inserted order. If the values are inserted as a,x,b it might store as, x,a,b. The LinkedSet maintains the inserted order. If the elements are inserted in a,x,b order, the storing order will be a,x,b.  The TreeSet implements Set and NavigableSet. It does not maintain the order of insertion but stores the elements in the sorted order. If the inserted order is a,c,b, then the elements will be stored as a,b,c. All HashSet, LinkedHashSet and TreeSet will not have any duplicate elements.

What are the Similarities Between List and Set?

What is the Difference Between List and Set?

List vs Set

List Interface is the sub interface of Collection that contains methods to perform operations such as insert, delete based on the index. Set Interface is a sub interface of Collection that contains methods to perform operations such as  insert, delete elements while maintaining the unique elements.
 Classes
ArrayList, Vector, and LinkedList are classes that implement List interface. HashSet, LinkedHashSet, and TreeSet are classes that implement Set interface.
Element Duplication
List supports duplication of elements. Set does not support duplication of elements. Elements are unique.

Summary – List vs Set

Collections are used to store elements dynamically. Programming languages such as Java provides Collection interface. List and Set are two interfaces which belong to Collection interface. Both interfaces extend Collection. This article discussed the difference between List and Set. The key difference between List and Set is that List supports storing the same element multiple times while Set does not support storing the same element multiple time. Set always maintains unique elements.

Reference:

1.Point, Tutorials. “Java Collections.”, Tutorials Point, 8 Jan. 2018. Available here