It’s very important to differentiate between List and Set, so in this Collection framework tutorial we will learn what are differences and similarities between java.util.List and java.util.Set in java.
java.util.List is ordered collection it maintain insertion order in java.
Most of the java.util.Set implementation does not maintain insertion order.
HashSet does not maintains insertion order in java.
Thought LinkedHashSet maintains insertion order in java.
TreeSet is sorted by natural order in java.
List allows to store duplicate elements in java.
Set does not allow to store duplicate elements in java.
List allows to store many null keys in java.
Most of the Set implementations allow to add only one null in java.
TreeSet does not allow to add null in java.
Getting element on specific index
List implementations provide get method to get element on specific index in java.
ArrayList, Vector, copyOnWriteArrayList and LinkedList provides -
Method returns element on specified index.
Get method directly gets element on specified index. Hence, offering O(1) complexity.
Set implementations does not provide any such get method to get element on specified index in java.
listIterator method returns listIterator to iterate over elements in List in java.
listIterator provides additional methods as compared to iterator like
hasPrevious(), previous(), nextIndex(), previousIndex(), add(E element), set(E element)
Set does not provide anything like listIterator. It simply return Iterator in java.
Structure and resizable
List are Resizable-array implementation of the java.util.List interface in java.
Set uses Map for their implementation.
Hence, structure is map based and resizing depends on Map implementation.
Index based structure /RandomAccess
As ArrayList uses array for implementation it is index based structure, hence provides random access to elements.
But LinkedList is not indexed based structure in java.
Set is not index based structure at all in java.
So far we have learned what are differences between List and Set in java. Now we will learn similarities in List and Set in Collection framework in java.
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
Similarity in java.util.List and java.util.Set >
java.util.List and java.util.Set
List and Set both implements java.util.Collection interface.
Implementation of List and Set both returns iterator, iterator method returns iterator to iterate over elements in List/Set.
So in this Collection framework tutorial we learned what are important differences and similarities between List and Set in java.
Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.
Important Similarity and Differences >
List Differences >
List hierarchy tutorial in java - Detailed - java.util.ArrayList, java.util.LinkedList, java.util.vector, java.util.concurrent.CopyOnWriteArrayList classes
Set hierarchy tutorial in java - Detailed - java.util.HashSet, java.util.concurrent.CopyOnWriteArraySet, java.util.LinkedHashSet, java.util.TreeSet, java.util.concurrent.ConcurrentSkipListSet, java.util.EnumSet classes
Important Similarity and Differences Collection classes in concurrent and non-concurrent packages >