How do I create a hash table in Java?

后端 未结 8 784
没有蜡笔的小新
没有蜡笔的小新 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:22

    You can use double-braces to set up the data. You still call add, or put, but it's less ugly:

    private static final Hashtable MYHASH = new Hashtable() {{
        put("foo",      1);
        put("bar",      256);
        put("data",     3);
        put("moredata", 27);
        put("hello",    32);
        put("world",    65536);
     }};
    

提交回复
热议问题