Java-如何从HashMap创建Java哈希表

Java-如何从HashMap创建Java哈希表,java,collections,hashmap,hashtable,Java,Collections,Hashmap,Hashtable,我想从HashMap创建Java哈希表 HashMap hMap = new HashMap(); //populate HashMap hMap.put("1","One"); hMap.put("2","Two"); hMap.put("3","Three"); //create new Hashtable Hashtable ht = new Hashtable(); //populate Hashtable ht.put("1","This value would b

我想从HashMap创建Java哈希表

 HashMap hMap = new HashMap();       
//populate HashMap
hMap.put("1","One");
hMap.put("2","Two");
hMap.put("3","Three");

//create new Hashtable
Hashtable ht = new Hashtable();

//populate Hashtable
ht.put("1","This value would be REPLACED !!");
ht.put("4","Four");

在此之后,最简单的步骤是什么?

使用接受
映射的
哈希表
构造函数:

public Hashtable(Map<? extends K, ? extends V> t) 

哇!!我做得很成功。。这是:)


Hashtable ht=新的Hashtable(hMap)
ht.putAll(hMap)。使用。此外,你应该使用泛型,而不是使用。是的,但我必须遵守要求@AndyTurner Thank friendOK@Jesper获得了它Thank FriendThank为什么要使用
哈希表
?哈希表,原始类型?你能解释一下你到底想实现什么吗?@Minati Das:
枚举
哈希表
一样过时,但是如果你真的需要它,可以使用,例如,代替
哈希表.keys()
,你可以使用
集合.枚举(map.keySet())
,而不是
哈希表.elements()
,您可以使用
Collections.enumeration(map.values())
。但通常情况下,您会对每个循环使用
迭代器
或仅使用
for(字符串键:map.keySet())…
for(字符串值:map.values())…
Map<String,String> hMap = new HashMap<>();       
//populate HashMap
hMap.put("1","One");
hMap.put("2","Two");
hMap.put("3","Three");

//create new Hashtable
Map<String,String> ht = new Hashtable<>(hMap);
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.HashMap;

public class CreateHashtableFromHashMap {

  public static void main(String[] args) {

    //create HashMap
    HashMap hMap = new HashMap();

    //populate HashMap
    hMap.put("1","One");
    hMap.put("2","Two");
    hMap.put("3","Three");

    //create new Hashtable
    Hashtable ht = new Hashtable();

    //populate Hashtable
    ht.put("1","This value would be REPLACED !!");
    ht.put("4","Four");

    //print values of Hashtable before copy from HashMap
    System.out.println("hastable contents displaying before copy");
    Enumeration e = ht.elements();
    while(e.hasMoreElements())
    System.out.println(e.nextElement());

    ht.putAll(hMap);

    //Display contents of Hashtable
    System.out.println("hashtable contents displaying after copy");
    e = ht.elements();
    while(e.hasMoreElements())
    System.out.println(e.nextElement());

  }
}