Professional Documents
Culture Documents
20 Collection
20 Collection
1. List(I)
2. set(I)
3. queue(I)
1. List(I)
4. order of insertion-maintain
1. Arraylist(IC)
2. vector(IC)------legecy
3. LinkedList(IC)
2. Set(I)
1. Hashset(IC)
2. LinkedHashset(IC)
3. Treeset(IC)
3. queue(I)
1. priorityQueue(IC)
Cursor
2. listIterator -- only for list interface type impl classes --not an universal curser(3)
1. Arraylist(IC)
1. duplicate are allowded in Arraylist
4. order of insertion-maintain
ArryList Example
package CollectionUse;
import java.util.ArrayList;
import java.util.Iterator;
//import com.sun.tools.javac.util.Iterators;
System.out.println(al);
System.out.println(al.size());
System.out.println(al.indexOf("Velocity"));
System.out.println(al.lastIndexOf("Velocity"));
System.out.println(al);
al.set(3, 'B');//update
System.out.println(al);
System.out.println(al.isEmpty());
System.out.println(al.contains(93));
System.out.println(al.get(5));
System.out.println(al);
al.add(1, "MorningBatch");// right
shift--> when you are inserting element in
between arraylist
System.out.println(al);
al.remove(1);//left shit--> when you try
to remove/delete from in between of arraylist
System.out.println(al);
// Iterator it= al.iterator();
//
// while(it.hasNext())
// {
// System.out.println(it.next());
// }
// for(int i =0; i<=al.size()-1;i++)
//
// {
// System.out.println(al.get(i));
// }
//
for(Object o:al)
{
System.out.println(o);
}
2. Vector
4. order of insertion-maintain
Vector Example:
package CollectionUse;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Vector;
Enumeration en=v.elements();
while(en.hasMoreElements())
{
System.out.println(en.nextElement());
}
3. LinkedList
3. order of insertion-maintain
*7. best choice: manipulation operation i.e. insertion in between linkedlist or delete()
*8. worst choice: retrival operation (random access interface is not implemented)
package Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
System.out.println("-----print info in
linkdlist using iterator cursor------");
Iterator itr = ll.iterator();
while(itr.hasNext()) {
System.out.println(itr.next());
}
System.out.println("-----print info in
linkdlist using Listiterator cursor------");
ListIterator litr = ll.listIterator();
while(litr.hasNext()) {
System.out.println(litr.next());
}
System.out.println("-----print info in
linkdlist using for loop------");
for(int i=0; i<=ll.size()-1; i++) {
System.out.println(ll.get(i));
}
System.out.println("-----print info in
linkdlist using foreach loop------");
for(Object s1:ll)
{
System.out.println(s1);
}
}
}
Arraylist Vector
not legacy class legacy class
DS: resizable DS: doubly
I.C=(C.C.*3/2)+1 IC=CC*2
non-synchronized synchronized
not-thread safe thread-safe
performance: high performance: low
List Set
duplicate: allowed duplicate: not allowed
Any no of null values only one null value-except treeset
order of insertion-maintain random insertion --> random/maintain/asscending
storage type- index storage type- hashtable
2. Set(I)
1. Hashset(IC)
2. LinkedHashset(IC)
3. Treeset(IC)
1.Hashset:
5. no default capacity
6. DS: Hashtable
best choice: To remove duplicate elements when order of insertion is not mandatory.
HashSet Exaple:
package CollectionUse;
import java.util.HashSet;
import java.util.Iterator;
//import com.sun.tools.jdi.EventSetImpl.Itr;
System.out.println(h);
System.out.println(h.size());
System.out.println(h.contains('A'));
System.out.println(h.isEmpty());
System.out.println(h);
System.out.println(h.remove('A'));
System.out.println(h);
for(Object o:h)
{
System.out.println(o);
}
h.clear();
System.out.println(h);
System.out.println(h.isEmpty());
2. LinkedHasSet:
4. no default capacity
import java.util.Iterator;
import java.util.LinkedHashSet;
for(Object o:lh)
{
System.out.println(o);
}
lh.clear();
System.out.println(lh);
System.out.println(lh.size());
System.out.println(lh.isEmpty());
}
3. TreeSet:
4. no default capacity.
ClassCastException
best choice: To remove duplicate elements when order of insertion is Asscending order.
Treeset Example
package CollectionUse;
import java.util.Iterator;
import java.util.TreeSet;
Priority Queue
package CollectionUse;
import java.util.PriorityQueue;
collection : interface
collections: class
read
remove
replace
Cursors in Collections
1. Iterator
2. listIterator
1. Only applicable for list interface type implementation classes --> not universal curser
2. Using list iterater we can traverse a List in forward direction and backword direction-->
bidirectional cursor
3. By using listIterator we can perform read, remove, replace and addition of new object
operations.
3. Enumeration
1. Only applicable for legacy classes and hence it is not a universal cursor.
2. Using Enumeration and iterater we can traverse collection object only in forword
direction not a backword --> Single directional cursor
3. By using enumeration we can get only read access.