Java 哈希表和字典有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9769797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between Hashtable and Dictionary?
提问by Samir Mangroliya
What's the difference between Dictionary
and Hashtable
and how do I work with the Dictionary
class in Java?
什么之间的区别Dictionary
,并Hashtable
和我如何与工作Dictionary
中的Java类?
采纳答案by AlexR
Dictionary
is an abstract base class of Hashtable
. Both are still in JDK for backwards compatibility with old code. We are expected to use HashMap
and other implementations of Map
interface introduced in Java 1.2.
Dictionary
是 的抽象基类Hashtable
。为了与旧代码向后兼容,两者仍在 JDK 中。我们预计会使用Java 1.2 中引入的HashMap
其他Map
接口实现。
回答by Bogdan Emil Mariesan
I've found a lecture on Principles of OOP that contains the answer you seek:
我找到了一个关于 OOP 原理的讲座,其中包含您寻求的答案:
EDIT:
编辑:
DictionaryA major theme in computing is the theme of storage/retrieval/removal: store data somewhere so that it can later be retrieved and discarded if no longer needed, all of this in the most efficient manner. The abstraction of these computing activities is embodied in the notion of what is called a dictionary, expressed in Java as an interface as follows.
Hash TablesA hash table is a generalization of an ordinary array. When the number of keys actually stored is small relative to the total number of possible keys, hash tables become an effective alternative to directly addressing an array, since a hash table typically uses an array of size proportional to the number of keys actually stored. Instead of using the key as an array index directly, the array index is computed from the key. With hashing, an element with key k is stored in slot h(k); i.e., a hash function h is used to compute the slot from the key k. h maps the set U of keys into the slots of a hash table T[0..m-1]: h:U -> {0, 1, ..., m - 1}
字典计算中的一个主要主题是存储/检索/删除的主题:将数据存储在某处,以便以后在不再需要时可以检索和丢弃,所有这些都以最有效的方式进行。这些计算活动的抽象体现在所谓的字典的概念中,用 Java 表示为如下接口。
哈希表哈希表是普通数组的泛化。当实际存储的键数相对于可能键的总数较小时,哈希表成为直接寻址数组的有效替代方案,因为哈希表通常使用大小与实际存储的键数成正比的数组。不是直接使用键作为数组索引,而是从键计算数组索引。通过散列,键为 k 的元素存储在槽 h(k) 中;即,散列函数 h 用于从密钥 k 计算槽。h 将键的集合 U 映射到哈希表 T[0..m-1] 的槽中: h:U -> {0, 1, ..., m - 1}
回答by kundan bora
The Dictionary class is the abstract parent of any class, such as Hashtable, which maps keys to values. Every key and every value is an object. In any one Dictionary object, every key is associated with at most one value. Given a Dictionary and a key, the associated element can be looked up. Any non-null object can be used as a key and as a value.
Dictionary 类是任何类的抽象父类,例如 Hashtable,它将键映射到值。每个键和每个值都是一个对象。在任何一个 Dictionary 对象中,每个键最多与一个值相关联。给定一个字典和一个键,可以查找相关的元素。任何非空对象都可以用作键和值。
回答by Bill the Lizard
The javadoc for Dictionaryhas your answer.
字典的 javadoc有你的答案。
The Dictionary class is the abstract parent of any class, such as Hashtable, which maps keys to values.
Dictionary 类是任何类的抽象父类,例如 Hashtable,它将键映射到值。
You don't work directly with Dictionary
, since it is an abstract
class.
您不直接使用Dictionary
,因为它是一个abstract
类。
Also note the following from the same documentation:
另请注意同一文档中的以下内容:
NOTE: This class is obsolete. New implementations should implement the Mapinterface, rather than extending this class.
注意:这个类已经过时了。新的实现应该实现Map接口,而不是扩展这个类。
回答by Nick
Hashtable
is an implementation of Dictionary
. You can't use Dictionary
directly because it's an abstract class.
Hashtable
是 的实现Dictionary
。你不能Dictionary
直接使用,因为它是一个抽象类。
But you shouldn't use either because they have been superceded by the Map
interface and the implementing classes, of which HashMap
is the most popular.
但是你不应该使用它们,因为它们已经被Map
接口和实现类取代,其中HashMap
最流行的。
回答by duffymo
According to the javadocs for Dictionary:
根据字典的javadocs:
NOTE: This class is obsolete. New implementations should implement the Map interface, rather than extending this class.
注意:这个类已经过时了。新的实现应该实现 Map 接口,而不是扩展这个类。
Hashtable is JDK 1.0 vintage, too. You should prefer the Map interface and its more modern implementations: HashMap and TreeMap.
Hashtable 也是 JDK 1.0 版本。您应该更喜欢 Map 接口及其更现代的实现:HashMap 和 TreeMap。
回答by assylias
Dictionary
is an abstract class, superclass of Hashtable
.
You should not use Dictionary
as it is obsolete.
As for Hashtable, the advantage it had over other maps such as HashMap
was thread safety, but with the introduction of ConcurrentHashMap since Java 1.5, there is no real reason to use it any longer - see javadoc
Dictionary
是一个抽象类,是 的超类Hashtable
。你不应该使用Dictionary
它,因为它已经过时了。至于 Hashtable,它优于其他映射(例如HashMap
线程安全)的优势,但是随着自 Java 1.5 以来 ConcurrentHashMap 的引入,没有真正的理由再使用它 - 参见javadoc
As of the Java 2 platform v1.2, this class was retrofitted to implement the Map interface, making it a member of the Java Collections Framework. Unlike the new collection implementations, Hashtable is synchronized. If a thread-safe implementation is not needed, it is recommended to use HashMap in place of Hashtable. If a thread-safe highly-concurrent implementation is desired, then it is recommended to use ConcurrentHashMap in place of Hashtable.
从 Java 2 平台 v1.2 开始,该类经过改造以实现 Map 接口,使其成为 Java Collections Framework 的成员。与新的集合实现不同,Hashtable 是同步的。如果不需要线程安全的实现,建议使用 HashMap 代替 Hashtable。如果需要线程安全的高并发实现,那么建议使用 ConcurrentHashMap 代替 Hashtable。
In summary: Don't use Dictionary
or Hashtable
, unless you really have to for compatibility reasons, use either HashMap
if you don't need thread safety, or ConcurrentHashMap
if your map is used in a concurrent environment.
总结:不要使用Dictionary
or Hashtable
,除非出于兼容性原因确实必须使用,否则在HashMap
不需要线程安全或ConcurrentHashMap
在并发环境中使用地图时使用。