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. 


This is also one of the popular Java Collection interview question and you can expect it during your Java developer interview, especially at beginner level.

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 the difference between the List and Set interface in Java. It's important to remember this difference not just for Java developer interview but also to use the right collection at the right scenario. 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.


Other Java Collection tutorials you may like
  • How to sort a Map by keys and values in Java? (tutorial)
  • Difference between HashMap and LinkedHashMap in Java? (answer)
  • My favorite free courses to learn Java in-depth (courses)
  • Difference between Hashtable and HashMap in Java? (answer)
  • How to sort an ArrayList in ascending and descending order in Java? (tutorial)
  • Difference between HashSet and TreeSet in Java? (answer)
  • My favorite free course to learn Object-Oriented Programming (courses)
  • Difference between ArrayList and HashSet in Java? (answer)
  • Difference between ArrayList and LinkedList in Java? (answer)
  • 7 Best courses to learn design patterns in Java? (courses)
  • Difference between TreeMap and TreeSet in Java? (answer)
  • Top 10 Courses to learn Data Structure and Algorithms in Java? (courses)
  • Difference between HashMap and ConcurrentHashMap in Java? (answer)
  • My favorite courses to learn Software Architecture? (courses)
  • Difference between Vector and ArrayList in Java? (answer)
  • Difference between EnumMap and HashMap in Java

Thanks for reading this Java Collection interview question explanation so far. If you like this article then please share it with your friends and colleagues. If you have any questions or feedback then please drop a comment.

P. S. - If you are want to learn Java from scratch and looking for an online training course to learn Java then you can also check out this Java Tutorial for Complete Beginners course on Udemy. It's completely free and you just need a Udemy account to join this online course.


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.