28

This is not a question about the differences between Hashtable and HashMap. I understand that a Hashtable object cannot accept null values for either key or value entries, that it is synchronized collection, and that it uses slightly less memory than a HashMap.

I'm wondering about the scenarios where it would be more appropriate to use a Hashtable instead of a HashMap.

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
  • 2
    duplicate of http://stackoverflow.com/questions/40471/differences-between-hashmap-and-hashtable – Matt Wolfe Mar 02 '12 at 15:43
  • @MattWolfe It's not a duplicate of that question. That question goes over the differences. I know the differences. I'm asking about when it is appropriate to use one over the other. – Vivin Paliath Mar 02 '12 at 15:47

5 Answers5

32

This is not a question about the differences between Hashtable and HashMap

Well it is really...

I'm wondering about the scenarios where it would be more appropriate to use a Hashtable instead of a HashMap.

Precisely when you want the differences between the two:

  • When you want to run on Java 1.1
  • When you want each operation to be synchronized (getting you a form of thread safety, so long as you never iterate over it) - and for some reason don't want to use Collections.synchronizedMap over a HashMap
  • When you don't want to be able to store null values
  • When the memory difference is actually significant (only after you've proved this is the case) - I wasn't even aware of this difference, personally...
  • When you're forced to by a nasty API which returns or takes Hashtable (relatively rare, fortunately)

I can't remember the last time I was in that situation, personally - I would say it's vanishingly rare to be appropriate to use Hashtable in modern Java code.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Just to add from java 1.6 onwards you will get HashTable as obsolete collection. – kosa Mar 02 '12 at 15:45
  • 2
    @thinksteep: Really? I don't see any deprecation notices on the 1.6 docs: http://docs.oracle.com/javase/6/docs/api/java/util/Hashtable.html – Jon Skeet Mar 02 '12 at 15:47
  • @JonSkeet I only put that bit about memory just to cover all the bases - in practice the memory difference is quite small as to be negligible. I was wondering if there were any strange corner cases where a `Hashtable` would be more useful than a `HashMap`. Thanks for enumerating all the different scenarios! – Vivin Paliath Mar 02 '12 at 15:51
  • It seems my netbeans misleading. – kosa Mar 02 '12 at 15:52
  • @VivinPaliath: Have added a generalization of skaffman's answer as well. – Jon Skeet Mar 02 '12 at 15:55
  • +1: Hashtable grows slower so it can have slightly less unused capacity, but at the cost of CPU (as it may need to grow more times) HashMap has to double each time as it has to be a size of a power of 2. – Peter Lawrey Mar 02 '12 at 16:06
  • @VivinPaliath You need to use a `Hashtable` when you have a API which requires it, in every other case you can use `HashMap` or `Collections.synchronizedMap(new HashMap())` – Peter Lawrey Mar 02 '12 at 16:08
  • I'm not sure I'd agree with point 2 (When you want each operation to be synchronized). In that case I'd still use HashMap (or ConcurrentHashMap) - I would only use Hashtable if I had no choice. – Paul Cager Mar 02 '12 at 16:58
19

Never.

Hashtable was the original implementation of a map in Java 1. It's been overtaken by the Map<K,V> implementations defined in the Java Collections Framework. Sure, Hashtable has been retrofitted to implement Map but that's not terribly useful.

It has the main problem in that it's synchronized. This means that it will be slow in any circumstance where it is shared between threads. ConcurrentHashMap is a better choice in that situation. If you are running on a single thread then the un-synchronized HashMap is a better choice.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Sarge
  • 2,367
  • 2
  • 23
  • 36
8

With InitialContext in JNDI

I can think of only one valid reason - when you're using an API that requires it, such as JNDI’s hugely irritating InitialContext class.

Other than that, I can see no good reason to use Hashtable at all. You can get a synchronized version of HashMap by using Collections.synchronizedMap, or use a ConcurrentMap implementation such as ConcurrentHashMap or ConcurrentSkipListMap

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
skaffman
  • 398,947
  • 96
  • 818
  • 769
3

I only see Hashtables in legacy apps/ libraries.

If you can, use ConcurrentHashMap or Collections.synchronizedMap if you need a synchronized Map.

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentHashMap.html

http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#synchronizedMap%28java.util.Map%29

Puce
  • 37,247
  • 13
  • 80
  • 152
1

Knowing when to use one class or structure over another is essentially understanding the differences between the two, and deciding which one best suits the problem at hand based on those differences.

I understand that a Hashtable object cannot accept null values

So in the situation where you need to store null values, a Hashtable would not be appropriate.

Also, in a Hashtable, enumeration is not fail-safe. So if you need to be able to change the content of the structure while enumerating, a Hashtable would be more appropriate.

Michael Hornfeck
  • 1,242
  • 1
  • 16
  • 33