Saturday, May 27, 2023

Difference between List and Set in Java Collection? Example

What is the difference between List and Set in Java is a very popular Java collection interview question and an important fundamental concept to remember while using the Collections class in Java. Both List and Set are two of the most important Collection classes Java Program use along with various Map implementations. The basic feature of List and Set are abstracted in the List and Set interface in Java and then various implementations of List and Set adds specific features on top of that e.g. ArrayList in Java is a List implementation backed by Array while LinkedList is another List implementation that works like linked list data-structure.

In this Java tutorial, we will see some fundamental differences between List and Set collections. Since List and Set are made Typesafe with the introduction of Generics in Java5 these differences also application to List and Set.

This article is in continuation of my earlier post on Collection e.g. Difference between HashMap vs HashSet, Difference between HashMap and Hashtable, and Difference between Concurrent Collection and Synchronized Collection. If you haven't read them already you may find them useful.


List vs Set in Java

Here are few noteworthy differences between List and Set in Java. Remember that both of them are used to store objects and provides a convenient API to insert, remove and retrieve elements, along with to support Iteration over collection.


1) Fundamental difference between List and Set in Java is allowing duplicate elements. List in Java allows duplicates while Set doesn't allow any duplicate. If you insert a duplicate in Set it will replace the older value. Any implementation of Set in Java will only contain unique elements.

2) Another significant difference between List and Set in Java is order. List is an Ordered Collection while Set is an unordered collection. List maintains insertion order of elements, means any element which is inserted before will go on lower index than any element which is inserted after. Set in Java doesn't  maintain any order. Though Set provide another alternative called SortedSet which can store Set elements in specific Sorting order defined by Comparable and Comparator methods of Objects stored in Set.

3) Set uses equals() method to check uniqueness of elements stored in Set, while SortedSet uses compareTo() method to implement natural sorting order of elements. In order for an element to behave properly in Set and SortedSet, equals and compareTo must be consistent to each other.

4) Popular implementation of List interface in Java includes ArrayList, Vector, and LinkedList. While popular implementation of the Set interface includes HashSet, TreeSet, and LinkedHashSet.

And, if you like to see the difference between List and Set in Java in a tabular format for better understanding, here is a nice table which compare List and Set classes into different parameters:

Difference between List and Set in Java Collection? Example

 

When to use List and Set in Java?

Difference between Set and List in JavaAnother good follow-up question is "when do you use List and Set in Java" , which can also be answered based on properties of List and Set we have learned here.

These difference between Set and List also teaches us when to use Set and when to prefer List. it's pretty clear that if you need to maintain insertion order or object and you collection can contain duplicates than List is a way to go. 

On the other hand if your requirement is to maintain unique collection without any duplicates then Set is the way to go.

Important point to note is that both List and Set are derived from Collection Interface. In short main difference between List and Set in Java is that List is an ordered collection which allows duplicates while Set is an unordered collection which doesn't allow duplicates.


Other Java interview questions you may like
Difference between start and run method of Thread

8 comments :

Anonymous said...

What if I need an ordered collection with unique elements?
(Thanks for you great blog, btw!)

Anonymous said...

Linkedhashset to maintain insertion order like list

Unknown said...

I was asked this question in my last interview. They also asked my for the difference between an ArrayList and Vector.

Javin @ example of hashtable in Java said...

@Unknow indeed this is very popular interview questions on Java collection framework. check here for difference between an ArrayList and Vector.

LinkedHashSet said...

@Anonymous
If you need ordered collection with unique elements than consider using LinkedHashSet which is not a List but a Set implementation with combination of hashtable and LinkedList and maintains insertion order or elements (order on which elements are inserted into Set) just like List interface.

Anonymous said...

Another important difference to note is the complexity of insertion. List is repetitive collection, so if you gotta insert, just insert at a particular node. But set on the other hand is unique and hence it has to validate before insertion. So this makes the Set bit slower than the list

Siraj said...

if u want ordered collection with unique elements
go for LinkedHashSet.

Anonymous said...

What does ConcurrentSkipList do, does it also implement List interface ?

Post a Comment