我知道map是一种将键映射到值的数据结构。字典不也是这样吗?地图和字典的区别是什么?


1. 我不是问它们在语言X或Y中是如何定义的(这似乎是人们在这里问的),我想知道它们在理论上有什么不同。


当前回答

所以在纯理论层面上。

字典是一个可以用来定位链接值的值。 Map是一个值,它提供了如何定位另一个值的指令

所有允许非线性访问的集合(即只有get first或get last)都是Map,因为即使是简单的Array也有一个映射到正确值的索引。因此,虽然字典是地图的一种类型,但地图具有更广泛的功能。

In Practice a its usually the mapping function that defines the name, so a HashMap is a mapped data structure that uses a hashing algorithm to link the key to the value, where as a Dictionary doesn't specify how the keys are linked to a value so could be stored via a linked list, tree or any other algorithm. from the usage end you usually don't care what the algorithm only that they work so you use a generic dictionary and only shift to one of the other structures only when you need to enfore the type of algorithm

其他回答

这是同一个概念的两个不同术语。 Hashtable和HashMap也引用相同的概念。

通常我假设映射是由哈希表支持的;它意味着一个无序的存储。 字典意味着有序的存储。

有一个基于树的字典叫做Trie。

在Lisp中,它可能是这样的:

(a (n (d t)) n d )

这句话概括为:

一个 而且 蚂蚁 一个 广告

从顶部到叶的遍历产生一个单词。

一个是另一个的旧说法。通常,“字典”一词是在数学术语“地图”占主导地位之前使用的。此外,字典倾向于有键类型的字符串,但这并不是100%正确的。

我现在在一个数据结构类中,我的理解是dict()数据类型也可以初始化为dictionary ={}或键和值,基本上与列表/数组数据类型用于实现堆栈和队列相同。因此,dict()是类型,映射是一个结果数据结构,您可以选择用字典数据类型实现,就像您可以使用列表类型并选择用它实现堆栈或队列数据结构一样。

我的2分钱。

Dictionary是Java中的抽象类,而Map是接口。由于Java不支持多重继承,如果一个类扩展了Dictionary,它就不能扩展任何其他类。

因此,引入了Map接口。

字典类已过时,首选使用Map。