Difference between List and Set in Java

List vs Set in Java
Main Difference between List and Set is that List is an ordered Collection while Set is an unordered collection. Java collection framework offers several collection classes for various needs but all collection can be divided into broadly three categories : List, Set, and Map. All List, Set, and Map are defined as interfaces and then you have several implementation like ArrayList and Vector are the popular implementation of List interface, while HashSet is a popular implementation of Set interface. In this java tutorial, we will mainly see What are differences between List and Set collection at the top level and How to choose when to use List in java and when to use Set in Java. I believe once you are able to understand the fundamental difference between List and Set you are most likely be well versed on when to use List and when to choose Set.



Difference between List and Set in Java

As I said the main difference between Set and List is that List is an ordered Collection which means List preserves the order on which an element is inserted into List


So if you insert Object A before Object B then A will be stored at lower index than B. Since Set is an UN-ordered collection it doesn't maintain any inserting order of element, 

Though you can have SortedSet which offers to sort functionality on top of Set interface and you can impose either natural order or Object or any custom order by using Comparator and Comparable while storing object inside Set.

Another significant difference between List and Set is that List allows you to store duplicates in the collection while Set doesn't allow any duplicates. This is very significant as it clearly says that if you want a collection of unique object use Set. duplication of Object is detected using equals() method. 

So if two objects are equal using equals method, the later object will replace the former in Set if added using add() method, due to this reason only one null element is allowed inside Set

It's also worth noting that in the case of SortedSet like TreeSet, compareTo method is used to compare object and decide whether an object is duplicate or not. two objects will be duplicate if their compareTo() method returns zero and that's why it's said that compareTo should be consistent with equals method in java.



For the sake of clarity let's see differences between List and Set interface in point format:


1) List maintains insertion order of elements while Set doesn't maintain any ordering.
2) The list allows duplicate objects while Set doesn't allow any duplicates.


If you compare the implementation of List and Set interface like ArrayList vs HashSet you can see not only differences which are imposed by characteristics of List and Set but also implementation level differences like ArrayList uses an array as data structure while HashSet uses hashing mechanism.

Here is a nice summary of difference between List, Set and Map in Java:


Difference between List and Set in Java Collection framework



That's all on differences between List and Set interface in Java. bottom line is that List is an ordered collection while Set is unordered, List allows duplicates and Set don't allow duplicates. Let us know if you come across any other significant difference between List and Set in Java.



3 comments:

  1. Only meaningful difference between List and Set is that List is ordered collection and contains duplicate while Set is unordered but doesn't allow duplicates.

    ReplyDelete
  2. LinkedHashSet is ordered implementation of Set, so the only difference is allowing duplicates.

    ReplyDelete
  3. could you please write code for this,it was given in an interview....Define a class RepeatedCharacters,using these methods SetgetRepeatedCharacters(Stringmessage)
    MapgetRepeatedCharacters(Stringmessage)..

    ReplyDelete