How do I create a hash table in Java?

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

    And is there a way to populate the table with a list of key->value pairs without individually calling an add method on the object for each pair?

    One problem with your question is that you don't mention what what form your data is in to begin with. If your list of pairs happened to be a list of Map.Entry objects it would be pretty easy.

    Just to throw this out, there is a (much maligned) class named java.util.Properties that is an extension of Hashtable. It expects only String keys and values and lets you load and store the data using files or streams. The format of the file it reads and writes is as follows:

    key1=value1
    key2=value2
    

    I don't know if this is what you're looking for, but there are situations where this can be useful.

提交回复
热议问题