键 - HashMap中的值映射

时间:2021-07-10 19:17:16

Whenever I wanted to use a general purpose data structure like a Set or Map in Java, I was presented with HashSet and HashMap. Upon searching more about these, I've read about the hash table that sits at the base of them. So I learned that, in order to map a key to its corresponding value, it uses a hash function.
My question is: why not use a simple by-index correspondence?
I tried to answer myself but I'm not sure of the correctness of the advantages:
by-index
+ faster, since there's no computation involved
+ collision-free
by-hash
+ memory economy, since a third array would be needed to correlate the keys and values

每当我想在Java中使用像Set或Map这样的通用数据结构时,我就会看到HashSet和HashMap。在搜索了更多这些内容之后,我已经阅读了位于它们底部的哈希表。所以我了解到,为了将键映射到其对应的值,它使用哈希函数。我的问题是:为什么不使用简单的索引对应?我试图回答自己,但我不确定优势的正确性:by-index +更快,因为没有计算涉及+无冲突的副本+内存经济,因为需要第三个数组来关联键和值

But I always thought that a few flops are more precious than a few bytes. Anyway, I don't think there is such a DS in Java as a simple IndexMap, is it? What am I missing here?

但我一直认为一些翻牌比几个字节更珍贵。无论如何,我不认为Java中有这样一个DS作为简单的IndexMap,是吗?我在这里想念的是什么?

EDIT:
By by-index I mean this: an array is by construction an ordered structure, it has indices; so having 2 arrays to correlate, say, keys[] and values[] would mean keys[i] corresponds to values[i]. So, no relation function needed. I'm surely missing something here and I want to see what.

编辑:按索引我的意思是:一个数组是通过构造一个有序的结构,它有索引;所以有2个数组来关联,比如,keys []和values []将意味着keys [i]对应于values [i]。所以,不需要关系功能。我肯定在这里遗漏了一些东西,我想看看是什么。

4 个解决方案

#1


0  

why not use a simple by-index correspondence?

为什么不使用简单的索引对应?

Because this would break the Set contract for that particular implementation; in a Set, all values must be unique according to a defined criterion. In HashSet, this is the equality (as in .equals()/.hashCode()) of objects.

因为这会破坏该特定实现的Set契约;在Set中,根据定义的标准,所有值必须是唯一的。在HashSet中,这是对象的相等(如.equals(。/。hashCode())。

Also, a Set has no iteration order guarantee -- again, by contract. Therefore you cannot use indices to locate anything in a Set, even if you know its .size() (unlike in a List, which looks more like what you want, you will notice that a Set has no .get() method).

此外,Set没有迭代订单保证 - 再次,按合同。因此,即使您知道它的.size(),也不能使用索引来定位Set中的任何内容(与List中的内容不同,它看起来更像您想要的内容,您会注意到Set没有.get()方法)。

Note that keys in a Map are also a Set; keys in a Map are unique.

请注意,Map中的键也是Set;地图中的键是唯一的。

Another advantage of using a hash table is the speed of lookup operations (as in .contains()).

使用哈希表的另一个好处是查找操作的速度(如.contains())。

#2


2  

You can think of a HashMap as IndexMap where the index can be any Object (key) and it is mapped to another Object (value). An IndexMap in your sense would be just a List, a List maps an int index to an Object. But the indexes of a List cannot be Objects.

您可以将HashMap视为IndexMap,其中索引可以是任何Object(键),并且它映射到另一个Object(值)。您意义上的IndexMap只是一个List,List将一个int索引映射到一个Object。但List的索引不能是Objects。

#3


0  

I'm going to take a stab at what your trying to ask. Why are Java HashMap not like EnumMaps or other special hashtable collections that do not require a hash (because the hash is pre-calculated).

我打算试试你要问的问题。为什么Java HashMap不像EnumMaps或其他不需要哈希的特殊哈希表集合(因为哈希是预先计算的)。

Its because HashMap is a generic collection so that you can store any object. If you want a faster HashMap (ie fast hash) then certain assumptions need to be made about the data your storing in the Map (like EnumMap).

因为HashMap是一个通用集合,所以你可以存储任何对象。如果您想要更快的HashMap(即快速哈希),则需要对您在Map中存储的数据(如EnumMap)进行某些假设。

#4


0  

I are in right direction but need to consider few more actions that you might want to perform on your data structure.

我的方向正确但需要考虑您可能想要对数据结构执行的更多操作。

accessing in array costs o(1) which is same as in hash based data structure but searching could be very-very costly o(n) in array where as it is still same o(1) in hash based structure.

以数组成本访问o(1),这与基于散列的数据结构相同,但是在数组中搜索可能是非常非常昂贵的o(n),因为它在基于散列的结构中仍然是相同的o(1)。

While deciding on which data structure is good for your application it depends what you are going to do on that data structure. For further reference please check this page which contains summary of different operation on different data structures:

在确定哪种数据结构对您的应用程序有利时,它取决于您将在该数据结构上执行的操作。如需进一步参考,请查看此页面,其中包含不同数据结构的不同操作摘要:

http://simplenotions.wordpress.com/2009/05/13/java-standard-data-structures-big-o-notation/

Hope it helps.

希望能帮助到你。

#1


0  

why not use a simple by-index correspondence?

为什么不使用简单的索引对应?

Because this would break the Set contract for that particular implementation; in a Set, all values must be unique according to a defined criterion. In HashSet, this is the equality (as in .equals()/.hashCode()) of objects.

因为这会破坏该特定实现的Set契约;在Set中,根据定义的标准,所有值必须是唯一的。在HashSet中,这是对象的相等(如.equals(。/。hashCode())。

Also, a Set has no iteration order guarantee -- again, by contract. Therefore you cannot use indices to locate anything in a Set, even if you know its .size() (unlike in a List, which looks more like what you want, you will notice that a Set has no .get() method).

此外,Set没有迭代订单保证 - 再次,按合同。因此,即使您知道它的.size(),也不能使用索引来定位Set中的任何内容(与List中的内容不同,它看起来更像您想要的内容,您会注意到Set没有.get()方法)。

Note that keys in a Map are also a Set; keys in a Map are unique.

请注意,Map中的键也是Set;地图中的键是唯一的。

Another advantage of using a hash table is the speed of lookup operations (as in .contains()).

使用哈希表的另一个好处是查找操作的速度(如.contains())。

#2


2  

You can think of a HashMap as IndexMap where the index can be any Object (key) and it is mapped to another Object (value). An IndexMap in your sense would be just a List, a List maps an int index to an Object. But the indexes of a List cannot be Objects.

您可以将HashMap视为IndexMap,其中索引可以是任何Object(键),并且它映射到另一个Object(值)。您意义上的IndexMap只是一个List,List将一个int索引映射到一个Object。但List的索引不能是Objects。

#3


0  

I'm going to take a stab at what your trying to ask. Why are Java HashMap not like EnumMaps or other special hashtable collections that do not require a hash (because the hash is pre-calculated).

我打算试试你要问的问题。为什么Java HashMap不像EnumMaps或其他不需要哈希的特殊哈希表集合(因为哈希是预先计算的)。

Its because HashMap is a generic collection so that you can store any object. If you want a faster HashMap (ie fast hash) then certain assumptions need to be made about the data your storing in the Map (like EnumMap).

因为HashMap是一个通用集合,所以你可以存储任何对象。如果您想要更快的HashMap(即快速哈希),则需要对您在Map中存储的数据(如EnumMap)进行某些假设。

#4


0  

I are in right direction but need to consider few more actions that you might want to perform on your data structure.

我的方向正确但需要考虑您可能想要对数据结构执行的更多操作。

accessing in array costs o(1) which is same as in hash based data structure but searching could be very-very costly o(n) in array where as it is still same o(1) in hash based structure.

以数组成本访问o(1),这与基于散列的数据结构相同,但是在数组中搜索可能是非常非常昂贵的o(n),因为它在基于散列的结构中仍然是相同的o(1)。

While deciding on which data structure is good for your application it depends what you are going to do on that data structure. For further reference please check this page which contains summary of different operation on different data structures:

在确定哪种数据结构对您的应用程序有利时,它取决于您将在该数据结构上执行的操作。如需进一步参考,请查看此页面,其中包含不同数据结构的不同操作摘要:

http://simplenotions.wordpress.com/2009/05/13/java-standard-data-structures-big-o-notation/

Hope it helps.

希望能帮助到你。