Java HashMap 和字典 ADT 之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/267312/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:14:56  来源:igfitidea点击:

Difference between a HashMap and a dictionary ADT

javadata-structures

提问by ashokgelal

What is the difference between a Hash Map and dictionary ADT. And when to prefer one over another. For my programming assignment my instructor has asked to use one of them but I don't see any difference in between both. The program is supposed to work with a huge no. of strings. Any suggestions?

哈希映射和字典 ADT 之间有什么区别。什么时候更喜欢一个。对于我的编程作业,我的老师要求使用其中之一,但我认为两者之间没有任何区别。该程序应该与一个巨大的否一起工作。的字符串。有什么建议?

采纳答案by Phil

In terms of Java, both the class HashMapand the class Dictionaryare implementationsof the "Map" abstract data type. Abstract data types are not specific to any one programming language, and the Map ADT can also be known as a Hash, or a Dictionary, or an Associative Array (others at http://en.wikipedia.org/wiki/Associative_array). (Notice we're making a distinction between the Dictionaryclass and the Dictionary ADT.)

在Java方面,无论是类HashMap和类Dictionary实现了“地图”的抽象数据类型。抽象数据类型并不特定于任何一种编程语言,Map ADT 也可以称为哈希、字典或关联数组(其他位于http://en.wikipedia.org/wiki/Associative_array)。(请注意,我们在Dictionary类和 Dictionary ADT之间进行了区分。)

The Dictionaryclasshas been marked as obsolete, so it's best not to use it.

Dictionary已被标记为过时,所以最好不要使用它。

回答by Vincent Ramdhanie

In Java the HashMap implements the Map interface while the Dictionary does not. That makes the Dictionary obsolete (according to the API docs). That is, they both do a similar function so you are right that they seem very similar...a HashMap is a type of dictionary.

在 Java 中,HashMap 实现了 Map 接口,而 Dictionary 没有。这使得 Dictionary 过时了(根据 API 文档)。也就是说,它们都执行相似的功能,因此您说得对,它们看起来非常相似……HashMap 是一种字典。

You are advised to use the HashMap though.

不过建议您使用 HashMap。

回答by Jim Nelson

This Stack Overflow post does a good job explaining the key differences:

这篇 Stack Overflow 帖子很好地解释了主要区别:

Java hashmap vs hashtable

Java 哈希图与哈希表

Note that Hashtable is simply an implementation of the Dictionary ADT. Also note that Java considers Dictionary "obsolete".

请注意,Hashtable 只是 Dictionary ADT 的一个实现。另请注意,Java 认为 Dictionary “已过时”

The fact that Hashtable is synchronized doesn't buy you much for most uses. Use HashMap.

对于大多数用途来说,Hashtable 是同步的这一事实并没有给您带来太多好处。使用哈希映射。

回答by masec

Map is an interface for an ADT in Java, the same general language-independent data structure for maintaining <key, value> pairs, and is introduced in Java 1.2.

Map 是 Java 中 ADT 的接口,与用于维护 <key, value> 对的通用独立于语言的数据结构相同,并在 Java 1.2 中引入。

Dictionary (not an implementation of Map) is an Abstract class for the same purpose introduced earlier in JDK 1.0. The only subclass it has is Hashtable which itself is implementing Map. Nevertheless, Dictionary class is obsolete now and you may forget it.

Dictionary(不是 Map 的实现)是一个抽象类,其目的与早先在 JDK 1.0 中引入的目的相同。它拥有的唯一子类是 Hashtable,它本身正在实现 Map。尽管如此,Dictionary 类现在已经过时了,您可能会忘记它。

There are differences between the function members of Map and Dictionary, however you may find the difference between HashMap and Hashtable more useful. hereyou can find the differences.

Map 和 Dictionary 的函数成员之间存在差异,但是您可能会发现 HashMap 和 Hashtable 之间的差异更有用。在这里您可以找到差异。