22

Possible Duplicate:
Differences between HashMap and Hashtable?

I've seen hash tables and hash maps used in different code but they look like they do the same thing. Is there a difference between them? Which one should I use in my code?

6
  • Yep, the primary difference is that Hashtable is synchronized, and HashMap is not. They also have different superclass hierarchies. (FWIW, I've on a couple of occasions seen fairly severe failures in large commercial apps due to using HashMap in a concurrent environment.) Commented Nov 10, 2012 at 1:00
  • BTW, it's "HashMap" and "Hashtable". Note the capitalization. Commented Nov 10, 2012 at 1:01
  • 2
    Did you consider reading the Javadoc? Commented Nov 10, 2012 at 1:08
  • @HotLicks - "I've on a couple of occasions seen fairly severe failures in large commercial apps due to using HashMap in a concurrent environment." - but using a Hashtable might be a bad idea too due to contention issues. Concurrent applications require more sophisticated thinking ... if you want them to scale. Commented Nov 10, 2012 at 1:27
  • Hashtable is more or less deprecated; ConcurrentHashMap is designed to work in a concurrent environment, but is much better at it. Commented Nov 10, 2012 at 2:13

3 Answers 3

11

java.util.Hashtable methods are synchronized , java.util.Hashmap methods are not. If you use Hashtable there will be a performance hit as no two threads will be able to access its methods at the same time. If you care about Thread safety in your app Hashtable is the way to go. if you dont care about thread safety Hashmap is the way to go as it is mor eefficient then hashtable. also java.util.Hashtable doesnt allow any null keys, where as java.util.HashMap allows one null key.

Sign up to request clarification or add additional context in comments.

Comments

4

Hashtable is synchronized, where as HashMap is not. This means if you only have a single thread accessing the data, use a HashMap, otherwise use a Hashtable.

2 Comments

its Hashtable , note the t:)
@GanGnaMStYleOverFlowErroR wow, never noticed, haha =)
1

HashTable dont't allow null keys where as hashmap allows one null key

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.