How Various Java Collection Classes Work

Introduction

 
In this article, we discuss how various Java Collection classes work.
 

HashSet class

 
HashSet extends AbstractSet and implements the Set interface. It creates a collection that uses a hash table (a hash table stores information by using a mechanism called hashing) for storage. It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element.
 
This class supports four constructors that are the following:
  • HashSet( )
  • HashSet(Collection cln)
  • HashSet(int capacity, float fillRatio)
  • HashSet(int capacity)
Example
  1. import java.util.*;  
  2. class HashSetEx {  
  3.  public static void main(String args[]) {  
  4.   HashSet arylst = new HashSet();  
  5.   arylst.add("Paul");  
  6.   arylst.add("John");  
  7.   arylst.add("Paul");  
  8.   arylst.add("San");  
  9.   Iterator itrtr = arylst.iterator();  
  10.   while (itrtr.hasNext()) {  
  11.    System.out.println(itrtr.next());  
  12.   }  
  13.  }  
  14. }   
Output
 
Fig-1.jpg

LinkedHashSet class

  • It contains only unique elements like HashSet.
  • It extends HashSet class and implements Set interface.
  • Maintains insertion order.
Example
  1. import java.util.*;  
  2. class HashSetEx {  
  3.  public static void main(String args[]) {  
  4.   HashSet arylst = new HashSet();  
  5.   arylst.add("Paul");  
  6.   arylst.add("John");  
  7.   arylst.add("Paul");  
  8.   arylst.add("San");  
  9.   Iterator itrtr = arylst.iterator();  
  10.   while (itrtr.hasNext()) {  
  11.    System.out.println(itrtr.next());  
  12.   }  
  13.  }  
  14. }   
Output
 
Fig-2.jpg

TreeSet class

  • This class implements a NavigableSet interface that extends the SortedSet interface.
  • It contains only unique elements like HashSet.
  • Maintains ascending order.
Example
  1. import java.util.*;  
  2. class TreeSetEx {  
  3.  public static void main(String args[]) {  
  4.   TreeSet arylst = new TreeSet();  
  5.   arylst.add("Paul");  
  6.   arylst.add("John");  
  7.   arylst.add("Paul");  
  8.   arylst.add("San");  
  9.   Iterator itrtr = arylst.iterator();  
  10.   while (itrtr.hasNext()) {  
  11.    System.out.println(itrtr.next());  
  12.   }  
  13.  }  
  14. }   
Output
 
fig-3.jpg

Queue Interface

 
It is basically used to order the elements in the FIFO (first in first out) manner.
 

Public Methods

 
Some public methods used in Queue Interface are:
  1. remove()
  2. poll()
  3. elements()
  4. peek()
  5. boolean add (object)
  6. boolean offer (object)

Priority Queue classes

 
The class provides the ability to use a queue, but it does not order the elements in a FIFO manner.
 
Example
  1. import java.util.*;  
  2. class PriorityQueueEx {  
  3.  public static void main(String args[]) {  
  4.   PriorityQueue que = new PriorityQueue();  
  5.   que.add("Paul");  
  6.   que.add("John");  
  7.   que.add("Dinesh");  
  8.   que.add("San");  
  9.   que.add("Rahul");  
  10.   System.out.println("head:" + que.element());  
  11.   System.out.println("head:" + que.peek());  
  12.   System.out.println("iterating the que elements:");  
  13.   Iterator itrtr = que.iterator();  
  14.   while (itrtr.hasNext()) {  
  15.    System.out.println(itrtr.next());  
  16.   }  
  17.   que.remove();  
  18.   que.poll();  
  19.   System.out.println("after removing two elements of queue:");  
  20.   Iterator itrtr2 = que.iterator();  
  21.   while (itrtr2.hasNext()) {  
  22.    System.out.println(itrtr2.next());  
  23.   }  
  24.  }  
  25. }   
Output
 
fig-4.jpg

HashMap class

  • may have one null key and multiple null values.
  • contains values based on the key.
  • implements the Map interface and extends the AbstractMap class.
  • contains only unique elements.
  • maintains no order.
Example
  1. import java.util.*;  
  2. class HashMapEx {  
  3.  public static void main(String args[]) {  
  4.   HashMap hsmp = new HashMap();  
  5.   hsmp.put(10"Paul");  
  6.   hsmp.put(11"John");  
  7.   hsmp.put(12"San");  
  8.   Set s = hsmp.entrySet();  
  9.   Iterator itrtr = s.iterator();  
  10.   while (itrtr.hasNext()) {  
  11.    Map.Entry me = (Map.Entry) itrtr.next();  
  12.    System.out.println(me.getKey() + " " + me.getValue());  
  13.   }  
  14.  }  
  15. }   
Output
 
fig-5.jpg