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

Gopal Das

Founder at GopalDas.Org
He is a technology evangelist, Salesforce trainer, blogger, and working as a Salesforce Technical Lead. After working in Java based project implementation, he jumped to the Salesforce system on a whim and never looked back. He fell in love with Salesforce’s flexibility, scalability, and power. He expanded his knowledge of the platform and became a Certified App Builder, Administrator, Platform Developer I, SalesCloud Consultant while leading the Salesforce implementation and technology needs. He has worked in a wide variety of applications/services like desktop, web and mobile applications.
Gopal Das
Follow me

Leave a Reply

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