0% found this document useful (0 votes)
4 views

Collections Differences

Uploaded by

Muddana Sarada
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Collections Differences

Uploaded by

Muddana Sarada
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

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 List allows to Set does not allow to Map stores data in form of
elements store store duplicate elements key-value pair it does not
duplicate in java. allow to store duplicate
elements in keys but allows duplicate
java. values in java.

2 Insertion java.util.List is Most of the java.util.Set Most of the java.util.Map


order ordered implementation does not implementation does not
collection it maintain insertion order. maintain insertion order.
maintain
insertion HashSet does not HashMap does not
order in java. maintains insertion order in maintains insertion order in
java. java.

Thought LinkedHashSet Thought LinkedHashMap


maintains insertion order in maintains insertion order of
java. keys in java.

TreeSet is sorted by TreeMap is sorted by


natural order in java. natural order of keys in
java.

3 Null keys List allows to Most of the Set Lets look at Map
store many implementations allow to implementations -
null keys in add only one null in java.
java. HashMap allows one null
TreeSet and key and many null values.
ConcurrentSkipListSet LinkedHashMap allows
does not allow to add null one null key and many null
in java. 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.
Differenc
es LinkedHashSet TreeSet
HashSet
HashSet uses LinkedHashSet
How they TreeSet uses
HashMap uses
work TreeMap internally
internally to LinkedHashMap
internally to store it’s
store it’s internally to store
? elements.
elements. it’s elements.
TreeSet orders the
elements
LinkedHashSet
according to
maintains
HashSet doesn’t supplied
insertion order of
Order Of maintain any Comparator. If no
elements. i.e
Elements order of comparator is
elements are
elements. supplied, elements
placed as they are
will be placed in
inserted.
their natural
ascending order.

TreeSet doesn’t
allow even a
single null
LinkedHashSet
HashSet allows element. If you try
Null also allows
maximum one to insert null
elements maximum one null
null element. element into
element.
TreeSet, it throws
NullPointerExcepti
on.

Similarities Between HashSet,


LinkedHashSet and TreeSet In
Java :
 All three doesn’t allow duplicate elements.
HashMap,
LinkedHashMap, and
TreeMap
HashMap

 Stores key-value pairs in no particular order.


 Allows one null key and multiple null values.

LinkedHashMap

LinkedHashMap is a subclass of HashMap that maintains the


insertion order of the key-value pairs.

 Maintains insertion order.


 Allows one null key and multiple null values.

TreeMap

TreeMap is a sorted map implementation that uses a Red-Black tree


as the underlying data structure. It maintains key-value pairs in
sorted order according to their keys

 Maintains keys in sorted order.


 Does not allow null keys but permits multiple null values.
Vector vs ArrayList vs LinkedList
Here I mentioned all the differences between Vector, ArrayList,
and LinkedList.
Vector ArrayList LinkedList
Data
Growable array Dynamic array Doubly linked list
structure
Increment
100% 50% No initial size
size
Traverse Uses enumeration Uses iterator Uses iterator
Random and
Accessibility Random and fast Sequential and slow
fast
Order Insertion order Insertion order Insertion order
Duplicates Allowed Allowed Allowed
Insert / Delete Slow Slow Fast
Synchronized Yes No No
Null values Allowed Allowed Allowed

You might also like