简体   繁体   English

在任何情况下,哈希表都比并发哈希表更好?

[英]is there any scenario where hashtable is better than concurrenthashmap?

众所周知,ConcurrentHashMap在性能上更好,但是我们可以在任何情况下使用Hashtable更好吗?

I'll say this before answering the question: do not ever use Hashtable anymore . 在回答问题之前,我会先说: 不要再使用Hashtable Hashtable is a legacy tool from Java 1.0, before the superior collections framework introduced with Java 2 going onwards. Hashtable是Java 1.0的遗留工具,在Java 2以后引入的高级集合框架之前。

If you require a simple hash map, use HashMap . 如果您需要简单的哈希映射,请使用HashMap If you require performing thread-safety, use ConcurrentHashMap . 如果需要执行线程安全性,请使用ConcurrentHashMap If you require plain basic and non-performing thread-safety, wrap a HashMap into Collections.synchronizedMap(Map) . 如果您需要简单的基本线程性能和非线程安全性,则将HashMap包装到Collections.synchronizedMap(Map)

Now to actually answer the question as is, which is purely compare two specific classes without seeing the spectrum of possibilities: 现在按原样实际回答问题,这是在不了解可能性范围的情况下,仅比较两个特定的类:

Yes, such scenarios exist 是的,存在这种情况

From the Hashtable documentation : Hashtable文档中

If a thread-safe implementation is not needed, it is recommended to use HashMap in place of Hashtable. 如果不需要线程安全的实现,建议使用HashMap代替Hashtable。 If a thread-safe highly-concurrent implementation is desired, then it is recommended to use ConcurrentHashMap in place of Hashtable. 如果需要线程安全的高度并发实现,则建议使用ConcurrentHashMap代替Hashtable。

  1. So yes, Hashtable is appropriate for scenarios where you need a thread-safe implementation, but do not "desire" a highly-concurrent implementation. 因此,是的, Hashtable适用于需要线程安全的实现,但又不“希望” 高度并行实现的情况。 This, again strictly in the exclusive comparison between ConcurrentHashMap and Hashtable . 同样,严格在ConcurrentHashMapHashtable之间进行排他比较。

  2. Also, if you need Enumeration [1] , Hashtable has a direct support, while you have to go through Collections.enumeration(...) for other Map s. 另外,如果您需要Enumeration [1] ,则Hashtable可以直接提供支持,而其他Map则必须通过Collections.enumeration(...)


1. Enumeration is also a Java 1.0 class. 1. Enumeration也是Java 1.0类。 Switch to Iterator (if using Java 2 to 7) or Stream (if using Java 8+) 切换到Iterator (如果使用Java 2至7)或Stream (如果使用Java 8+)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM