Java Generic Example – Parameterizd Type Eexample

Hello Freinds

Here I will try to write small program which will enables you to view why generics are there in java.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package genericbasic;

/**
 *
 * @author Vishal
 * In this example, I will show why generic are there
 * first we write tree separate overloaded method to print
 * reference type array.
 * after that we write single generic method which can
 * perform same tast.
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // TODO code application logic here

        Integer[] intarray = {1,2,3,4,5};
        Double[] dblarray = {1.1,1.2,1.3,1.4};
        Character[] charray = {‘a’,’b’,’c’};

        System.out.println( “Array integerArray contains:” );
            printArray( intarray ); // pass an Integer array
        System.out.println( “\nArray doubleArray contains:” );
            printArray( dblarray ); // pass a Double array
        System.out.println( “\nArray characterArray contains:” );
              printArray( charray ); // pass a Character array

        Integer[] intarraygeneric = {1,2,3,4,5};
        Double[] dblarraygeneric = {1.1,1.2,1.3,1.4};
        Character[] charraygeneric = {‘a’,’b’,’c’};

              System.out.println( “Array integerArraygeneric contains:” );
              // How Calling Works
              /*
               *When the compiler encounters line  printArrayGeneric( intarraygeneric );
               , it first determines argument
               intarraygeneric’s type (i.e., Integer[]) and attempts to locate a
               named printArrayGeneric that specifies a single Integer[] parameter.
               There is no such method in this example. Next, the compiler
               determines whether there is a generic method named printArrayGeneric
               that specifies a single array parameter and uses a type parameter
               to represent the array element type. The compiler determines that
               method printArrayGeneric is a match and sets up a call to
               the method. The same process is repeated for the calls to method
               printArrayGeneric for double and character
               */
              
           // pass an Integer array
               printArrayGeneric( intarraygeneric );
        System.out.println( “\nArray doubleArraygeneric contains:” );
            printArrayGeneric( dblarraygeneric ); // pass a Double array
        System.out.println( “\nArray characterArraygeneric contains:” );
              printArrayGeneric( charraygeneric ); // pass a Character array
   
    }
    //Overloaded methods (printarray) are often used to perform similar
    //operations on different types of data.

    // method printArray to print Integer array
     public static void printArray( Integer[] inputArray )
       {
         // display array elements
          for ( Integer element : inputArray )
             System.out.printf( “%s “, element );

          System.out.println();
       } // end method printArray

     //Overloaded methods (printarray) are often used to perform similar
    //operations on different types of data.

// method printArray to print Double array
        public static void printArray( Double[] dblArray )
       {
         // display array elements
          for ( Double element : dblArray )
             System.out.printf( “%s “, element );

          System.out.println();
       } // end method printArray

    //Overloaded methods (printarray) are often used to perform similar
    //operations on different types of data.

// method printArray to print Character array
   public static void printArray( Character[] chArray )
       {
         // display array elements
          for ( Character element : chArray )
             System.out.printf( “%s “, element );

          System.out.println();
       } // end method printArray

   //Generic method

  // Compile will translate following method in below manner
   /*
    * check input type..its now Object – the root class
    1   public static void printArray( Object[] inputArray )
    2   {   
    3    
    4      for ( Object element : inputArray )   
    5      System.out.printf( “%s “, element );    
    6      System.out.println();   
    7   }
    */
   public static < E > void printArrayGeneric( E[] inputArray )
   {
       for ( E element : inputArray )
            System.out.printf( “%s “, element );
       System.out.println();
   } // end method printArray
}

most of the things are self explanatory, please copy and run the program to see output.

Java – J2EE Collection

How to run:

copy whole code and save file as main.java

If you are using Textpad then press ctrl+1 for compilation and ctrl+2 for run.
start coping from below line

import java.util.ArrayList;

import java. util.LinkedList;

import java.util.Iterator;

import java.util.Collection;

import java.util.Collections;

import java.util.HashMap;

import java.util.HashSet;

import java.util.Hashtable;

import java.util.LinkedHashMap;

import java.util.LinkedHashSet;

import java.util.List;

import java.util.ListIterator;

import java.util.PriorityQueue;

import java.util.Queue;

import java.util.TreeMap;

import java.util.TreeSet;

import java.util.Vector;

/**

 *

 * @author Vishal_Shah02

 */

public class Main {

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        // TODO code application logic here

        System.out.println();

        System.out.println(“**********************************************************************”);

        System.out.println(” Array Example”);

        System.out.println(“**********************************************************************”);

        System.out.println();

        //ArrayList Example: Start

        ArrayList exarray = new ArrayList();

        //Add element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“add operation”);

        System.out.println(“——————————“);

        exarray.add(“Item1”);

        exarray.add(“Item2”);

        exarray.add(“Item3”);

        exarray.add(“Item4”);

        exarray.add(“Item5”);

        System.out.println(“Result:->”+ exarray);

        //iterate element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Iterating”);

        System.out.println(“——————————“);

        Iterator itarraylist = exarray.iterator();

        while(itarraylist.hasNext())

        {

            System.out.println(itarraylist.next());

        }

        //remove element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Removing”);

        System.out.println(“——————————“);

        exarray.remove(1);

        System.out.println(“Result:->”+ exarray);

         //copy element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“copy”);

        System.out.println(“——————————“);

         Object[] objarraylist = exarray.toArray();

         for(int i=0; i<objarraylist.length; i++)

         {

            System.out.println(objarraylist[i]);

         }

        // search element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Search”);

        System.out.println(“——————————“);

            if(exarray.contains(“Item1”))

            {

                System.out.println(“Match Found”);

            }

            else

            {

                System.out.println(“Match Not Found”);

            }

        // sort element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Sort”);

        System.out.println(“——————————“);

         Collections.sort(exarray);

         System.out.println(“Result:->”+ exarray);

        // replace element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“replace”);

        System.out.println(“——————————“);

        exarray.set(1, “Repplaced item”);

         System.out.println(“Result:->”+ exarray);

        // sub element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“sublist”);

        System.out.println(“——————————“);

        List sublist = exarray.subList(1, 4);

        System.out.println(sublist);

        // List iterator element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“List iterator”);

        System.out.println(“——————————“);

        ListIterator listitrarray = exarray.listIterator();

        listitrarray.next();

        listitrarray.add(“adding item via list iterator”);

         System.out.println(exarray);

         System.out.println(“Previous Index is : ” + listitrarray.previousIndex());

         System.out.println(“Next Index is : ” + listitrarray.nextIndex());

          while(listitrarray.hasPrevious())

              System.out.println(listitrarray.previous());

        //ArrayList Example: End

   // ***********************************************************************//

         //LinkedList Example: Start

        System.out.println();

        System.out.println(“**********************************************************************”);

        System.out.println(“LinkedList Example”);

        System.out.println(“**********************************************************************”);

        System.out.println();

        LinkedList exlinkedlist = new LinkedList();

        //Add element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“add operation”);

        System.out.println(“——————————“);

        exlinkedlist.add(“LinkedListItem1”);

        exlinkedlist.add(“LinkedListItem2”);

        exlinkedlist.add(“LinkedListItem3”);

        exlinkedlist.add(“LinkedListItem4”);

        exlinkedlist.add(“LinkedListItem5”);

        System.out.println(“Result:->”+ exlinkedlist);

        //iterate element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Iterating”);

        System.out.println(“——————————“);

        Iterator itLinkedlist = exlinkedlist.iterator();

        while(itLinkedlist.hasNext())

        {

            System.out.println(itLinkedlist.next());

        }

        //remove element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Removing”);

        System.out.println(“——————————“);

        exlinkedlist.remove(3);

        System.out.println(“Result:->”+ exlinkedlist);

         //copy element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“copy”);

        System.out.println(“——————————“);

        exlinkedlist.set(2,”Copied Item”);

         System.out.println(“Result:->”+ exlinkedlist);

        // search element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Search”);

        System.out.println(“——————————“);

         if(exlinkedlist.indexOf(“iterm1”)!= 1)

         {

             System.out.println(“Record Found”);

         }

         else

         {

             System.out.println(“Record not Found”);

         }

        // sort element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Sort”);

        System.out.println(“——————————“);

        Collections.sort(exlinkedlist);

        System.out.println(“Result:->”+ exlinkedlist);

        // replace element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“replace”);

        System.out.println(“——————————“);

        exlinkedlist.set(1,”Replaced LinkItem”);

         System.out.println(“Result:->”+ exlinkedlist);

        // sub element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“sublist”);

        System.out.println(“——————————“);

        List subLinkedList = exlinkedlist.subList(0, 2);

        System.out.println(“Result:->”+ subLinkedList);

        // get element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“get element”);

        System.out.println(“——————————“);

        if(exlinkedlist.contains(“Iterm 1”)  )

        {

            System.out.println(“element found”);

        }

        else

        {

            System.out.println(“element not found”);

        }

        //LinkedList Example: End

       //*******************************************************************//

    //Vector Example: Start

        System.out.println();

        System.out.println(“**********************************************************************”);

        System.out.println(“Vector Example”);

        System.out.println(“**********************************************************************”);

        System.out.println();

        Vector exvector = new Vector();

        //Add element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“add operation”);

        System.out.println(“——————————“);

        exvector.add(“Item1”);

        exvector.add(“Item2”);

        exvector.add(“Item3”);

        exvector.add(“Item4”);

        exvector.add(“Item5”);

        System.out.println(“Vector Result:->”+ exvector);

        //iterate element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Iterating”);

        System.out.println(“——————————“);

        Iterator itVector = exvector.iterator();

        while(itVector.hasNext())

        {

            System.out.println(itVector.next());

        }

        //remove element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Removing”);

        System.out.println(“——————————“);

        exvector.remove(1);

        System.out.println(“Result:->”+ exvector);

         //copy element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“copy”);

        System.out.println(“——————————“);

         Object[] objVector = exvector.toArray();

         for(int i=0; i<objVector.length; i++)

         {

            System.out.println(objVector[i]);

         }

        // search element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Search”);

        System.out.println(“——————————“);

            if(exvector.contains(“Item1”))

            {

                System.out.println(“Match Found”);

            }

            else

            {

                System.out.println(“Match Not Found”);

            }

        // sort element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Sort”);

        System.out.println(“——————————“);

         Collections.sort(exvector);

         System.out.println(“Result:->”+ exvector);

        // replace element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“replace”);

        System.out.println(“——————————“);

        exvector.set(1, “Repplaced item”);

         System.out.println(“Result:->”+ exvector);

        // sub element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“sublist”);

        System.out.println(“——————————“);

        List sublistvector = exvector.subList(1, 4);

        System.out.println(sublistvector);

        // List iterator element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“List iterator”);

        System.out.println(“——————————“);

        ListIterator listitrarrayvector = exvector.listIterator();

        listitrarrayvector.next();

        listitrarrayvector.add(“adding item via list iterator”);

         System.out.println(exvector);

         System.out.println(“Previous Index is : ” + listitrarrayvector.previousIndex());

         System.out.println(“Next Index is : ” + listitrarrayvector.nextIndex());

          while(listitrarray.hasPrevious())

              System.out.println(listitrarray.previous());

        //Vector Example: End

        System.out.println();

        System.out.println(“**********************************************************************”);

        System.out.println(” HashSet Example”);

        System.out.println(“**********************************************************************”);

        System.out.println();

        //HashSet Example: Start

        HashSet exHashSet = new HashSet();

        //Add element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“add operation”);

        System.out.println(“——————————“);

        exHashSet.add(“HashSetItem1”);

        exHashSet.add(“HashSetItem2”);

        exHashSet.add(“HashSetItem3”);

        exHashSet.add(“HashSetItem5”);

        exHashSet.add(“HashSetItem6”);

        System.out.println(“Result:->”+ exHashSet);

        //iterate element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Iterating”);

        System.out.println(“——————————“);

        Iterator itHashSet = exHashSet.iterator();

        while(itHashSet.hasNext())

        {

            System.out.println(itHashSet.next());

        }

        //remove element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Removing”);

        System.out.println(“——————————“);

        if(exHashSet.remove(“HashSetItem1”)            )

        {

                System.out.println(“item removed”);

        }

        else

        {

                System.out.println(“item not removed”);

        }

        System.out.println(“Result:->”+ exHashSet);

         //copy element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“copy”);

        System.out.println(“——————————“);

        Object[] objhashset = exHashSet.toArray();

        for(int index=0; index < objhashset.length ; index++)

            System.out.println(objhashset[index]);

        // search element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Search”);

        System.out.println(“——————————“);

        if(exHashSet.contains(“HashSetItem2”))

        {

         System.out.println(“item found”);

        }

         else

        {

         System.out.println(“item does not found”);

        }

        // sub element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Cloning”);

        System.out.println(“——————————“);

        Object copyhashset = exHashSet.clone();

        System.out.println(copyhashset);

        //HashSet Example: End

    //*******************************************************************//

        System.out.println();

        System.out.println(“**********************************************************************”);

        System.out.println(“TreeSet Example”);

        System.out.println(“**********************************************************************”);

        System.out.println();        //TreeSet Example: Start

        TreeSet exTreeSet = new TreeSet();

        //Add element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“add operation”);

        System.out.println(“——————————“);

        exTreeSet.add(“TreeSetItem1”);

        exTreeSet.add(“TreeSetItem2”);

        exTreeSet.add(“TreeSetItem3”);

        exTreeSet.add(“TreeSetItem5”);

        exTreeSet.add(“TreeSetItem6”);

        System.out.println(“Result:->”+ exTreeSet);

        //iterate element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Iterating”);

        System.out.println(“——————————“);

        Iterator itTreeSet = exTreeSet.iterator();

        while(itTreeSet.hasNext())

        {

            System.out.println(itTreeSet.next());

        }

        //remove element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Removing”);

        System.out.println(“——————————“);

        if(exTreeSet.remove(“TreeSetItem1”)            )

        {

                System.out.println(“item removed”);

        }

        else

        {

                System.out.println(“item not removed”);

        }

        System.out.println(“Result:->”+ exTreeSet);

         //copy element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“copy”);

        System.out.println(“——————————“);

        Object[] objTreeSet = exTreeSet.toArray();

        for(int index=0; index < objTreeSet.length ; index++)

            System.out.println(objTreeSet[index]);

        // search element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Search”);

        System.out.println(“——————————“);

        if(exTreeSet.contains(“TreeSetItem2”))

        {

         System.out.println(“item found”);

        }

         else

        {

         System.out.println(“item does not found”);

        }

        // sub element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Cloning”);

        System.out.println(“——————————“);

        Object copyTreeSet = exTreeSet.clone();

        System.out.println(copyTreeSet);

        //TreeSet Example: End

       //*******************************************************************//

         System.out.println();

        System.out.println(“**********************************************************************”);

        System.out.println(” LinkedHashSet Example”);

        System.out.println(“**********************************************************************”);

        System.out.println();

        //LinkedHashSet Example: Start

        LinkedHashSet exLinkedHashSet = new LinkedHashSet();

        //Add element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“add operation”);

        System.out.println(“——————————“);

        exLinkedHashSet.add(“LinkedHashSetItem1”);

        exLinkedHashSet.add(“LinkedHashSetItem2”);

        exLinkedHashSet.add(“LinkedHashSetItem3”);

        exLinkedHashSet.add(“LinkedHashSetItem5”);

        exLinkedHashSet.add(“LinkedHashSetItem6”);

        System.out.println(“Result:->”+ exLinkedHashSet);

        //iterate element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Iterating”);

        System.out.println(“——————————“);

        Iterator itLinkedHashSet = exLinkedHashSet.iterator();

        while(itLinkedHashSet.hasNext())

        {

            System.out.println(itLinkedHashSet.next());

        }

        //remove element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Removing”);

        System.out.println(“——————————“);

        if(exLinkedHashSet.remove(“LinkedHashSetItem1”)            )

        {

                System.out.println(“item removed”);

        }

        else

        {

                System.out.println(“item not removed”);

        }

        System.out.println(“Result:->”+ exLinkedHashSet);

         //copy element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“copy”);

        System.out.println(“——————————“);

        Object[] objLinkedHashSet = exLinkedHashSet.toArray();

        for(int index=0; index < objLinkedHashSet.length ; index++)

            System.out.println(objLinkedHashSet[index]);

        // search element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Search”);

        System.out.println(“——————————“);

        if(exLinkedHashSet.contains(“LinkedHashSetItem2”))

        {

         System.out.println(“item found”);

        }

         else

        {

         System.out.println(“item does not found”);

        }

        // sub element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Cloning”);

        System.out.println(“——————————“);

        Object copyLinkedHashSet = exLinkedHashSet.clone();

        System.out.println(copyLinkedHashSet);

        //LinkedHashSet Example: End

       //*******************************************************************//

         System.out.println();

        System.out.println(“**********************************************************************”);

        System.out.println(” HashMap Example”);

        System.out.println(“**********************************************************************”);

        System.out.println();      //HashMap Example: Start

        HashMap exHashMap = new HashMap();

        //Add element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“add operation”);

        System.out.println(“——————————“);

        exHashMap.put(“key1″,”HashMapItem1”);

        exHashMap.put(“key2″,”HashMapItem2”);

        exHashMap.put(“key3″,”HashMapItem3”);

        exHashMap.put(“key4″,”HashMapItem5”);

        exHashMap.put(“key5″,”HashMapItem6”);

        System.out.println(“Result:->”+ exHashMap);

        //retrive element

            //retrieve value using Object get(Object key) method of Java HashMap class

        Object obj = exHashMap.get(“HashMapItem6”);

        System.out.println(“Retrival:->”+obj);

        //remove element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Removing”);

        System.out.println(“——————————“);

        exHashMap.remove(“key4”);

        System.out.println(“Result:->”+ exHashMap);

         //Iterate element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Iterate-values”);

        System.out.println(“——————————“);

        Collection clnhashvalue = exHashMap.values();

        Iterator itrhashmap = clnhashvalue.iterator();

        while(itrhashmap.hasNext())

        {

            System.out.println(itrhashmap.next());

        }

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Iterate-keys”);

        System.out.println(“——————————“);

        Collection clnhashkey = exHashMap.keySet();

        Iterator itrhashmapkey = clnhashkey.iterator();

        while(itrhashmapkey.hasNext())

        {

            System.out.println(itrhashmapkey.next());

        }

        // search element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Search-Value”);

        System.out.println(“——————————“);

        if(exHashMap.containsValue(“HashMapItem1”))

        {

            System.out.println(“Hashmap Value found”);

        }

        else

        {

            System.out.println(“Hashmap Value Not found”);

        }

         System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Search-Key”);

        System.out.println(“——————————“);

        if(exHashMap.containsKey(“key3”))

        {

            System.out.println(“Hashmap Key found”);

        }

        else

        {

            System.out.println(“Hashmap Key Not found”);

        }

        // sub element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Cloning”);

        System.out.println(“——————————“);

        Object copyHashMap = exHashMap.clone();

        System.out.println(copyHashMap);

        //HashMap Example: End

          //*******************************************************************//

        System.out.println();

        System.out.println(“**********************************************************************”);

        System.out.println(” LinkedHashMap Example”);

        System.out.println(“**********************************************************************”);

        System.out.println();

        //LinkedHashMap Example: Start

        LinkedHashMap exLinkedHashMap = new LinkedHashMap();

        //Add element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“add operation”);

        System.out.println(“——————————“);

        exLinkedHashMap.put(“key1″,”LinkedHashMapItem1”);

        exLinkedHashMap.put(“key2″,”LinkedHashMapItem2”);

        exLinkedHashMap.put(“key3″,”LinkedHashMapItem3”);

        exLinkedHashMap.put(“key4″,”LinkedHashMapItem5”);

        exLinkedHashMap.put(“key5″,”LinkedHashMapItem6”);

        System.out.println(“Result:->”+ exLinkedHashMap);

        //retrive element

            //retrieve value using Object get(Object key) method of Java LinkedHashMap class

        Object objLinkedHashmap = exLinkedHashMap.get(“LinkedHashMapItem6”);

        System.out.println(“Retrival:->”+objLinkedHashmap);

        //remove element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Removing”);

        System.out.println(“——————————“);

        exLinkedHashMap.remove(“key4”);

        System.out.println(“Result:->”+ exLinkedHashMap);

         //Iterate element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Iterate-values”);

        System.out.println(“——————————“);

        Collection clnlinkhashvalue = exLinkedHashMap.values();

        Iterator itrLinkedHashMap = clnlinkhashvalue.iterator();

        while(itrLinkedHashMap.hasNext())

        {

            System.out.println(itrLinkedHashMap.next());

        }

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Iterate-keys”);

        System.out.println(“——————————“);

        Collection clnlinkhashkey = exLinkedHashMap.keySet();

        Iterator itrLinkedHashMapkey = clnlinkhashkey.iterator();

        while(itrLinkedHashMapkey.hasNext())

        {

            System.out.println(itrLinkedHashMapkey.next());

        }

        // search element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Search-Value”);

        System.out.println(“——————————“);

        if(exLinkedHashMap.containsValue(“LinkedHashMapItem1”))

        {

            System.out.println(“LinkedHashMap Value found”);

        }

        else

        {

            System.out.println(“LinkedHashMap Value Not found”);

        }

         System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Search-Key”);

        System.out.println(“——————————“);

        if(exLinkedHashMap.containsKey(“key3”))

        {

            System.out.println(“LinkedHashMap Key found”);

        }

        else

        {

            System.out.println(“LinkedHashMap Key Not found”);

        }

        // sub element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Cloning”);

        System.out.println(“——————————“);

        Object copyLinkedHashMap = exLinkedHashMap.clone();

        System.out.println(copyLinkedHashMap);

        //LinkedHashMap Example: End

          //*******************************************************************//

   System.out.println();

        System.out.println(“**********************************************************************”);

        System.out.println(” HashTable Example”);

        System.out.println(“**********************************************************************”);

        System.out.println();

             //HashTable Example: Start

        Hashtable exHashTable = new Hashtable();

        //Add element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“add operation”);

        System.out.println(“——————————“);

        exHashTable.put(“key1″,”HashTableItem1”);

        exHashTable.put(“key2″,”HashTableItem2”);

        exHashTable.put(“key3″,”HashTableItem3”);

        exHashTable.put(“key4″,”HashTableItem5”);

        exHashTable.put(“key5″,”HashTableItem6”);

        System.out.println(“Result:->”+ exHashTable);

        //retrive element

            //retrieve value using Object get(Object key) method of Java HashTable class

        Object objHashTable = exHashTable.get(“HashTableItem6”);

        System.out.println(“Retrival:->”+objHashTable);

        //remove element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Removing”);

        System.out.println(“——————————“);

        exHashTable.remove(“key4”);

        System.out.println(“Result:->”+ exHashTable);

         //Iterate element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Iterate-values”);

        System.out.println(“——————————“);

        Collection clnhashtablevalue = exHashTable.values();

        Iterator itrHashTable = clnhashtablevalue.iterator();

        while(itrHashTable.hasNext())

        {

            System.out.println(itrHashTable.next());

        }

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Iterate-keys”);

        System.out.println(“——————————“);

        Collection clnhashtablekey = exHashTable.keySet();

        Iterator itrHashTablekey = clnhashtablekey.iterator();

        while(itrHashTablekey.hasNext())

        {

            System.out.println(itrHashTablekey.next());

        }

        // search element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Search-Value”);

        System.out.println(“——————————“);

        if(exHashTable.containsValue(“HashTableItem1”))

        {

            System.out.println(“HashTable Value found”);

        }

        else

        {

            System.out.println(“HashTable Value Not found”);

        }

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Search-Key”);

        System.out.println(“——————————“);

        if(exHashTable.containsKey(“key3”))

        {

            System.out.println(“HashTable Key found”);

        }

        else

        {

            System.out.println(“HashTable Key Not found”);

        }

        // sub element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Cloning”);

        System.out.println(“——————————“);

        Object copyHashTable = exHashTable.clone();

        System.out.println(copyHashTable);

        //HashTable Example: End

          //*******************************************************************//

        System.out.println();

        System.out.println(“**********************************************************************”);

        System.out.println(” TreeMap Example”);

        System.out.println(“**********************************************************************”);

        System.out.println();

            //Treemap Example: Start

        TreeMap exTreemap = new TreeMap();

        //Add element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“add operation”);

        System.out.println(“——————————“);

        exTreemap.put(“key1″,”TreemapItem1”);

        exTreemap.put(“key2″,”TreemapItem2”);

        exTreemap.put(“key3″,”TreemapItem3”);

        exTreemap.put(“key4″,”TreemapItem5”);

        exTreemap.put(“key5″,”TreemapItem6”);

        System.out.println(“Result:->”+ exTreemap);

        //retrive element

            //retrieve value using Object get(Object key) method of Java Treemap class

        Object objTreemap = exTreemap.get(“TreemapItem6”);

        System.out.println(“Retrival:->”+objTreemap);

        //remove element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Removing”);

        System.out.println(“——————————“);

        exTreemap.remove(“key4”);

        System.out.println(“Result:->”+ exTreemap);

         //Iterate element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Iterate-values”);

        System.out.println(“——————————“);

        Collection clntreemapvalue = exTreemap.values();

        Iterator itrTreemap = clntreemapvalue.iterator();

        while(itrTreemap.hasNext())

        {

            System.out.println(itrTreemap.next());

        }

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Iterate-keys”);

        System.out.println(“——————————“);

        Collection clntreemapkey = exTreemap.keySet();

        Iterator itrTreemapkey = clntreemapkey.iterator();

        while(itrTreemapkey.hasNext())

        {

            System.out.println(itrTreemapkey.next());

        }

        // search element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Search-Value”);

        System.out.println(“——————————“);

        if(exTreemap.containsValue(“TreemapItem1”))

        {

            System.out.println(“Treemap Value found”);

        }

        else

        {

            System.out.println(“Treemap Value Not found”);

        }

         System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Search-Key”);

        System.out.println(“——————————“);

        if(exTreemap.containsKey(“key3”))

        {

            System.out.println(“Treemap Key found”);

        }

        else

        {

            System.out.println(“Treemap Key Not found”);

        }

        // sub element

        System.out.println(“”);

        System.out.println(“——————————“);

        System.out.println(“Cloning”);

        System.out.println(“——————————“);

        Object copyTreemap = exTreemap.clone();

        System.out.println(copyTreemap);

        //Treemap Example: End

        System.out.println();

        System.out.println(“**********************************************************************”);

        System.out.println(” Queue Example”);

        System.out.println(“**********************************************************************”);

        System.out.println();

        //Queue Example: Start

        Queue<String> exqueue = new LinkedList<String>();

        exqueue.add(“Shah”);

        exqueue.add(“Vishal”);

        exqueue.add(“Infy”);

        Iterator it = exqueue.iterator();

        while(it.hasNext())

        {

            System.out.println(it.next());

        }

        System.out.println(“Peek Example/ null and no exception:->”+ exqueue.peek());

        System.out.println(exqueue);

        System.out.println(“Poll Example/ remove first element:->”+ exqueue.poll());

        System.out.println(exqueue);

        System.out.println(“Element Example/ throw exception:->”+ exqueue.element());

        int[] iarray = {1,2,3,4,5,6};

        PriorityQueue<Integer> pq1 = new PriorityQueue<Integer>();

        for(int x:iarray)

        {

            pq1.offer(x);

        }

        System.out.println(pq1);

        for(int x:iarray)

        {

            pq1.poll();

        }

        System.out.println(pq1);

        //Queye Example: end

    }

}