0% found this document useful (0 votes)
25 views

Java Collection Framework

The document discusses the Java Collection Framework which provides a unified architecture for storing and manipulating groups of objects. It includes interfaces like Set, List, Queue, Deque and classes like ArrayList, LinkedList, HashSet, TreeSet. The framework contains commonly used methods on the Collection interface like add(), remove(), size(). It also discusses the Iterator interface for iterating over elements and classes like ArrayList, LinkedList, HashSet and LinkedHashSet.

Uploaded by

manohar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Java Collection Framework

The document discusses the Java Collection Framework which provides a unified architecture for storing and manipulating groups of objects. It includes interfaces like Set, List, Queue, Deque and classes like ArrayList, LinkedList, HashSet, TreeSet. The framework contains commonly used methods on the Collection interface like add(), remove(), size(). It also discusses the Iterator interface for iterating over elements and classes like ArrayList, LinkedList, HashSet and LinkedHashSet.

Uploaded by

manohar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Java Collection Framework

1. Collection Framework
2. Hierarchy of Collection Framework
3. Methods of Collection interface
4. Iterator interface

Collection Framework provides an architecture to store and manipulate the group of objects.
All the operations that you perform on a data such as searching, sorting, insertion, deletion etc.
can be performed by Java Collection Framework.

Collection simply means a single unit of objects. Collection framework provides many interfaces
(Set, List, Queue, Deque etc.) and classes (ArrayList, Vector, LinkedList, PriorityQueue,
HashSet, LinkedHashSet, TreeSet etc).

What is Collection>

Collection represents a single unit of objects i.e. a group.

What is framework?

 provides readymade architecture.


 represents set of classes and interface.
 is optional.

Collection framework

Collection framework represents a unified architecture for storing and manipulating group of
object. It has:

1. Interfaces and its implementations i.e. classes


2. Algorithm

Do You Know ?
 What are the two ways to iterate the elements of a collection ?
 What is the difference between ArrayList and LinkedList classes in collection
framework ?
 What is the difference between ArrayList and Vector classes in collection framework ?
 What is the difference between HashSet and HashMap classes in collection framework ?
 What is the difference between HashMap and Hashtable class ?
 What is the difference between Iterator and Enumeration interface in collection
framework ?
 How can we sort the elements of an object. What is the difference between Comparable
and Comparator interfaces ?
 What does the hashcode() method ?

Hierarchy of Collection Framework

Let us see the hierarchy of collection framework.The java.util package contains all the classes
and interfaces for Collection framework.

Commonly used methods of Collection interface

There are many methods declared in the Collection interface. They are as follows:

Method Description
public boolean add(object
is used to insert an element in this collection.
element)
public boolean addAll(collection is used to insert the specified collection elements in the
c) invoking collection.
public boolean remove(object
is used to delete an element from this collection.
element)
public boolean is used to delete all the elements of specified collection from
removeAll(Collection c) the invoking collection.
public boolean is used to delete all the elements of invoking collection
retainAll(Collection c) except the specified collection.
public int size() return the total number of elements in the collection.
public void clear() removes the total no of element from the collection.
public boolean contains(object
is used to search an element.
element)
public boolean
is used to search the specified collection in this collection.
containsAll(collection c)
public Iterator iterator() returns an iterator.

Iterator interface

Iterator interface provides the facility of iterating the elements in forward direction only.

Methods of Iterator interface

There are only three methods in the Iterator interface. They are:
1. public boolean hasNext() it returns true if iterator has more elements.
2. public object next() it returns the element and moves the cursor pointer to the next
element.
3. public void remove() it removes the last elements returned by the iterator. It is rarely
used.

What we will learn in Collection Framework ?


 ArrayList class
 LinkedList class
 ListIterator interface
 HashSet class
 LinkedHashSet class
 TreeSet class
 PriorityQueue class
 Map interface
 HashMap class
 LinkedHashMap class
 TreeMap class
 Hashtable class
 Sorting
 Comparable interface
 Comparator interface

Difference between List and Set:


List can contain duplicate elements whereas Set contains unique elements only.

HashSet class:

 uses hashtable to store the elements.It extends AbstractSet class and implements Set interface.
 contains unique elements only.

Hierarchy of HashSet class:

Example of HashSet class:

1. import java.util.*;  
2. class Simple{  
3.  public static void main(String args[]){  
4.    
5.   HashSet al=new HashSet();  
6.   al.add("Ravi");  
7.   al.add("Vijay");  
8.   al.add("Ravi");  
9.   al.add("Ajay");  
10.   
11.   Iterator itr=al.iterator();  
12.   while(itr.hasNext()){  
13.    System.out.println(itr.next());  
14.   }  
15.  }  
16. }  

Output:Ajay
Vijay
Ravi

LinkedHashSet class:
 contains unique elements only like HashSet. It extends HashSet class and implements Set
interface.
 maintains insertion order.

Hierarchy of LinkedHashSet class:


Example of LinkedHashSet class:

1. import java.util.*;  
2. class Simple{  
3.  public static void main(String args[]){  
4.    
5.   LinkedHashSet al=new LinkedHashSet();  
6.   al.add("Ravi");  
7.   al.add("Vijay");  
8.   al.add("Ravi");  
9.   al.add("Ajay");  
10.   
11.   Iterator itr=al.iterator();  
12.   while(itr.hasNext()){  
13.    System.out.println(itr.next());  
14.   }  
15.  }  
16. }  
Output:>Ravi
Vijay
Ajay

List Interface:
List Interface is the subinterface of Collection.It contains methods to insert and delete elements
in index basis.It is a factory of ListIterator interface.

Commonly used mehtods of List Interface:

1. public void add(int index,Object element);


2. public boolean addAll(int index,Collection c);
3. public object get(int Index position);
4. public object set(int index,Object element);
5. public object remove(int index);
6. public ListIterator listIterator();
7. public ListIterator listIterator(int i);

ListIterator Interface:

ListIterator Interface is used to traverse the element in backward and forward direction.

Commonly used mehtods of ListIterator Interface:

1. public boolean hasNext();


2. public Object next();
3. public boolean hasPrevious();
4. public Object previous();

Example of ListIterator Interface:

import java.util.*;  
class Simple5{  
public static void main(String args[]){  
  
ArrayList al=new ArrayList();  
al.add("Amit");  
al.add("Vijay");  
al.add("Kumar");  
al.add(1,"Sachin");  
  
System.out.println("element at 2nd position: "+al.get(2));  
  
ListIterator itr=al.listIterator();  
  
System.out.println("traversing elements in forward direction...");  
while(itr.hasNext()){  
System.out.println(itr.next());  
 }  
  
  
System.out.println("traversing elements in backward direction...");  
while(itr.hasPrevious()){  
System.out.println(itr.previous());  
 }  
}  
}  
Output:element at 2nd position: Vijay
traversing elements in forward direction...
Amit
Sachin
Vijay
Kumar
traversing elements in backward direction...
Kumar
Vijay
Sachin
Amit

Difference between List and Set:


List can contain duplicate elements whereas Set contains unique elements only.

HashSet class:

 uses hashtable to store the elements.It extends AbstractSet class and implements Set interface.
 contains unique elements only.
Hierarchy of HashSet class:

Example of HashSet class:

1. import java.util.*;  
2. class Simple{  
3.  public static void main(String args[]){  
4.    
5.   HashSet al=new HashSet();  
6.   al.add("Ravi");  
7.   al.add("Vijay");  
8.   al.add("Ravi");  
9.   al.add("Ajay");  
10.   
11.   Iterator itr=al.iterator();  
12.   while(itr.hasNext()){  
13.    System.out.println(itr.next());  
14.   }  
15.  }  
16. }  

Output:Ajay
Vijay
Ravi
LinkedHashSet class:
 contains unique elements only like HashSet. It extends HashSet class and implements Set
interface.
 maintains insertion order.

Hierarchy of LinkedHashSet class:

Example of LinkedHashSet class:

1. import java.util.*;  
2. class Simple{  
3.  public static void main(String args[]){  
4.    
5.   LinkedHashSet al=new LinkedHashSet();  
6.   al.add("Ravi");  
7.   al.add("Vijay");  
8.   al.add("Ravi");  
9.   al.add("Ajay");  
10.   
11.   Iterator itr=al.iterator();  
12.   while(itr.hasNext()){  
13.    System.out.println(itr.next());  
14.   }  
15.  }  
16. }  

Output:>Ravi
Vijay
Ajay

ArrayList class:
 uses a dynamic array for storing the elements.It extends AbstractList class and implements List
interface.
 can contain duplicate elements.
 maintains insertion order.
 not synchronized.
 random access because array works at the index basis.
 manipulation slow because a lot of shifting needs to be occured.
Hierarchy of ArrayList class:

Example of ArrayList:

1. import java.util.*;  
2. class Simple{  
3.  public static void main(String args[]){  
4.    
5.   ArrayList al=new ArrayList();  
6.   al.add("Ravi");  
7.   al.add("Vijay");  
8.   al.add("Ravi");  
9.   al.add("Ajay");  
10.   
11.   Iterator itr=al.iterator();  
12.   while(itr.hasNext()){  
13.    System.out.println(itr.next());  
14.   }  
15.  }  
16. }  

Output:Ravi
Vijay
Ravi
Ajay

Two ways to iterate the elements of collection:

1. By Iterator interface.
2. By for-each loop.

Iterating the elements of Collection by for-each loop:

1. import java.util.*;  
2. class Simple{  
3.  public static void main(String args[]){  
4.    
5.   ArrayList al=new ArrayList();  
6.   al.add("Ravi");  
7.   al.add("Vijay");  
8.   al.add("Ravi");  
9.   al.add("Ajay");  
10.   
11.   for(Object obj:al)  
12.     System.out.println(obj);  
13.  }  
14. }  

Output:Ravi
Vijay
Ravi
Ajay

Storing user-defined class objects:

class Student{  
  int rollno;  
  String name;  
  int age;  
  Student(int rollno,String name,int age){  
   this.rollno=rollno;  
   this.name=name;  
   this.age=age;  
  }  
}  
import java.util.*;  
class Simple{  
 public static void main(String args[]){  
    
  Student s1=new Student(101,"Sonoo",23);  
  Student s2=new Student(102,"Ravi",21);  
  Student s2=new Student(103,"Hanumat",25);  
      
  ArrayList al=new ArrayList();  
  al.add(s1);  
  al.add(s2);  
  al.add(s3);  
    
  Iterator itr=al.iterator();  
  while(itr.hasNext()){  
    Student st=(Student)itr.next();  
    System.out.println(st.rollno+" "+st.name+" "+st.age);  
  }  
 }  
}  
Output:101 Sonoo 23
102 Ravi 21
103 Hanumat 25

Example of addAll(Collection c) method:

1. import java.util.*;  
2. class Simple{  
3.  public static void main(String args[]){  
4.    
5.   ArrayList al=new ArrayList();  
6.   al.add("Ravi");  
7.   al.add("Vijay");  
8.   al.add("Ajay");  
9.     
10.   ArrayList al2=new ArrayList();  
11.   al2.add("Sonoo");  
12.   al2.add("Hanumat");  
13.     
14.   al.addAll(al2);    
15.   
16.   Iterator itr=al.iterator();  
17.   while(itr.hasNext()){  
18.    System.out.println(itr.next());  
19.   }  
20.  }  
21. }  

Output:Ravi
Vijay
Ajay
Sonoo
Hanumat
Example of removeAll() method:

1. import java.util.*;  
2. class Simple{  
3.  public static void main(String args[]){  
4.    
5.   ArrayList al=new ArrayList();  
6.   al.add("Ravi");  
7.   al.add("Vijay");  
8.   al.add("Ajay");  
9.     
10.   ArrayList al2=new ArrayList();  
11.   al2.add("Ravi");  
12.   al2.add("Hanumat");  
13.     
14.   al.removeAll(al2);  
15.   
16.   System.out.println("iterating the elements after removing the elements of al2...");  
17.   Iterator itr=al.iterator();  
18.   while(itr.hasNext()){  
19.    System.out.println(itr.next());  
20.   }  
21.   
22.   }  
23. }  

Output:iterating the elements after removing the elements of al2...


Vijay
Ajay

Example of retainAll() method:

1. import java.util.*;  
2. class Simple{  
3.  public static void main(String args[]){  
4.    
5.   ArrayList al=new ArrayList();  
6.   al.add("Ravi");  
7.   al.add("Vijay");  
8.   al.add("Ajay");  
9.     
10.   ArrayList al2=new ArrayList();  
11.   al2.add("Ravi");  
12.   al2.add("Hanumat");  
13.     
14.   al.retainAll(al2);  
15.   
16.   System.out.println("iterating the elements after retaining the elements of al2...");  
17.   Iterator itr=al.iterator();  
18.   while(itr.hasNext()){  
19.    System.out.println(itr.next());  
20.   }  
21.  }  
22. }  

Output:iterating the elements after retaining the elements of al2...


Ravi

LinkedList class:
 uses doubly linked list to store the elements. It extends the AbstractList class and
implements List and Deque interfaces.
 can contain duplicate elements.
 maintains insertion order.
 not synchronized.
 No random access.
 manipulation fast because no shifting needs to be occured.
 can be used as list, stack or queue.

Example of LinkedList:

1. import java.util.*;  
2. class Simple{  
3.  public static void main(String args[]){  
4.    
5.   LinkedList al=new LinkedList();  
6.   al.add("Ravi");  
7.   al.add("Vijay");  
8.   al.add("Ravi");  
9.   al.add("Ajay");  
10.   
11.   Iterator itr=al.iterator();  
12.   while(itr.hasNext()){  
13.    System.out.println(itr.next());  
14.   }  
15.  }  
16. }  

Output:Ravi
Vijay
Ravi
Ajay

Map Interface
A map contains values based on the key i.e. key and value pair.Each pair is known as an
entry.Map contains only unique elements.

Commonly used methods of Map interface:

1. public Object put(object key,Object value): is used to insert an entry in this map.
2. public void putAll(Map map):is used to insert the specifed map in this map.
3. public Object remove(object key):is used to delete an entry for the specified key.
4. public Object get(Object key):is used to return the value for the specified key.
5. public boolean containsKey(Object key):is used to search the specified key from this
map.
6. public boolean containsValue(Object value):is used to search the specified value from
this map.
7. public Set keySet():returns the Set view containing all the keys.
8. public Set entrySet():returns the Set view containing all the keys and values.

Entry

Entry is the subinterface of Map.So we will access it by Map.Entry name.It provides methods to
get key and value.

Methods of Entry interface:

1. public Object getKey(): is used to obtain key.


2. public Object getValue():is used to obtain value.

HashMap class:
 A HashMap contains values based on the key. It implements the Map interface and extends
AbstractMap class.
 It contains only unique elements.
 It may have one null key and multiple null values.
 It maintains no order.

Hierarchy of HashMap class:

Example of HashMap class:

1. import java.util.*;  
2. class Simple{  
3.  public static void main(String args[]){  
4.    
5.   HashMap hm=new HashMap();  
6.   
7.   hm.put(100,"Amit");  
8.   hm.put(101,"Vijay");  
9.   hm.put(102,"Rahul");  
10.   
11.   Set set=hm.entrySet();  
12.   Iterator itr=set.iterator();  
13.   
14.   while(itr.hasNext()){  
15.    Map.Entry m=(Map.Entry)itr.next();  
16.    System.out.println(m.getKey()+" "+m.getValue());  
17.   }  
18.  }  
19. }  

Output:102 Rahul
100 Amit
101 Vijay

What is difference between HashSet and HashMap?


HashSet contains only values whereas HashMap contains entry(key and value).

TreeMap class
 A TreeMap contains values based on the key. It implements the NavigableMap interface and
extends AbstractMap class.
 It contains only unique elements.
 It cannot have null key but can have multiple null values.
 It is same as HashMap instead maintains ascending order.

Hierarchy of TreeMap class:

Example of TreeMap class:

1. import java.util.*;  
2. class Simple{  
3.  public static void main(String args[]){  
4.    
5.   TreeMap hm=new TreeMap();  
6.   
7.   hm.put(100,"Amit");  
8.   hm.put(102,"Ravi");  
9.   hm.put(101,"Vijay");  
10.   hm.put(103,"Rahul");  
11.   
12.   Set set=hm.entrySet();  
13.   Iterator itr=set.iterator();  
14.   
15.   while(itr.hasNext()){  
16.    Map.Entry m=(Map.Entry)itr.next();  
17.    System.out.println(m.getKey()+" "+m.getValue());  
18.   }  
19.  }  
20. }  

Output:100 Amit
101 Vijay
102 Ravi
103 Rahul

What is difference between HashMap and TreeMap?

1) HashMap is can contain one null key. TreeMap connot contain any null key.

2) HashMap maintains no order. TreeMap maintains ascending order.

Next TopicHashtable Class In Collection Framework

Hashtable
 A Hashtable is an array of list.Each list is known as a bucket.The position of bucket is
identified by calling the hashcode() method.A Hashtable contains values based on the
key. It implements the Map interface and extends Dictionary class.
 It contains only unique elements.
 It may have not have any null key or value.
 It is synchronized.

Example of Hashtable:

1. import java.util.*;  
2. class Simple{  
3.  public static void main(String args[]){  
4.    
5.   Hashtable hm=new Hashtable();  
6.   
7.   hm.put(100,"Amit");  
8.   hm.put(102,"Ravi");  
9.   hm.put(101,"Vijay");  
10.   hm.put(103,"Rahul");  
11.   
12.   Set set=hm.entrySet();  
13.   Iterator itr=set.iterator();  
14.   
15.   while(itr.hasNext()){  
16.    Map.Entry m=(Map.Entry)itr.next();  
17.    System.out.println(m.getKey()+" "+m.getValue());  
18.   }  
19.  }  
20. }  

Output:103 Rahul
102 Ravi
101 Vijay
100 Amit

What is difference between HashMap and Hashtable?

1) HashMap is not synchronized. Hashtable is synchronized.


2) HashMap can contain one null key and multiple Hashtable cannot contain any null key nor
null values. value.

You might also like