Tuesday, April 17, 2012

Difference between List and Set in Java Collection

What is difference between List and Set in Java is a very popular Java collection interview questions and an important fundamental concept to remember while using Collections class in Java. Both List and Set are two of most important Collection classes Java Program use along with various Map implementation. Basic feature of List and Set are abstracted in List and Set interface in Java and then various implementation of List and Set adds specific feature on top of that e.g. ArrayList in Java is a List implementation backed by Array while LinkedList is another List implementation which works like linked list data-structure. In this Java tutorial we will see some fundamental difference between List and Set collections. Since List and Set are generified with introduction of Generics in Java5 these difference 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

Difference between Set and List in Javahere are few note worthy differences between List and Set in Java. Remember that both of them are used to store objects and provides 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 duplicate in Set it will replace the older value. Any implementation of Set in Java will only contains 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 Set interface includes HashSet, TreeSet and LinkedHashSet.

When to use List and Set in Java

Another 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 learn here.These difference between Set and List also teaches us when to use Set and when to prefer List. its 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 than 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.

Further Reading
Java Fundamentals, Part 1 and 2
Java Programming Interview Exposed
Java Fundamentals: Collections

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


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...

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