tags:

views:

2820

answers:

8

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?

+2  A: 

import java.util.*;

Map map = new HashMap();

John
+9  A: 
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  A: 

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.

SCdF
+5  A: 

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
A: 

I downvoted this because I believe that it's a fake post. How can your google-fu fail you when searching for the question proposed yields this as the first result:

Java Tips - How to create a Hash Table

Outlaw Programmer
+1  A: 

@Outlaw Programmer

My google-fu failed me apparently because what I was wanting to do is not technically possible (I did in fact mention I found examples... read the question.).

I'm primarily used to dynamic languages which will let you recast a list of pairs to a dictionary (python) or just straight up let you define a hash (python, perl, php, etc....). The goal was to see what people actually did, and if there were an idiom to fill out the table on construction instead of having to add every single item. I realize that I can loop through... but I was hoping to just create one object, not three....

And isn't the whole point of this site to ask programming questions? This may be a simple one, but that doesn't make it any less legitimate.

akdom
+1  A: 

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
+6  A: 

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