TOPIC Name: List VS Set
I have just gone through Java's most important topic called Collections Framework. I thought to share my little knowledge about Collections with you. List, Set, Map are the most important topic of it. So let's start with List and Set.
Difference between List and Set:
List is a collection class which extends
AbstractListclass where as Set is a collection class which extendsAbstractSetclass but both implements Collection interface.List interface allows duplicate values (elements) whereas Set interface does not allow duplicate values. In case of duplicate elements in Set, it replaces older values.
List interface allows NULL values where as Set interface does not allow Null values. In case of using Null values in Set it gives
NullPointerException.List interface maintains insertion order. That means the way we add the elements in the List in the same way we obtain it using iterator or for-each style. Whereas Set interface does
Setimplementations do not maintainsnecessarily maintain insertion order. Although (AlthoughSortedSetdoes usingTreeSet, andLinkedHashSetmaintains insertion order).List interface has it'sits own methods defined whereas Set interface does not have it'sits own method so Set uses Collection interface methods only.
List interface has one legacy class called Vector
Vectorwhereas Set interface does not have any legacy classLast but not the least... The
listIterator()method can only be used to cycle through the elements within List Classes whereas we can use iterator() method to access Set class elements
Anything else can we add? Please let me know.
Thanks.