Thursday, 10 December 2015

How to Copy Map content to another Hashtable ?

package com.JavaInterview.Programs;

import java.util.Hashtable;
import java.util.HashMap;

/**
 * Copy Map content to another Hashtable
 * 
 * 
 */
public class CopyMapToHashTable {

 public static void main(String args[]) {
  // Create Hashtable Instance
  Hashtable<Integer, String> JipHT = new Hashtable<Integer, String>();
  // Adding element to Hashtable
  JipHT.put(1, "first");
  JipHT.put(2, "second");
  JipHT.put(3, "third");

  // Print the Hashtable
  System.out.println("Original Hashtable :" + JipHT);

  // Create HashMap
  HashMap<Integer, String> JipHM = new HashMap<Integer, String>();
  JipHM.put(5, "five");
  JipHM.put(6, "six");

  // Copying Map to Hashtable
  JipHT.putAll(JipHM);

  // Print the Hashtable
  System.out.println("Hashtable after copying :" + JipHT);

 }
}

Output:
Original Hashtable :{3=third, 2=second, 1=first}
Hashtable after copying :{6=six, 5=five, 3=third, 2=second, 1=first}


No comments:

Post a Comment

Difference between final, finally and finalize()?

final :Final is a keyword. Final is used to apply restrictions on class, method and variable. Final class can't be inherited, final ...