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 this?
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?
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).
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 be98
. 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.What Edmund said.
As for not calling .add all the time, no, not idiomatically. There would be various hacks (storing it in an array and then looping) that you could do if you really wanted to, but I wouldn't recommend it.
You can use double-braces to set up the data. You still call add, or put, but it's less ugly:
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:
I don't know if this is what you're looking for, but there are situations where this can be useful.
Both classes can be found from the java.util package. The difference between the 2 is explained in the following jGuru FAQ entry.