views:

122

answers:

4

What is Hashtables used for in Java? Further more please give examples of simple usage of Hashtables.

+2  A: 

If you need to have a synchronized key value pairs, you will use hashtable. If you wonder why you need hashtable when there is hashmap, there are lot of resources available, check here

Teja Kantamneni
A: 

Hash tables, in pretty much any language (not just Java), are used for mapping keys to values, with very good normal-case performance characteristics.

Anon.
+2  A: 
Map<Integer,String> map = new HashMap<Integer,String>();
map.put(1, "One");
map.put(2, "Two");
map.put(10, "Ten");
...

int n = ... // some number
String s = map.get(n);
if(s == null)
   System.out.println("No entry was found for " + n);
else
   System.out.println(n + " is " + s);

Note that there is standard Java class called Hashtable, however it recommended not to use it. Best practice is to create instance of HashMap (which is a standard implementation of a hash-table) and use it through its interface Map.

Itay
A: 

It basically is used for like in any other language I know: To map keys to values.

You may e.g. think of a dictionary which maps keywords to definitions.

Helper Method