Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 16

Advantages of Collection Class

• High Performance.
ArrayList
It Extends AbstractList and Impliments List
Interface

Advantages:
1.It is Dynamic array So it grows as needed.
How to create an ArrayList ?
ArrayList arrayList = new ArrayList();
To know the size:
arrayList.size();
To add value in to arraylist:
arrayList.add();
To remove a value from arraylist
arrayList.remove();
HashSet
It extends AbstractSet and impliments set
interface
Advantages:
Execution time for basic operation is fast.
How to Create?
HashSet hashSet = new HashSet();

To add value in to HashSet:


hashSet.add();
TreeSet
It extends AbstractSet and impliments set
interface
Advantages:
1.Execution time for basic operation is fast
2.Sorting can be possible using treeset.
How to Create?
TreeSet treeSet = new TreeSet();

To add value in to TreeSet:


treeSet.add();
Iterator
It is used to iterate values from collection
class
• How to use?
Arraylist arrayList new ArrayList();
Iterator itr= new arraylist . Iterator()
While(itr.hasNext()){
Object element= itr.next();
}
HashMap
• It uses HashTable to implement the Map
Advantages:

Execution time will be faster even for large


sets
• How to create a HashMap
HashMap hashMap = new HashMap();
hashMap.put(“John”,”2000”);
Set set = hashMap.entrySet();
Iterator i= set.iterator();
While(i.hasNext()){
Map.Entry me= (Map.Entry)i.next();
System.out.println(me.getKey());
System.out.println(me.getValue());
}
hashMap.get(“john”);
Vector
• It is same as ArrayList
• Advantages over Arraylist
1.It is Synchronized
2.It contans Many Legacy methods which are
not part of collection Framework.
• Some of methods used in Vectors
• 1.To add an element
addElement();
• 2.To know specific location
elementAt();
• 3.To obtain first element
firstElement();
4.To retrieve last element
lastElement()

5.To remove element


removeElemet()
• How to use Vector
Vector v=new Vector();
V.addElemet(“Test”);
Enumeration en=v.elements();
While(en.hasMoreElement()){
System.out.println(en.nextElment());
}
HashTable
• HashTable is same as HashMap
It stores Key/Value pair in a hashtable
• Advantage over HashMap
It is Synchronized
• How to Use HashTable
HashTable ht= new HashTable();
Ht.put(“test”);
Enumeration en=ht.keys();
While(en.hasMoreElement()){
System.out.println(en.nextElment());
}

You might also like