Difference between List and Set in Java Collections

Difference between List and Set in Java Collections –

 

List Set
List in Java allows duplicate elements. 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.
List is an Ordered Collection, 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 is an unordered Collection.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. Set uses equals() method to check uniqueness of elements stored in Set, while SortedSet uses compareTo() method to implement natural sorting order of elements.
List typically allow multiple null elements if they allow null elements at all. Set allow at most one null element.
Implementation of List interface in Java includes ArrayList, Vector and LinkedList. Implementation of Set interface includes HashSet, TreeSet and LinkedHashSet.
Gopal Das
Follow me

Leave a Reply

Your email address will not be published. Required fields are marked *