Difference between List, Set and Map in java


In this Collection framework tutorial we will learn Difference between List, Set and Map in java, it forms the base of java collection api. We will find out what are most important differences between java.util.List, java.util.Set and java.util.Map in java.




10 Differences between java.util.List, java.util.Set and java,util.Map in java >


Property
java.util.List
java.util.Set
java.util.Map
1
Duplicate elements
List allows to store duplicate elements in java.
Set does not allow to store duplicate elements in java.
Map stores data in form of key-value pair it does not allow to store duplicate keys but allows duplicate values in java.
2
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.
Most of the java.util.Map implementation does not maintain insertion order.

HashMap does not maintains insertion order in java.

Thought LinkedHashMap maintains insertion order of keys in java.

TreeMap is sorted by natural order of keys 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 and ConcurrentSkipListSet does not allow to add null in java.
Lets look at Map implementations -

HashMap allows one null key and many null values.
LinkedHashMap allows one null key and many null values.

TreeMap doesn't allow null key but allow many null values.

Hashtable doesn't allow null key or null values.
ConcurrentHashMap doesn't allow null key or null values.
ConcurrentSkipListMap doesn't allow null key or null values.
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.

Map 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.

HashMap, Hashtable, ConcurrentHashMap,  LinkedHashMap,  TreeMap,  ConcurrentSkipListMap,  IdentityHashMap,WeakHashMap,  EnumMap classes implements Map 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.
Map provides three type of iterators -

map.keySet().iterator() method returns iterator to iterate over keys in HashMap

map.values().iterator() method returns iterator to iterate over keys in HashMap in java.

map.entrySet().iterator() method returns iterator to iterate over keys in HashMap.
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.
Map uses hashing technique for storing key-value pairs.
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.
Map is not index based structure at all in java.
9
unsynchronized implementations

HashMap,  LinkedHashMap,  TreeMap,  IdentityHashMap, WeakHashMap,  EnumMap

10
synchronized implementations

Hashtable, ConcurrentHashMap, ConcurrentSkipListMap,




So in this Collection framework tutorial we learned difference between List, Set and Map in java. We finded what are most important differences between java.util.List, java.util.Set and java.util.Map in java.

Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy(JMSE) on facebook, following on google+ or Twitter.

/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */


RELATED LINKS>


ArrayList vs LinkedList in java - Similarity and Differences


ArrayList vs Vector in java - Similarity and Differences


HashMap and Hashtable in java - Similarity and 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


Map hierarchy tutorial in java - Detailed - java.util.HashMap, java.util.Hashtable, java.util.concurrent.ConcurrentHashMap, java.util.LinkedHashMap, java.util.TreeMap, java.util.concurrent.ConcurrentSkipListMap, java.util.IdentityHashMap, java.util.WeakHashMap, java.util.EnumMap classes


No comments:

Post a Comment