0% found this document useful (0 votes)
68 views32 pages

JAVA COLLECTION Differences

The document provides a comprehensive comparison of various Java collection classes and interfaces, including Collection, List, Set, Map, Array, ArrayList, HashSet, TreeSet, HashMap, and others. It details their characteristics, functionalities, and differences, such as ordering, null value handling, and performance. Additionally, it discusses the internal workings of these collections and their appropriate use cases.

Uploaded by

sachinws2015
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views32 pages

JAVA COLLECTION Differences

The document provides a comprehensive comparison of various Java collection classes and interfaces, including Collection, List, Set, Map, Array, ArrayList, HashSet, TreeSet, HashMap, and others. It details their characteristics, functionalities, and differences, such as ordering, null value handling, and performance. Additionally, it discusses the internal workings of these collections and their appropriate use cases.

Uploaded by

sachinws2015
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Collection Collections

1. Java.util.Collection is an Interface. Java.util.Collections is a Utility Class


2. It is used to represent group of Individuals It defines several utility methods that are
Objects as an single unit. Used to operate on collection
3. The Collection is an interface that contain It contains only static method
a static method since java 8.The Interface
can Also contain abstract and default
methods.
4. It is the root interface of the Collection It is utility class.
framework.
5. It is used to derive the data structure of It contains various static methods which
the Collection framework. help in data structure manipulation
6. The Collection interface provides the Collections class is to sort and synchronise
standard Functionality of data structure the collection element.
to List ,Set and Queue.
List Set
1. List can contain duplicate element Set includes unique items.
2. List is an ordered collection which Set is an unordered collection which does
maintain the insertion order not Preserve the insertion order.
3. List Interface contains a single legacy class Set interface does not have any legacy
which is Vector class class.
4. List interface can allow n number of null Set interface only allows a single null value.
values

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.

Both are Using Hashing techniq to store Unique keys.

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.

Performance: The performance of TreeSet performance is


The performance of HashSet LinkedHashSet is slower than better than LinkedHashSet
is better when compared to TreeSet. It is almost similar except for insertion and
LinkedHashSet and TreeSet. to HashSet but slower removal operations because
because LinkedHashSet it has to sort the elements
internally maintains after each insertion and
LinkedList to maintain the removal operation.
insertion order of elements
Compare: LinkedHashSet uses equals() TreeSet uses compare() and
HashSet uses equals() and and hashCode() methods to compareTo() methods to
hashCode() methods to compare it’s objects compare the objects
compare the objects
Null Elements: LinkedHashSet allows only TreeSet does not permit null
HashSet allows only one null one null value. value. If you insert null value
value. into TreeSet, it will throw
NullPointerException.
Syntax: LinkedHashSet obj = new TreeSet obj = new TreeSet();
HashSet obj = new HashSet(); LinkedHashSet();
LinkedHashMap LinkedHashSet
1. Used to store the key-value pair. Used to store the collection of things.
2. Takes unique and no duplicate keys but Stores no duplicate elements.
can take duplicate values.
3. Implements HashMap. Implements HashSet.
4. Map<String,Integer> lhmap=new Set<String> lhset=new
LinkedHashMap<String,Integer>(); LinkedHashSet<String>
Lhset.add("welcome");
Lhmap.put("geeks",4);
Lhmap.put("welcome",8);

5. LinkedHashMap has element in key-values LinkedHashSet simply stores a collection of


pairs so have only one null key and things with one null value.
multiple null values
6. LinkedHashMap replaces the value with LinkedHashSet not change the original
duplicate key. value.
7.

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.

4. Iterator: TreeSet iterator is Fail-fast.


EnumSet iterator is weakly consistent.
5. Best Choice: TreeSet serves as an excellent choice for
EnumSet is best choice for storing storing large amounts of sorted
enumeration type elements. information which are supposed to be
accessed quickly because of its faster
access and retrieval time.

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.

You might also like