views:

166

answers:

6

I've used hash tables occasionally in a couple of languages, but I just came across the Java map while browsing through some code. I checked up the differences in this SO question, which expressed it very clearly. My question is this:

  • When either one could work for you , which is better to use? Which one should you choose if you're not dealing with nulls/threads...?

  • Which one is used more often / is more standard?

+1  A: 

HashMap because it runs faster.

cherouvim
+5  A: 
  • If either would work for you, I would use a HashMap as there would be overhead in the synchronization that Hashtable provides
  • I would imagine that HashMap is more standard, and is used more often. The synchronized Hashtable has to a certain extent been superseded by advances in Collections and concurrency frameworks.
akf
A: 

HashMap for local variables or method parameters because they are thread safe.

fastcodejava
Method parameters should be using `Map`, not `HashMap`.
Chris Jester-Young
One cannot pass an instance of `Map` to a method.
fastcodejava
One can, and should pass an implementation of Map to a method, without the method specifying what that implementation is. This would be the standard case, of course there may be exceptions. Design by Contract.
Robin
Oh, and as a method parameter, it is not threadsafe, unless it is not accessed by any other thread outside of the method. Hashtable is threadsafe.
Robin
@Robin - It is not possible to create an instance of `Map`. It is an interface. I agree the method declaration should use `Map`.
fastcodejava
I think it is pretty obvious we understand that already. I was simply pointing out that this fact is irrelevant to the comment made by @Chris Jester-Young. You stated it as if it was refuting what he had said.
Robin
Nitpick @fastcodejava: an instance of HashMap is also an instance of Map (the same goes for Hashtable). Otherwise you wouldn't be able to pass a HashMap as a Map.More serious: Even local variables and method parameters aren't always accessed in single-threaded fashion. So they are not per se thread safe.
Hardcoded
+1  A: 

For general use choose HashMap because Hashtable is synchronized, and therefore consumes more computing resources.

Drew Wills
A: 

Hashtable is one of the original collection classes in Java, while HashMap is part of the Collections Framework added with Java 2. The main differences are:

  • 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.

  • a HashMap iterator is fail-safe while the enumerator for the Hashtable isn't.

  • a HashMap permits null values in it, while Hashtable doesn't.

So you'd go with HashMap for any new code. If you need synchronization you're better off with Collections.synchronizedMap(HashMap). See this similar SO thread for more ideas.

JRL
I already linked to that SO post in my question. I wanted to know which to use when you DON'T care about nulls or threads.
froadie
+4  A: 

Hashtable is older. It was shipped already in JDK 1.0. In 1.2, when the collections framework was introduced, Hashtable was identified as a problem since it was implemented with all public methods being synchronized. This was a precaution which is only necessary in multi-threaded contexts and hurts performance otherwise (some people have indicated that this could be optimized away, but YMMV).

Unfortunately it was not possible to just remove the synchronization, since some code already relied on Hashtable being implemented this way. Hence, HashMap was born. While they were at it, they threw in the 'permit nulls' feature and adapted it to the general collections framework.

The same thing happened with StringBuffer which new unsynchronized version is called StringBuilder.

So, in short: Use HashMap: it's the newest and most thought through implementation. Hashtable is legacy. If you want a synchronized implementation you can go for Hashtable or Collections.synchronizedMap(Map).

disown
+1 for mentioning that Hashtable is legacy--even if you have a threaded situation, use a synchronized Hashmap instead of Hashtable, or better yet wrap your hashmap in an object where you control the access.
Bill K
@Bill: Actually, the recommended replacement (as of Java 5) for `Hashtable` is `ConcurrentHashMap` (in `java.util.concurrent`), but for code that targets 1.4 or older, I suppose synchronized `HashMap` is reasonable for most uses.
Chris Jester-Young
By the way, `ConcurrentHashMap` is faster than plain `HashMap` even in a single thread context. Try it out yourself if you don't believe me :)
Esko