2:14 am - Tuesday April 23, 2019

java program to Create Hashtable from HashMap example

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

import java.util.Enumeration;

import java.util.Hashtable;

import java.util.HashMap;

class CreateHashtableFromHashMap {

public static void main(String[] args) {

// creating HashMap Object

HashMap hMap = new HashMap();

hMap.put("1", "One");

hMap.put("2", "Two");

hMap.put("3", "Three");

// creating Hashtable Object

Hashtable ht = new Hashtable();

// to insert an element to the respective index into the table

ht.put("1", "This value would be REPLACED !!");

ht.put("4", "Four");

System.out.println("Hashtable contents before copy");

// the hashtable elements are called sequentially

Enumeration e = ht.elements();

// the while condition is used to check that the table has contains any
// more elements and get the next element

while (e.hasMoreElements())

System.out.println(e.nextElement());

// putAll() method is used to insert all the element of Map to table

ht.putAll(hMap);

System.out.println("Hashtable contents after copy");

e = ht.elements();

while (e.hasMoreElements())

System.out.println(e.nextElement());

}

}

Output:

D:\CandidJava\Util\HashMap>javac CreateHashtableFromHashMap.java

Note: CreateHashtableFromHashMap.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.

D:\CandidJava\Util\HashMap>java CreateHashtableFromHashMap

Hashtable contents before copy

Four                                                          

This value would be REPLACED !!

Hashtable contents after copy

Four

Three

Two

One

Filed in: Z-A All Java Codes

No comments yet.

Leave a Reply