Difference between List and Set in Java [Answered]

List vs Set in Java
The 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 collections 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 implementations like ArrayList and Vector are the popular implementation of List interface, while HashSet is a popular implementation of the Set interface. 

In this java tutorial, we will mainly see What are differences between the 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 the List and Set you are most likely to be well versed on when to use List and when to choose Set, which is a key skill for any Java programmer. 



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 a lower index than B. Since Set is an UN-ordered collection it doesn't maintain any inserting order of an element, 

Though you can have SortedSet which offers to sort functionality on top of the Set interface and you can impose either natural order or Object or any custom order by using Comparator and Comparable while storing objects 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 objects use Set. duplication of Object is detected using the equals() method

So if two objects are equal using the 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, the compareTo method is used to compare objects 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 the equals method in java.




List vs Set in Java

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

1. List maintains insertion order of elements while Set doesn't maintain any order.

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 the difference between List, Set, and Map in Java:


Difference between List and Set in Java Collection framework



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

Further Learning
Java In-Depth: Become a Complete Java Engineer
Java Fundamentals: Collections
Data Structures and Algorithms: Deep Dive Using Java

4 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
  4. whts the exact purpose of the class n methods..?

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.