14

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?

nbro
  • 15,395
  • 32
  • 113
  • 196
akdom
  • 32,264
  • 27
  • 73
  • 79

8 Answers8

28
Map map = new HashMap();
Hashtable ht = new Hashtable();

Both classes can be found from the java.util package. The difference between the 2 is explained in the following jGuru FAQ entry.

Edmund Tay
  • 1,257
  • 1
  • 11
  • 10
  • From the jGuru entry: "The key difference between the two is that access to the Hashtable is synchronized on the table while access to the HashMap isn't. You can add it, but it isn't there by default. Another difference is that iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn't. If you change the map while iterating, you'll know." – ErikAGriffin Oct 26 '15 at 10:13
23

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<String,Integer> MYHASH = new Hashtable<String,Integer>() {{
    put("foo",      1);
    put("bar",      256);
    put("data",     3);
    put("moredata", 27);
    put("hello",    32);
    put("world",    65536);
 }};
izb
  • 50,101
  • 39
  • 117
  • 168
7

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<String, Integer> numbers = new HashMap<String, Integer>();
numbers.put("one", 1);
numbers.put("two", 2);
numbers.put("three", 3);

Integer one = numbers.get("one");
Assert.assertEquals(1, one);
Cem Catikkas
  • 7,171
  • 4
  • 29
  • 33
2
import java.util.HashMap;

Map map = new HashMap();
nbro
  • 15,395
  • 32
  • 113
  • 196
John
  • 14,944
  • 12
  • 57
  • 57
1

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.

Community
  • 1
  • 1
SCdF
  • 57,260
  • 24
  • 77
  • 113
0

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.

huzeyfe
  • 3,554
  • 6
  • 39
  • 49
hedrick
  • 211
  • 4
  • 13
0

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.

Mocky
  • 7,768
  • 5
  • 28
  • 23
-1
Hashtable<Object, Double> hashTable = new Hashtable<>();

put values ...

get max

Optional<Double> optionalMax = hashTable.values().stream().max(Comparator.naturalOrder());

if (optionalMax.isPresent())
 System.out.println(optionalMax.get());
Sanip
  • 1,772
  • 1
  • 14
  • 29
  • Hi, could you add explanation to your code ? You can edit your question to do so and also use code formatting. It seems like a bother but it would actually add a lot of value. – Matthias Beaupère May 03 '19 at 09:26