How do I create a hash table in Java?

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

    It is important to note that Java's hash function is less than optimal. If you want less collisions and almost complete elimination of re-hashing at ~50% capacity, I'd use a Buz Hash algorithm Buz Hash

    The reason Java's hashing algorithm is weak is most evident in how it hashes Strings.

    "a".hash() give you the ASCII representation of "a" - 97, so "b" would be 98. The whole point of hashing is to assign an arbitrary and "as random as possible" number.

    If you need a quick and dirty hash table, by all means, use java.util. If you are looking for something robust that is more scalable, I'd look into implementing your own.

提交回复
热议问题