When should I use a Hashtable versus a HashMap

断了今生、忘了曾经 提交于 2019-11-27 01:05:59

问题


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.


回答1:


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.




回答2:


Never. Hashtable was the original implementation of a map in Java 1. It's been overtaken by the Map implementations. Sure, Hashtable has been retrofitted to match 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.




回答3:


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.




回答4:


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




回答5:


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.



来源:https://stackoverflow.com/questions/9536077/when-should-i-use-a-hashtable-versus-a-hashmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!