Differences and Similarities between List and Set in java


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.




Differences between java.util.List and java.util.Set in java >


Property
java.util.List
java.util.Set
1
Insertion order
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.
2
Duplicate elements
List allows to store duplicate elements in java.
Set does not allow to store duplicate elements in java.
3
Null keys
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.
4
Getting element on specific index
List implementations provide get method to get element on specific index in java.

ArrayList, Vector, copyOnWriteArrayList and LinkedList provides -
get(int index)
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.

5
Implementing classes
ArrayList, LinkedList, Vector, CopyOnWriteArrayList classes implements List interface in java.
6
listIterator
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.
7
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.
Example > HashSet internally uses HashMap.
8
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 >
Property
java.util.List and java.util.Set
Implements
List and Set both implements java.util.Collection interface.
iterator
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.


RELATED LINKS>

ConcurrentModificationException, Fail-fast and Fail-safe in detail in java


Important Similarity and Differences >

List Differences >

ArrayList vs LinkedList in java - Similarity and Differences


ArrayList vs Vector in java - Similarity and Differences


HashMap and Hashtable in java - Similarity and Differences


Iterator vs ListIterator in java - Similarity and Differences


Iterator vs Enumeration in java - Differences and similarities



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 >

ArrayList vs CopyOnWriteArrayList - Similarity and Differences with program


eEdit
Must read for you :