JAVA COLLECTION Differences
JAVA COLLECTION Differences
List Map
1. List Interface Extends Collection interface Map Interface doesn't extend Collection
Interface.
2. Duplicate element are allowed. Duplicate keys are not allowed but
duplicate values are allowed.
3. Multiple null values can be stored. Only one null key can be stored but
multiple null values are allowed.
4. Belongs to java.util package Belongs to java.util package
Set Map
1. Set contains values only Map contain Key and values both.
2. Set contain unique values Map can contain unique keys with
Duplicate values.
3. Set holds a single number of null values Map can include a single null keys with
N number of null values.
4. Belongs to java.util package Belongs to java.util package
5. Extend the Collection Interface Doesn't extend the Collection interface.
6. Duplicate values are not allowed Duplicate keys are not allowed but allow
duplicate value.
7. Only one null value can be stored. Only one null key can be stored but
multiple null values are allowed.
Array ArrayList
1. Java.util.Array is a Class. Java.util.ArrayList is a Class.
2. Array is of Fixed size ,means we can not ArrayList is not of Fixed size ,we can
resize As per need. change The size dynamically.
3. Arrays can store Primitives datatypes as ArrayList can not store the primitive
well as objects. datatypes.
It can only store the objects.
4. Arrays are of static types. ArrayList is of dynamic size.
5. It is Strongly typed. It is Loosly typed.
6. No need to box and unbox the element. Needs to box and unbox elements.
7. Can not be dynamically resized. Can be dynamically resized.
List ArrayList
1. List is an Interface. ArrayList is a Class.
2. List Interface is extend the Collection ArrayList extends AbstractList Class and
Framework. implements List Interface.
3. List can not be instantiated. ArrayList can be Instantiated.
4. List Interface is used to create a list of ArrayList Class is used to create a dynamic
elements(objects) which are associated array that contains objects.
with Their index number.
5. List Interface creates a collection of ArrayList creates an Array of object where
elements that are stored in a Sequence the array can grow dynamically.
and they are Identified and Accessed using
index.
ArrayList LinkedList
1. This class uses a dynamic array to store This class uses a doubly linked list to store
the the
Element in it. With introduction of Element in it . Similar to the ArrayList ,this
Generics, class also supports the storage of all types
This class supports the storage of all types of objects.
of objects.
2. Manipulating ArrayList takes more time Manipulating LinkedList takes less time
due to Compared to the ArrayList because ,in
The internal implementation .Whenever Doubly linked list , there is no concept of
we shifting the memory bits .the List is
Remove an element , internally , the array traversed
is And reference link is chnaged.
Traversed and memory bits are shifted
3. This class implements List Interface. This class implements both the List
therefore Interface
This act as list. And the Deque interface. therefore it can
act
As a list or deque.
4. This class works better when the This class works better when the
application application demands manipulation of the
Demands storing the data and accessing stored data.
it.
5. Effective for data storage and access. Effective for data manipulation.
6. Manipulation of element is Slower. Manipulation of element is faster.
7. ArrayList is better to store and fetch the Linked List is better is manipulate the data
data.
8. ArrayList provides Random access LinkedList does not provide random
access.
HashSet TreeSet
1. HashSet maintains no order. TreeSet maintains Ascending order.
2. HashSet impended by hash table. TreeSet implemented by tree structure.
3. HashSet perform faster than TreeSet. TreeSet perform slower than HashSet.
4. HashSet is backed by HashMap. TreeSet is backed by TreeMap.
5. Uses hascode() and equals() for Uses compare() and CompareTo() for
comparing. comparing.
6. Uses HashMap to store the element. Uses TreeMap to store the element.
HashMap HashTable
1. HashMap is not Synchronised in nature. HashTable is Synchronised in nature.
2. HashMap allows one null key and multiple Hashtable doesn't allow any null key and
null values. Null value.
3. HashMap is not Thread-Safe ,so it is useful Hashtable is Thread-Safe, and it can be
for shared
Non-Threaded application. Between the various threads.
4. HashMap inherits the AbstractMap class. Hashtable inherits the Dictionary class.
5. HashMap Has faster Processing. HashTable Has Slower Processing.
6. HashMap traversed by Iterator. HashTable traversed by Iterator and
Enumeration.
7. HashMap is a new class introduced in jdk Hashtable is Legacy class.
1.2
8. Iterator in HashMap is fail-fast. Enumerator in Hashtable is not a fail-fast.
9. We can make the hashMap as HashTable is internally synchronised and
synchronised by cant be unsynchronized.
Calling this code
Map m
=Collections.synchrinisedMap(hashMap);
Note:HashMap and HashTable both are used to store the data in key and value form.
HashSet HashMap
1. HashSet contains only values. HashMap includes the entry(key,values).
2. HashSet can be Iterated hashMap needs to convert into Set to be
itrated.
3. HashSet implements the Set interface. Hashmap implements the Map interface.
4. hashSet can not have any duplicate value. HashMap can contain duplicate values
with
Unique keys.
5. HashSet contains the only single number HashMap can hold a single null key with
of null value n numbers of null values.
6. HashSet has slower processing time than HashMap has faster processing time than
Hashmap HashSet.
7. HashSet requires only one objects add HashMap requires two objects
(object o). put(K key, v value) to add an element to
the the HashMap object.
8. HashSet doesn't allow duplicate values. HashMap store key, value pair and it does
not
Allow duplicate keys. if key is duplicate
then the old keys is replace with new
value.
9. HashSet uses the add() method for add or HashMap uses the put() method for
storing the data. storing data.
10. HashSet internally uses the HashMap HashMap internally uses hashing to store
object to store or add the objects. or
Add the object.
11. HashSet internally uses HashMap to add HashMap does not have any concept of
elements. in HashSet, the arguments dummy values.
passed in add(Object )method serve as
key K.
Java internally associated dummy value
for each value passed in add(Object)
method.
12. Ex. Ex.
HashSet is a set eg.{1,2,3,4,5,6,7} HashMap is key->value pair(key to
value)map.
Eg.{a->1,b->2,c->2,d->1}
HashMap TreeMap
1. HashMap maintains no order. TreeMap maintains Ascending order.
2. HashMap is implemented by HashTable. TreeMap is implemented by a tree
structure.
3. HashMap can be sorted by key or values. TreeMap can be sorted by key.
4. HashMap may contain a null key with the TreeMap can not hold a null key but can
Multiple null values. have
Multiple null values.
5. HashMap can contain one null key. TreeMap can not contain any null key.
6. HashMap dont preserve any ordering TreeMap preserve the natural ordering.
7. Implicitly implements the hashing Implicitly implements the Red-Black tree
principal implementation.
HashSet LinkedHashSet TreeSet
Internal Working: Internal Working: Internal Working:
HashSet internally uses LinkedHashSet uses TreeSet uses TreeMap
HashMap for string the LinkedHashMap internally to internally to store the object.
object store the object.
When to use: When to use: When to use:
If you dont want to maintain If you want to maintain If you want to sort the
insertion order but want to insertion order of element element according to some
store unique object then comparator then use
You can use LinkedhashSet. TreeSet.
Order: Order: Order:
HashSet does not maintain LinkedhashSet maintains the While TreeSet orders the
insertion order. insertion order of objects. elements according to
supplied Comparator. By
default, objects will be
placed according to their
natural ascending order.
Complexity of Operations: LinkedHashSet gives While TreeSet gives the
HashSet gives O(1) insertion, removing, and performance of order
complexity for insertion, retrieving operations O(log(n)) for insertion,
removing, and retrieving performance in order O(1). removing, and retrieving
objects operations.
TreeSet TreeMap
1. TreeSet implements SortedSet in java TreeMap implements Map interface in
java.
2. TreeSet stored a single object in java. TreeMap stores two object one key and
one value.
3. Tree doesnot allow duplication object in TreeMap in java allows duplication of
java values.
4. TreeSets implements NavigableSet in java TreeMap implements NavigableMap in
java.
5. TreeSet is sorted based on objects TreeMap is sorted based on keys.
TreeSet SortedSet
1. TreeSet is a concrete class. SortedSet is an interface.
2. TreeSet is more efficient than SortedSet. SortedSet is less efficient than TreeSet.
3. TreeSet allows a heterogeneous object. SortedSet allows a heterogeneous object.
4. TreeSet maintains an object in sorted SortedSet maintains an object in sorted
order. order.
5. Syntax: Syntax:
TreeSet<String> treeset = new SortedSet<String> sortedset = new
TreeSet<String>(); SortedSet<String>();
EnumSet TreeSet
1. Basic: TreeSet is a class that implementation the
EnumSet is a specialized implementation SortedSet interface in java.
of the Set interface.
2. Data Structure: It internally represented as a Red-black
It internally represented as a BitVector. tree.
3. Sorting: It sorts the elements according to sorted
It sorts the elements according to natural order.
order.
Iterable Iterator
1. Iterable is an Interface. Iterator is an interface.
2. Belongs to java.lang package. Belongs to java.util package.
3. Provides one single abstract method Provides two abstract methods called
called hasNext() and next().
Iterator().
4. It is representation of a series of elements It represents the object with iteration
that can be traversed. state.
Iterator ListIterator
1. Iterator traverse the element in forward ListIterator traverse the element in
direction only. backward and forward directions both.
2. Iterator can be used in List,Set,Queue. ListIterator can be used in List only.
3. Iterator can only perform remove ListIterator can perform add remove and
operation while traversing the collection. set operation while while traversing the
collection.
4. Provide no method to retrieve the index Provide method to retrieve the index of
of element. element.
Comparable Comparator
1. Comparable provides only one sort of The Comparator provides multiple sorts of
sequence. sequences.
2. It provides one method named It provides one method named compare()
compareTo(). and equals() method.
3. It is found in java.lang package. It is located in java.util package.
4. If we implement the Comparable The actual class is not changed.
interface, The actual class is modified.
5. Elements are sorted based on natural Elements are sorted based on user
ordering customized ordering.
Iterator Enumeration
1. The Iterator can traverse legacy and non- Enumeration can traverse only legacy
legacy elements. elements
Such as vector,hashtable.
2. The Iterator is fail-fast. Enumeration is not fail-fast.
3. The Iterator is slower than Enumeration. Enumeration is faster than Iterator.
4. The Iterator can perform remove The Enumeration can perform only
operation while traversing the collection. traverse operation on the collection.
5. Is safe and secure Is not safe and secure.
ArrayList Vector
1. ArrayList is not synchronized. Vector is synchronized.
2. ArrayList is not a legacy class. Vector is a legacy class.
3. ArrayList increases its size by 50% of the Vector increases its size by doubling the
array size. array size.
4. ArrayList is not thread-safe as it is not Vector list is thread-safe as it's every
synchronized. method is synchronized.