Collections Notes
Collections Notes
Set : It is used ti store the group of objects (duplicates are not allowed).
1. HashSet
2. LinkedHashSet
3. TreeSet
Cursors->
Cursor are used to iterate collections (retrieve data from collections).
1.Iterate
2.ListIterator
3.Enumeration
Collection Framework related classes & interface are part of java.util package.
Collection Interface
It is a super interface for List, Set and Queue.
Collection interface providing several methods to store and retrieve objects.
List Interface
--> Extending from Collection interface.
======================
ArrayList Constructor
======================
1. ArrayList a1= new ArrayList( );
2. ArrayList a1= new ArrayList (int capacity);
3. ArrayList a1= new ArrayList (Collection c );
Limitations of ArrayList-:
1.ArrayList is not recommended for insertion because it has to a lot Shifting.
2.Arraylist class is recommended for retrieval option because there is an index
concept which can directly take the element out.
ii.LinkedList
Implementation of List Interface.
Internal data structure is double linked list.
Insertion order preserved.
Duplicates objects are allowed.
Homogenous & hetrogeneous data we can store.
The main difference b/w the array and LinkedListList is that
ArrayList uses growable array concept and LinkedList uses
double linkedList concept.
iii. Vector
Implementation class for List Interface.
Internal data structure of growable array.
duplicates are allowed.
Insertion order is preserved.
Vector is called as a legacy class( jdk v 1.0)
To traverse vector we can use Enumeraryion as a cursor.
Enumeration is called legacy cursor.
The main difference between the arraylist, linkedlist is that they are not thread
safe but vectors are thread safe i.e one thread can access only a vector at a
time.
iv.Stack
1.Implementation class of List Interface.
2.Extending from vector class.
3.Data structure of stack is LIFO (last in first out).
The differences between the different class of list interface is the data
structure.
Set :
Set is interface available in java.util package.
Set interface extending from collection interface.
Set is used to store group of objects.
Duplicates objects are not allowed.
Support homogenous &Hetrogeneous data.
i.HashSet:-
Insertion ordered not maintained.
Duplicates are not allowed.
Null objects are allowed.
Initial capacity is 16.
Load Factor 0.75.
Internal data structure is Hashtable.
Constructor
HashSet hs=new HashSet ( );
HashSet hs = new HashSet ( int capacity);
HashSet hs = new HashSet ( int capacity, load factor);
ii.LinkedHashSet :
Implementation class for set Interface.
Duplicates are not allowed.
Internal data structure is Hash table + Double linkedList.
Initial capacity is 16.
Load factor 0.75.
HashSet does not maintain insertion order where as LinkedHashSet
does maintain insertion order.
iii.TreeSet
Natural sorting order, it means whatever type object you give as input it will
automatically arrange itself in a ascending order or in alphabetical order.
Internal data structure is Binary Tree.
Null values are not allowed because it arranges data it ascending order and
null values can’t be compare with other data to arrange itself.
Homogenous values.
When we add null values it will try to compare null values with previous
objects we will get NullPointerException.
TreeSet shouble perform sorting so always it will compare newly added
objects with old objects. In order to compare the objects should be same type
otherwise we will get the ClassCastException.
Map: key, value
Map is an interface available in java.util package.
Map is used to store the data in key-value format.
One key-value pair is called as one entry.
In map keys should be unique and values be duplicates.
Map < K, V > == Map < Object, Object >
If we try store duplicates key in map then it will replace old key data with new
key data.
We can take key value as any type of data.
Map interface having several implementation classes.
i) HashMap
ii) LinkedHashMap
iii) TreeMap
iv) HashTable
v) IdentityHashMap
vi) WeakHashMap
i.HashMap:-
It is implementation class for Map interface.
used to store data in key value format.
Default capacity is 16.
Load Factor is 0.75.
Underlying data structure is hashtable..
insertion order will not be maintained.
ii)LinkedHashMap
Implemetation class for Map Interface.
Maintain Insertion order.
Data structure is HashTable + Double LinkedList.
iii)TreeMap
Implementation class for Map interface.
It maintains natural sorted order for keys.
Internal data structure for tree map is binary tree.
iv) Hashtable
Implemetation class for Map interface.
Default capacity is 11.
Load factor 0.75.
Key-value format to store the data.
Hashtable is legacy class ( jdk 1.0 v )
Hashtable is synchronized.
Queue
It is extending properties from collection interface.
It is used to store group of objects.
Internal data structure is FIFO ( first in first out).
Insertion will happen at end of the collection.
Insertion will happen at the beginning of the collection.
i)ArrayDeque
ii)PriorityQueue
If the thread safety is not required then use HashMao instead if Hashtable.
If thread safety is important then go for concurrentHashMap instead of
Hashtable.
Important Topics