我知道map是一种将键映射到值的数据结构。字典不也是这样吗?地图和字典的区别是什么?
1. 我不是问它们在语言X或Y中是如何定义的(这似乎是人们在这里问的),我想知道它们在理论上有什么不同。
我知道map是一种将键映射到值的数据结构。字典不也是这样吗?地图和字典的区别是什么?
1. 我不是问它们在语言X或Y中是如何定义的(这似乎是人们在这里问的),我想知道它们在理论上有什么不同。
当前回答
其实不是一回事。映射是字典的一个子集。Dictionary在这里定义为具有插入、删除和查找函数。Java使用的Map(根据本文)是一个字典,它要求键映射到值严格地映射为一对一的函数。一个字典可能有多个键映射到一个值,或者一个键映射到几个值(如hashtable中的链接),例如Twitter标签搜索。
As a more "real world" example, looking up a word in a dictionary can give us a number of definitions for the same word, and when we find an entry that points us to another entry (see other word), a number of words for the same list of definitions. In the real world, maps are much broader, allowing us to have locations for names or names for coordinates, but also we can find a nearest neighbor or other attributes (populations, etc), so IMHO there could be argument for a greater expansion of the map type to possibly have graph based implementations, but it would be best to always assume just the key-value pair, especially since nearest neighbor and other attributes to the value could all just be data members of the value.
尽管有一对一的要求,但如果值被泛化为集合本身,或者值仅仅是对存储在其他地方的集合的引用,则Java映射可以实现更类似于广义字典的东西。
请记住,Java维护者不是ADT定义的维护者,Java决策是专门针对Java的。
其他回答
这是同一个概念的两个不同术语。 Hashtable和HashMap也引用相同的概念。
一个是另一个的旧说法。通常,“字典”一词是在数学术语“地图”占主导地位之前使用的。此外,字典倾向于有键类型的字符串,但这并不是100%正确的。
所以在纯理论层面上。
字典是一个可以用来定位链接值的值。 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
计算机科学术语摘要:
字典是表示一组元素的数据结构,包含插入、删除和成员关系测试;元素可以(但不一定)由不同的键和值部分组成 map是一种关联数据结构,能够存储一组键,每个键与一个(或多个-例如c++的multimap)值相关联,能够访问和删除仅给定键的现有项。
讨论
回答这个问题很复杂,因为程序员在他们使用的特定语言或系统中看到了术语被赋予了更具体的含义,但这个问题要求“理论上”进行语言不可知的比较,我指的是计算科学术语。
术语解释
牛津大学计算机科学词典列出:
字典表示一组元素的任何数据结构,这些数据结构支持元素的插入和删除以及成员关系测试
例如,我们有一组元素{a, B, C, D…},我们已经能够插入和开始删除,我们能够查询“C是否存在?”
然而,地图的计算科学概念是基于数学语言术语映射,牛津词典将其定义为:
将给定集合(域)的每个元素与第二个集合(范围)的一个或多个元素相关联的操作。
As such, a map data structure provides a way to go from elements of a given set - known as "keys" in the map, to one or more elements in the second set - known as the associated "value(s)". The "...or more elements in the second set" aspect can be supported by an implementation is two distinct way: Many map implementations enforce uniqueness of the keys and only allow each key to be associated with one value, but that value might be able to be a data structure itself containing many values of a simpler data type, e.g. { {1,{"one", "ichi"}, {2, {"two", "ni"}} } illustrates values consisting of pairs/sets of strings. Other map implementations allow duplicate keys each mapping to the same or different values - which functionally satisfies the "associates...each [key] element...with...more [than one] [value] elements" case. For example, { {1, "one"}, {1, "ichi"}, {2, "two"}, {2, "ni"} }.
字典和地图对比
因此,使用上面严格的Comp Sci术语,只有当接口恰好支持不是每个字典都需要的附加操作时,字典才会是映射:
能够存储具有不同键和值组件的元素 能够检索和删除只给定键的值
一个小插曲:
映射接口可能不直接支持测试容器中是否有{key,value}对,这是字典的学究式要求,其中元素恰好是{key,value}对;映射甚至可能没有测试键的函数,但在最坏的情况下,您可以查看尝试的按键值检索是否成功或失败,然后如果您关心,您可以检查是否检索了期望的值。
与听众清晰地沟通
机场机场机场机场机场机场机场机场机场机场机场机场机场机场机场机场机场机场机场机场机场机场机场机场机场这个问题的其他答案(以及他们的赞)表明,在大多数程序员的经验中,“字典”将成为“地图”的同义词。试着选择更广泛和更容易理解的术语。
关联容器:任何存储键/值对的容器,可以按键进行值检索和擦除 哈希映射:关联容器的哈希表实现 强制惟一键的哈希集:存储元素/值的字典的哈希表实现,不将它们视为包含不同的键/值组件,其中元素的副本不能插入 平衡二叉树映射支持重复键:…
交叉引用Comp Sci术语与具体实现
c++标准库
maps: map, multimap, unordered_map, unordered_multimap other dictionaries: set, multiset, unordered_set, unordered_multiset note: with iterators or std::find you can erase an element and test for membership in array, vector, list, deque etc, but the container interfaces don't directly support that because finding an element is spectacularly inefficient at O(N), in some cases insert/erase is inefficient, and supporting those operations undermines the deliberately limited API the container implies - e.g. deques should only support erase/pop at the front and back and not in terms of some key. Having to do more work in code to orchestrate the search gently encourages the programmer to switch to a container data structure with more efficient searching.
...以后可以添加其他语言/可以随意编辑…
其实不是一回事。映射是字典的一个子集。Dictionary在这里定义为具有插入、删除和查找函数。Java使用的Map(根据本文)是一个字典,它要求键映射到值严格地映射为一对一的函数。一个字典可能有多个键映射到一个值,或者一个键映射到几个值(如hashtable中的链接),例如Twitter标签搜索。
As a more "real world" example, looking up a word in a dictionary can give us a number of definitions for the same word, and when we find an entry that points us to another entry (see other word), a number of words for the same list of definitions. In the real world, maps are much broader, allowing us to have locations for names or names for coordinates, but also we can find a nearest neighbor or other attributes (populations, etc), so IMHO there could be argument for a greater expansion of the map type to possibly have graph based implementations, but it would be best to always assume just the key-value pair, especially since nearest neighbor and other attributes to the value could all just be data members of the value.
尽管有一对一的要求,但如果值被泛化为集合本身,或者值仅仅是对存储在其他地方的集合的引用,则Java映射可以实现更类似于广义字典的东西。
请记住,Java维护者不是ADT定义的维护者,Java决策是专门针对Java的。