Difference between List and Set in Java Collection

来源:互联网 发布:js对象属性遍历 编辑:程序博客网 时间:2024/05/21 10:12
What is difference between List and Set in Java is a verypopular 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 importantCollection 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 thisJava tutorial we will see some fundamental difference between List and Set collections. Since List and Set are generified with introduction ofGenerics 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 tostore 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 allowsduplicates whileSet 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 maintainsinsertion 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 usescompareTo() 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.Thesedifference 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 shortmain difference between List and Set in Java is that List is an ordered collectionwhich allows duplicates while Set is an unordered collection which doesn't allow duplicates.


Read more: http://javarevisited.blogspot.com/2012/04/difference-between-list-and-set-in-java.html#ixzz2kghs7mHt
原创粉丝点击