How do I create a hash table in Java?

后端 未结 8 851
没有蜡笔的小新
没有蜡笔的小新 2021-02-04 12:41

What is the most straightforward way to create a hash table (or associative array...) in Java? My google-fu has turned up a couple examples, but is there a standard way to do t

8条回答
  •  星月不相逢
    2021-02-04 13:34

    Also don't forget that both Map and Hashtable are generic in Java 5 and up (as in any other class in the Collections framework).

    Map numbers = new HashMap();
    numbers.put("one", 1);
    numbers.put("two", 2);
    numbers.put("three", 3);
    
    Integer one = numbers.get("one");
    Assert.assertEquals(1, one);
    

提交回复
热议问题