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
here 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
- Difference between List and Set in Java Collection
- Difference between List and Set in Java Collection
- Difference between List and Set in Java Collection
- Difference between Set, List and Map in Java - Interview question
- Difference between Externalizable and Serializable in Java
- Difference between Vector and ArrayList in java?
- Difference between Comparator and Comparable in Java
- Difference between TreeMap and TreeSet in Java
- Difference between RegularEnumSet and JumboEnumSet in Java
- Difference between Comparator and Comparable in java
- Difference between Stack and Heap in Java
- Difference between ArrayList and Vector In java
- Difference between HashMap and Hashtable in Java
- Difference between Enumeration and Iterator in Java
- Difference between HashSet and HashMap in Java
- Difference between parseInt and valueOf in java?
- Difference between ArrayList and LinkedList in Java
- Difference between List View and DataGrid in WPF
- Tiled Map Editor(一)
- 经典面试题:100的阶乘有几个结尾零
- 栈/入栈/出栈顺序(c语言)-linux
- imageview圆角的实现
- 如何实现将'one,two,three,four,five' 分别打印
- Difference between List and Set in Java Collection
- 开发者必备网站
- JAVA抓取网页的图片,JAVA利用正则表达式抓取网站图片
- TableView中单选的实现
- Java__凯撒密码 非常详细
- 学习网址
- /usr/bin/ld: cannot find -lc
- css 中的background:transparent到底是什么意思有什么作用
- 析构函数和Dispose的使用区别