0% found this document useful (0 votes)
12 views10 pages

Collections Notes

The document provides an overview of the Java Collections Framework, detailing its hierarchy, interfaces, and implementations such as List, Set, Queue, and Map. It explains the characteristics and uses of various collection types, including ArrayList, LinkedList, HashSet, and HashMap, as well as their internal data structures and behaviors. Additionally, it discusses cursors for iterating collections and the Properties class for managing key-value pairs to avoid hardcoding in applications.

Uploaded by

Sanju Shukla
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)
12 views10 pages

Collections Notes

The document provides an overview of the Java Collections Framework, detailing its hierarchy, interfaces, and implementations such as List, Set, Queue, and Map. It explains the characteristics and uses of various collection types, including ArrayList, LinkedList, HashSet, and HashMap, as well as their internal data structures and behaviors. Additionally, it discusses cursors for iterating collections and the Properties class for managing key-value pairs to avoid hardcoding in applications.

Uploaded by

Sanju Shukla
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/ 10

 Why we need to got for Collections.

 What is collection framework.


 Collection Hierarchy
 List Interface & implementations.
 Set Interface & implementations.
 Queue Interface & implemenations.
 Map Interface & implementations.
 Cursors
 Collections

List : It is used to store group of objects (Duplicates are allowed) 4


implementation classes.
1.ArrayList
2.LinkedList
3.Vector
4. Stack

Set : It is used ti store the group of objects (duplicates are not allowed).
1. HashSet
2. LinkedHashSet
3. TreeSet

Queue : It is used to store group of objects(FIFO order).


1.priorityQueue

Map : It is used to store group of objects (Key-value pair).


1.HashMap
2.LinkedHashMap
3.HashTable
4.IdentityHashMap
5. WeakHashMap

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.

-->It will maintain objects in insertion order


--> It is having 4 implementation classes
1.ArrayList
2.LinkedList
3. Vector
4. Stack
List l = new List( );
List l = new ArrayList( );
List l = new LinkedList( );
i.ArrayList-
-- Implementation Class for list interface.
-- Duplicates objects are allowed.
-- Insertion order preserved.
-- Null values are accepted.
-- Internal data structure of ArrayList is growable array.
-- Default capacity is 10.

======================

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.

Iterator= forward direction(List & Set)


ListIterator= forward and backward direction (List implementation classes).
Enumeration= Forward direction & support for legacy collection. (Vector, Stack)

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.

Set implementation classes:-


1.HashSet
2.LinkedHashSet
3.TressSet

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

1.Collections sorting ( comparartor )


2.Fail-Fast & Fail-Safe Collections.
3.Properties class
4.Collection use cases
5.Java.util.Date & Java.util.Calender
6.Java.util.String Tokenizer

Properties Class in Java


Properties is a predefined class available in java.util package.
Properties class extending properties from hashtable class.
properties class to avoid hardcoding in the project ( means fixing the value ).
Hardcoding means fixing the values in the program. Ex ( db properties)
(like db name , password name ).
if java application wants to commicate with the database, then we need to
database credentials in java program.
if we hardcode database credentials in java program then projects
maintainence will become difficult why because in future if database
credentials modified then we need to modify our java program also. If java
program is modified then we need to recompile and re-excute our program/
project will take a lot of time.
To overcome above problems we should not do hardcoding.
to avoid hardcoding in java project java.util.Propetrties class.
properties class is used to read data from properties file.
properties file contains data in key-value format ( like Map ).

You might also like