Professional Documents
Culture Documents
Collections
Collections
|
|
|
List | Set(I)
Queue(I)
| | | | |
| |
| | | HashSet |
| |
ArrayList LinkedList Vector | SortedSet
| |
| LinkedHashSet |
| |
Stack
NavigableMap(I) PriorityQueue BLOCKINGQUEUE
|
|
TreeSet |--PRIORITYBLOCKINGQUEUE
|--LINKEDBLOCKINGQUEUE
ArrayList
LinkedList
1.It Internaly use dynamic array to store the elements
1.it uses internally a double LinkedList to store the elements.
2.Manupulation ArrayList is slow because it internally use and
2.Manupulation of LinkedList is faster than ArrayList
array.if any element removed from array,all bits are shifted
because it uses a doubly linked list, so no bit shifting
in memory.
is required in memory.
Q)ArrayList ?
1.The Underlying Data Structure is Double LinkedList.
2.Insertion Order is Preserved.
3.Duplicate Objects are allowed.
3.Heterogeneous Objects are allowed.
4.null Insertion is Possible.
5.Implements Serializable and Cloneable Interfaces but Not RandomAccess
Interface.
6.Best Choice if Our Frequent Operation is Insertion OR Deletion in the Middle.
7.Worst Choice if Our Frequent Operation is Retrieval.
LinkedList:
1.The Underlying Data Structure is Double LinkedList.
2.Insertion Order is Preserved.
3.Duplicate Objects are allowed.
4.Heterogeneous Objects are allowed.
5.null Insertion is Possible.
6.Implements Serializable and Cloneable Interfaces but Not
RandomAccessInterface.
7.Best Choice if Our Frequent Operation is InsertionOR Deletion in the Middle.
8.Worst Choice if Our Frequent Operation is Retrieval
Vector:
New Capacity = Current Capacity * 2
1.The Underlying Data Structure is Resizable Array OR Growable Array.
2.Insertion Order is Preserved.
3.Duplicate Objects are allowed.
4.Heterogeneous Objects are allowed.
5.null Insertion is Possible.
6.Implements Serializable, Cloneable and RandomAccess interfaces.
7.Every Method Present Inside Vector is Synchronized and Hence Vector Object is
Thread Safe.
8.Vector is the Best Choice if Our Frequent Operation is Retrieval.
9.Worst Choice if Our Frequent Operation is Insertion OR Deletion in the Middle.
Stack:
It is the Child Class of Vector.
It is a Specially Designed Class for Last In First Out (LIFO) Order.
1) Object push(Object o); To Insert an Object into the Stack.
2) Object pop(); To Remove and Return Top of the Stack.
3) Object peek(); Ro Return Top of the Stack without Removal.
4) boolean empty(); Returns true if Stack is Empty
5) int search(Object o);Returns Offset if the Element is Available Otherwise
Returns -1
Q)Detect Loop in LinkedList?
First, you keep two pointers of the head node.
At each iteration, you move one of the pointers by two steps and the other
one by one step. So you have two pointers tortoise and the hare.
Eventually one of the two cases will happen:
Hare will reach the tail of the linked list(null), which means that there is
no cycle in it
Hare will meet tortoise, which means that there is a cycle
Time complexity is O(N) where N is the number of nodes in the linked list,
space complexity is O(1) as you use only two pointers.
=====================================================================SET===========
=====================================================================
Set:
1.It is the Child Interface of Collection.
2.If we want to Represent a Group of Individual Objects as a Single Entity where
Duplicates are Not allowed and Insertion Order is Not Preserved then we should go
for Set.
3.Set Interface doesn't contain any New Methods and Hence we have to Use Only
Collection Interface Methods
Q)Hashset ?
The Underlying Data Structure is Hashtable.
1.Insertion Order is Not Preserved and it isBased on hashCode of the Objects.
2.Duplicate Objects are Not Allowed. If we are trying to Insert Duplicate
Objects then we
won't get any Compile Time ORRuntime Error.add() Simply Returns false.
3.null Insertion is Possible.
4.Heterogeneous objects are allowed.
5.HashSet implements Serializable and Cloneable Interfaces but Not
RandomAccess.
6.If Our Frequent Operation is Search Operation, then HashSet is the Best
Choice.
LinkedHashSet:
1.The Underlying Data Structure is a Combination of LinkedList and Hashtable
2.Insertion Order will be Preserved.
3.Introduced in 1.4 Version.
SortedSet:
1.It is the Child Interface of Set.
2.If we want to Represent a Group of Individual Objects without Duplicates and
all Objects will be Inserted According to Some Sorting Order,
then we should go for SortedSet.
3. The Sorting can be Either Default Natural Sorting OR Customized Sorting
Order.
4. For String Objects Default Natural Sorting is Alphabetical Order.
5. For Numbers Default Natural Sorting is Ascending Order.
Methods :
Object first(); Returns 1 st Element of the SortedSet.
Object last(); Returns Last Element of the SortedSet.
SortedSet headSet(Object obj); Returns SortedSet whose
Elements are < Object.
SortedSet tailSet(Object obj); Returns SortedSet whose Elements
are >= Object
SortedSetsubSet(Object obj1, Object obj2); Returns SortedSet
whose Elements are >= obj1 and <obj2
Comparator comparator(); Returns Comparator Object that
Describes Underlying SortingTechnique.
If we
are using Default Natural Sorting Order then we will get null.
TreeSet:
1. The Underlying Data Structure is Balanced Tree.
2. Insertion Order is Not Preserved and it is Based on Some Sorting Order.
3. Heterogeneous Objects are Not Allowed. If we are trying to Insert we will
get Runtime Exception Saying ClassCastException.
4. Duplicate Objects are Not allowed.
5. null Insertion is Possible (Only Once).--re check
6. Implements Serializable and Cloneable Interfaces but Not RandomAccess
Interface
=====================================================================MAP===========
==========================================================================
Map(i)
|
|
|
|
| | | | |
Disctionary(AC)
| | | |
|
| | | |
HashTabe
| | | | |
|
| | | |
|
HashMap WeakHashMap IdentityHashMap SortedMap
Properties
| |
|
|
LinkedHashMap NavigableMap
|
TreeMap
Map methods:
-----------
Object put(Object key, Object value);
putAll(Map m) , get(Object key),remove(Object key) ,containsKey(Object
key),containsValue(Object
value),isEmpty() ,size(),clear(),keySet(),values(),entrySet()
HashMap:
-------
1.The Underlying Data Structure is Hashtable.
2.Duplicate Keys are Not Allowed. But Values can be Duplicated.
3.Heterogeneous Objects are allowed for Both Keys and Values.
4.Insertion Order is not preserved and it is based on hash code of the keys
5.null Insertion is allowed for Key (Only Once) and allowed for Values (Any Number
of Times)
HashMap
Hashtable
-------
---------
1.No Method Present in HashMap is Synchronized 1.Every Method Present in
Hashtable is Synchronized.
2.At a Time Multiple Threads are allowed to 2.At a Time Only One
Thread is allowed to Operate on the Hashtable Object and
Operate on HashMap Object Hence it is Thread Safe.
simultaneously and Hence it is Not Thread
Safe
3.Relatively Performance is High. 3.Relatively Performance
is Low
4.null is allowed for Both Keys and Values. 4.null is Not allowed for
Both Keys and Values. Otherwise we will get NPE.
5.Introduced in 1.2 Version and it is Non–Legacy. 5.Introduced in 1.0
Version and it is Legacy.
Q)what is Hashtable?
Creates an Empty Hashtable Object with Default Initial Capacity 11 and Default
Fill Ratio 0.75.
1.The Underlying Data Structure for Hashtable is Hashtable Only.
2.Duplicate Keys are Not Allowed. But Values can be Duplicated.
3.Insertion Order is Not Preserved and it is Based on Hashcode of the Keys.
4.Heterogeneous Objects are Allowed for Both Keys and Values.
5.null Insertion is Not Possible for Both Key and Values. Otherwise we will
get Runtime Exception Saying NullPointerException.
6.Every Method Present in Hashtable is Synchronized and Hence Hashtable Object
is Thread Safe.
Q)Properties:
1.It is the Child Class of Hashtable.
2.In Our Program if anything which Changes Frequently
3.The Main Advantage in this Approach is if a there is a Change in
Properties File, to Reflect that Change Just Redeployment is Enough,
which won't Create any Business Impact.
4.We can Use Properties Object to Hold Properties which are coming from
Properties File.
5. Properties can be used to Represent a Group of Key – Value Pairs where Both
Key and Value should be String Type.
========================================================================QUEUE======
=================================================================
QUEUE
| | | |
| | |
| |
|
| | |
|
| |
|
| |
|
PRIORIRTYQUEUE
TRANSFERQUEUE BLOCKINGQUEUE
|---- PRIORITYQUEUE
|-----
LINKEDBLOCKINGQUEUE
Queue:
1. Queue is a Child Interface of Collection.
2.If we want to Represent a Group of Individual Objects Prior to processing then we
should go for Queue.
3.From 1.5 Version onwards LinkedList also implements Queue Interface.
4. Usually Queue follows FIFO Order. But Based on Our Requirement we can Implement
Our Own Priorities Also (PriorityQueue).
5. LinkedList based Implementation of Queue always follows FIFO Order.
boolean offer(Object o); -- To Add an Object into the Queue.
Object peek(); --To Return Head Element of the Queue ,If Queue is Empty
then this Method Returns null.
Object element(); --To Return Head Element of the Queue,If Queue is Empty then
this Methodraises RE: NoSuchElementException
Object poll(); --To Remove and Return Head Element of the Queue,If Queue is
Empty then this Method Returns null
Object remove(); --To Remove and Return Head Element of the Queue,If Queue
is Empty then this Method raise RE: NoSuchElementException.
PriorityQueue:
1.This is a Data Structure which can be used to Represent a Group of Individual
Objects
2.Prior to processing according to Some Priority.
3.The Priority Order can be Either Default Natural Sorting Order OR Customized
Sorting Order specified by Comparator Object.
4.If we are Depending on Natural Sorting Order then the Objects should be
Homogeneous and Comparable otherwise we will get ClassCastException.
5.If we are defining Our Own Sorting by Comparator then the Objects Need Not
beHomogeneous and Comparable.
6.Duplicate objects are Not Allowed.
7.Insertion Order is Not Preserved and it is Based on Some Priority.
8. null Insertion is Not Possible Even as 1st Element Also.
BlockingQueue:
1.It is the Child Interface of Queue. Present in java.util.Concurrent Package.
2. It is a Thread Safe Collection.
3.It is a specially designed Collection Not Only to Store Elements but also
Supports Flow Control by Blocking Mechanism.
4.If Queue is Empty take() (Retrieval Operation) will be Blocked until Queue will
be Updated with Items.
5. put() will be blocked if Queue is Full until Space Availability.
6.This Property Makes BlockingQueue Best Choice for Producer Consumer Problem.
When One Thread producing Items to the Queue and
the Other Thread consuming Items from the Queue.
TransferQueue:
1.In BlockingQueue we can Only Put Elements into the Queue and if Queue is Full
then Our put() will be blocked until Space is Available.
2.But in TransferQueue we can also Block until Other Thread receiving Our
Element. Hence this is the Behavior of transfer().
3.In BlockingQueue we are Not required to wait until Other Threads Receive Our
Element but in TransferQueue we have to wait until Some Other
Thread Receive Our Element.
4.TrasferQueue is the Best Choice for Message Passing Application where
Guarantee for the Delivery.
Collections (C):
---------------
Collections Class is an Utility Class Present in java.util Package to Define
Several Utility Methods for Collection Objects.
======================================================Concurrent
Collections=======================================================================
--------------------- -------------------
-------------
ConcurrentHashMap synchronizedMap()
Hashtable
-------------------- -------------------
------------
--------------------- ----------------------------
----------------------------------- --------------------------
Property Enumeration
Iterator ListIterator
--------------------- -----------------------------
------------------------------------ ---------------------------
-----------------------------------------------------------------------------------
---------------------------------------------------------
HashMap |
HashSet
|
-----------------------------------------------------------------------------------
---------------------------------------------------------
|
1) In HashMap we store a key-value pair. It maintains the mapping of | 1.In
HashSet, we store objects
key and value
|
2)It does not allow duplicate keys, but duplicate values are allowed. | 2.It
does not allow duplicate values.
3)It can contain a single null key and multiple null values |
3.It can contain a single null value.
5)HashMap uses the put() method to add the elements in the HashMap. |
5.HashSet uses the add() method to add elements in the HashSet.
|
6)HashMap is faster/ than HashSet because values are associated with |
6)HashSet is slower than HashMap because the member object is
a unique key.
| used for calculating hashcode can same for two objects.
7)Only one object is created during the add operation.
| 7)There are two objects created during put operation,