Collection
Collection
...................................................................................
............................
1)ArrayList :
...................................................
RandomAccess Interface is marker interface so contains no methods hence required
ability is provided by JVM present in java.util package
...................................................................................
..................
ArrayList:
-Every methods is non-synchronized
-multiple threads are allowed to operate because every method is non-synchronized
so its not Thread-safe
-relatably performance is high
-introduced in 1.2 its non-legacy
Vector:
-Every methods is synchronized
-single thread is allowed to operate only because most of the method is
synchronized hence its Thread-safe
-relatably performance is low
-introduced in 1.0 its legacy
...................................................................................
.......................
LinkList is the best choice for inserion and deletion in middle. Why?
->
...................................................................................
LinkList:
...................................................................................
..........................
Vector Class :
...................................................................................
......
Stack Class :
...................................................................................
....................
1)Enumeration: we can use Emumeration to get elements one by one from legacy
collection object
-we can create Enumeration object by using elements() method of vector class
-enumeration we cannot perform remove element from collection
-enumeration methods
1)public boolean hasmoreElements()
2)public Object nextElement()
-Enumeration e=v.elements();
while(e.hasmoreElements())
{
Integer I = (Integer)e.nextElement();
if(I%2==0)
{
sop(I);
}
}
Integer I = (Integer)e.next();
if(I%2==0)
{
sop(I);
}
else
itr.remove();
}
Limitations of Iteratoe
-we can only move/iterate in forward direction cannot move in backward direction
-we can perform only read and remove operation we cannot perform add and replace
operation
3)ListIterator :
-It has bi-direction cursor that means we can move either in forward direction and
backward direction
-we can perform replacement and the addition on new objects in addition to read and
remove operations
-public ListIterator listIterator
-ListIterator is child Interface of Iterator
-thier are 9 methods in ListIterator :
1)public boolean hasNext()
2)public Object next();
3)public int nextIndex()
4)public boolean hasprevious()
5)public Object previous()
6)public int previousIndex()
7)public void remmove()
8)public void add(Object o)
9)public void set(Object 0) :-to replace current object with new object
Limitation of ListIterator
-the most powrful cursor is ListIterator but its limitation is it is applicable
only for List Objects
..................................................................................
HashSet Class:
-The underline Data Structer is Hash table
-Duplicates are not allowed
-insertion order is not preserved but all
-Null insertion is possible but only one time
-Hetrogeneous objects are allowed
-hashSet implements Serializable,clonable interface
-Random accessor interface is not there
-Everywhere search operation is frequently required go for Hashing related data
structer
-hashSet is best choice if our frequent operation is search operation
-Note : in HashSet duplicates are not allowed if we are trying to insert duplicates
then we wont get any compile or run time errors and the add() method simply returns
false
HashSet h = =new HashSet();
-Constructors in HashSet
1)HashSet h = =new HashSet(); : creates empty HashSet Object with initial capacity
of 16 and default Fill Ratio: 0.75(75%)
Note : default Fill Ratio is 75% means when HashSet fills to 75% automatic new
HashSet Object will be created
Note
2)HashSet h = =new HashSet(int initialcapacity); : creates an empty HashSet Object
with initial specifieid capacity with default flll ratio 0.75
3)HashSet h = =new HashSet(int initialcapacity,float fillratio);
4)HashSet h = =new HashSet(Collection c); : interconversion of collection obj
..............................................................................
100
101
104
106
110
115
120
...................................................................................
......
Note : until 1.6 version Null is allowed as a first element to the empty TreeSet
but from 1.7 version onwards Null is not allowed even as a first element
...................................................................................
.................
sop("A".compareTo("Z"));-ve
sop("Z".compareTo("K"));+ve
sop("A".compareTo("A")); 0
sop("A".compareTo(null));RE : NPE
Note :if we are depending on default natural sorting order then while adding obj
into TreeSet JVM willcall compareTo() method
TreeSet t = new TreeSet();
t.add("K")
t.add("Z")->"Z".compareTo("K") +ve Note : in treeset algo. greater means right side
t.add("A")->"A".compareTo("K") -ve
t.add("A")->"A".compareTo("K") -ve "A".compareTo("A") 0 : here second operations
because root "K" left side already have "A" element
sop(k); [A,K,Z]
...................................................................................
.............................................
Comparator Interface :
Q.Write a program to insert Integer Objects into the TreeSet where the sorting
order is descending order
->
10
/ \
15 0
/ /
20 5
t.add(0);
//compare(0,10)
t.add(15);
//compare(15,10)
t.add(5);
//compare(5,10)
//compare(5,0)
t.add(20);
//compare(20,10)
//compare(20,15)
t.add(20);
//compare(20,10)
//compare(20,15)
//compare(20,20)
sop(t)
//{20,15,10,5,0}
}
}
Note : at line 1 if we arenot passing comparator object then internally JVM will
call compareTo() method which is meantfor default natural sorting order
...................................................................................
...................
...................................................................................
............................
m.put(101,"durga")
m.put(102,"steve")
m.put(101,"ravi") // durga will be replacedwith ravi having same 101 key associated
Note :if the key is already present then old value will be replace with new value
and returns old value
2)void putAll(Map m)
3)Object get(key) :
4)Object remove(key)
5)boolean containsKey(key)
6)boolean containsValue(vslue)
7)boolean isEmpty()
8)int size()
9)void clear();
1)Set KeySet()
2)Collection values()
3)Set entrySet()
-Without existing of Map Object there is no chance of existing Entry Object hence
Entry Interface is defined inside Map Intrface
-methods of Entry interface called as entry specific methods and we can apply only
on entry object
Object setKey()
Object getValue()
Object setvalue()
.........................................................
HashMap Class :
-underlyong Data Structer is Hash Table
-insertion order not preserved it is based on hash code of keys
-duplicates key are not allowed , duplicate values are allowed
-implements serializable and clonable but not Random Access interface
-for both key value hetrogenious object is allowed
-null only once in kay,unlimited null in value
-good for serchiing technique
-Constructor
1)HashMap m = new HashMap : creates an empty HashMap object with a default initial
capacity 16 and default fill ratio 0.75
2)HashMap m = new HashMap(int initialcapacity) : creates an empty HashMap object
with the specfied initial capacity and the default fill ratio 0.75
3)HashMap m = new HashMap(int initialcapacity floatfillratio) :
4)HashMap m = new HashMap(Map m) : interconversion
HashTable :
-synchronized
-can operate only single thread at time
-null key not allowe
-null value not allowed
-it is legcy introduced 1.0 v
...................................................................................
...............
Q.How to get sunchrozed version of HashMap Object?
->we can get synchronized version of HashMap by using Synchronizedmap() method of
Collections class
map m=Collections.synchronizedmap(m); //m is non-synchronized HashMap object
reerence
...................................................................................
..............................
2)for empty TreeMap : as the first entry with the Null key is allowed but after
inserting that entry if we tryiing to insert any other entry then we will get RE
expection saying NULLPOINTEREXPE
Note : the above null acceptance rull applicable until 1.6 version only from 1.7
version onwards null is not allowed for key.
-four constructer
1)TreeMap t = new TreeMap(); : creates empty TreeSet with default natural sorting
order
2)TreeMap t = new TreeSet(Comparator c) : creates empty TreeSet objectwhere the
object will be inserted according to customizer sorting order specified by
Comparator object
3)TreeMap t = new TreeSet(SortedMap m); :
4)TreeMap t = new TreeSet(Map m); : for any sorted set obj eqivalent TreeSet object
will be created
-for program code example refer durga java part 13 - time : 41:28
...................................................................................
.......................
HashTable Class : - underlying Data Structer for HashTable Class is Hash table
-insertion order is not preserved it is based on hash code of keys
-duplicate keys are not allowed and values can be duplicated
-Hetrogenious Objects are allowed for both keys and values
-Null is not allowed for both key and value otherwise we will get RE saying NPE
-It implements Clonable,Serializable and clonable interfaces but not RandomAccess
Interface
-Every method present in HashTable is sychronized and hence HashTable Object is
Thread-Safe
-Hashtable is a best choice if our frequent operation is search operation
-Constructors
1)HashTable m = new HashTable : creates an empty HashMap object with a default
initial capacity 11 and default fill ratio 0.75
2)HashTable m = new HashTable(int initialcapacity) : creates an empty HashMap
object with the specfied initial capacity and the default fill ratio 0.75
3)HashTable m = new HashTable(int initialcapacity float fillratio) :
4)HashTable m = new HashTable(Map m) : interconversion
...................................................................................
.....
3)Enumeration propertyNames();
4)void load(InputStream is) : to load properties from properties file into java
properties object
..................................................................................
1.5v enchanchments(Queue Interface)
Queue interface :-Child interface of collection interface
-if we want to represent a group of individual objects prior to processing then we
should go for Queue
-for example : before sending sms message we have to store all mobile numbers in
some Data Structer in which ordered we added mobile numbers in same order only
message should be sent for this FIFO requirement Queue is the best choice
-Usually Queue follows FIFO order but based on our requirement we can implement our
own Priority order also(Priority Queue)
-from 1.5v onwards LinkList Class also implements Queue Interface
-LinkList based implementation of Queue always follows FIFO
-Queue Interface specific methods
1)booleanoffer(Object o) : to add object into the queue
2)pull() : to remove and return head element of the queue. If queue is empty then
this method returns null
3)remove() : to remove and return head element of the queue. If queue is empty then
this method raises RE : NoSuchElementException
4)Object peek() :to return head element of the queue. If queue is empty then this
method returns null
5)Object element() : to return head element of the queue. If queue is empty then
this method raises RE : NoSuchElementException
Priority Queue Class : -if we want to represent a group of indvidual objects prior
to processing according to some priority then we should go for prioroty queue
-the priority can be defauld natural sorting order or customizer sorting order
defined by compararator
-insertion order is nor prserved it is based on some priority
-duplicated not allowed
-if we are depending on default natural sorting order compulspry object should be
homogenious and comparable otherwise we will get RE: CCE
-if we are defining our own sorting by comparator then objects need not be
Homoenious and comparable
-null is not allowed even as the first element also
-constructors
1)PriorityQueue q=new PriorityQueue() :Creates an empty PriorityQueue with default
initial capacity 11 and all object will be inserted according to default sorting
natural order
2)PriorityQueue q=new PriorityQueue(int initialcapacity) :
3)PriorityQueue q=new PriorityQueue(int initialcapacity,Comparator c);
4)PriorityQueue q=new PriorityQueue(Sorted s)
5)PriorityQueue q=new PriorityQueue(Collection c):
class PriorityQueueDemo
{
psvm()
{
PriorityQueue q=new PriorityQueue(15,new MyComparator());
q.offer("A")
q.offer("Z")
q.offer("L")
q.offer("B")
SOP(q);//[Z,L,B,A]
}
}
class MyComparator implements Comparator
{
public int compare(Object obj1,Object obj2)
{
String s1=(String)obj1;
String s2=obj2.toString();
return s2.compareTo(s1);
}
}
...................................................................................
...............................
Enhancment of 1.6v
Note : Can we used onlly for TreeSet Object as implementing class is TreeSet
class NavigableSet
{
PSVM()
{
TreeSet t = new TreeSet();
t.add(1000);
t.add(2000);
t.add(3000);
t.add(4000);
t.add(5000);
SOP(t);//[1000.2000.3000.4000.5000]
SOP(t.ceiling(2000))//2000
SOP(t.higher(2000))//3000
SOP(t.floor(3000))//2000
SOP(t.lower(3000))//2000
SOP(t.pollFirst())//1000
SOP(t.pollLast())//5000
SOP(t.descendingSet)//[4000,3000,2000,1000]
SOP(t)//[2000,3000,4000]
]
}
...................................................................................
..................