share

Sunday, 14 June 2015

How to create shallow copy of Hashtable

import java.util.Hashtable;

public class HashTableShallowCopy {
    public static void main(String[] args) {
       
        Hashtable<Integer,String> ht = new Hashtable<Integer,String>();
       
        /*
         * Maps the specified key to the specified
         * value in this hashtable
         */
        ht.put(1, "java");
        ht.put(2, "jersey");
        ht.put(3, "spring");
        ht.put(4, "c");
        ht.put(5, "php");
       
        //print hashtable
        System.out.println(" Hash Table is ="+ht);
       
      //Creates a shallow copy of this hashtable.
      Hashtable<Integer,String> myHt = (Hashtable<Integer, String>) ht.clone();
   
      System.out.println(" shallow copy ="+myHt);
     
    }   

}


Output:

 Hash Table is ={5=php, 4=c, 3=spring, 2=jersey, 1=java}

 shallow copy ={5=php, 4=c, 3=spring, 2=jersey, 1=java}

No comments:

Post a Comment