简体   繁体   English

如何使用O(1)get()方法将键映射到java中hasmap中的值?

[英]How key is mapped to value in a hasmap in java with O(1) get() method?

Consider an int array variable x[]. 考虑一个int数组变量x []。 The variable X will have starting address reference. 变量X将具有起始地址引用。 When array is accessed with index 2 that is x[2].then its memory location is calculated as 当索引2为x [2]访问数组时,然后将其内存位置计算为

address of x[2] is starting addr + index * size of int. x [2]的地址是int的起始地址+索引*的大小。

ie. 即。 x[2]=x + 2*4. x [2] = x + 2 * 4。

But in case of hashmap how the memory address will be mapped internally. 但是在hashmap的情况下如何在内部映射内存地址。

By reading many previous posts I observed that HashMap uses a linked list to store the key value list. 通过阅读许多以前的帖子,我发现HashMap使用链表来存储键值列表。 But if that is the case, then to find a key, it generates a hashcode then it will checks for equal hash code in list and retrieves the value.. 但如果是这种情况,那么为了找到一个密钥,它会生成一个哈希码,然后它将检查列表中的相等哈希码并检索该值。

This takes O(n) complexity. 这需要O(n)复杂度。 If i am wrong in above observation Please correct me... I am a beginner. 如果我在上述观察中出错了请纠正我...我是初学者。 Thank you 谢谢

The traditional implementation of a HashMap is to use a function to generate a key, then use that key to access a value directly. HashMap的传统实现是使用函数生成密钥,然后使用该密钥直接访问值。 Think of it as generating something that will translate into an array index. 将其视为生成将转换为数组索引的内容。 It does not look through the hashmap comparing element hashes to the generated hash; 它不会查看将元素哈希值与生成的哈希值进行比较的哈希值; it generates the hash, and uses the hash to access an element directly. 它生成哈希,并使用哈希直接访问元素。

What I think you're talking about is the case where two values in the HashMap generate the SAME key. 我认为你在谈论的是HashMap中的两个值生成SAME密钥的情况。 THEN it uses a list of those, and has to look through them to determine which one it wants. 然后它使用那些列表,并且必须通过它们来确定它想要的那个。 But that's not O(n) where n is the number of elements in the HashMap, just O(m) where m is the number of elements with the same hash. 但这不是O(n),其中n是HashMap中元素的数量,只是O(m),其中m是具有相同散列的元素的数量。 Clearly the name of the game is to find a hash function where the generated hash is unique for all the elements, as much as is feasible. 显然,游戏的名称是找到一个散列函数,其中生成的散列对于所有元素是唯一的,尽可能多。

--- edit to expand on the explanation --- ---编辑扩大解释---

In your post, you state: 在您的帖子中,您声明:

By reading many previous posts I observed that HashMap uses a linked list to store the key value list. 通过阅读许多以前的帖子,我发现HashMap使用链表来存储键值列表。

This is wrong for the overall HashMap. 这对整个HashMap来说是错误的。 For a HashMap to work reasonably, there must be a way to use the key to calculate a way to access the corresponding element directly, not by searching through all the values in the HashMap. 为了使HashMap合理地工作,必须有一种方法来使用密钥来计算直接访问相应元素的方法,而不是通过搜索HashMap中的所有值。

A "perfect" hash calculation would translate each possible key into hash value that was not calculated for any other key. “完美”哈希计算会将每个可能的密钥转换为未针对任何其他密钥计算的哈希值。 This is usually not feasible, and so it is usually possible that two different keys will result in the same result from the hash calculation. 这通常是不可行的,因此通常可能两个不同的密钥将导致哈希计算的相同结果。 In this case, the HashMap implementation could use a linked list of values, and would need to look through all such values to find the one that it was looking for. 在这种情况下,HashMap实现可以使用链接的值列表,并且需要查看所有这些值以找到它正在查找的值。 But this number is FAR less than the number of values in the overall HashMap. 但是这个数字的FAR小于整个HashMap中的值的数量。

You can make a hash where strings are the keys, and in which the first character of the string is converted to a number which is then used as an array index. 您可以创建一个以字符串为键的哈希值,并将字符串的第一个字符转换为一个数字,然后将其用作数组索引。 As long as your strings all have different first characters, then accessing the value is a simple calc plus an array access -- O(1). 只要你的字符串都有不同的第一个字符,那么访问该值就是一个简单的计算加上一个数组访问--O(1)。 Or you could add all the character values of the string indices together and take the last two (or three) digits, and THAT would be your hash calc. 或者你可以将字符串索引的所有字符值加在一起并取最后两(或三)个数字,那就是你的哈希计算。 As long as the addition produced unique values for each index string, you don't ever have to look through a list; 只要添加为每个索引字符串生成唯一值,您就不必查看列表; again, O(1). O(1)。

And, in fact, as long as the hash calculation is approximately perfect, the lookup is still O(1) overall, because the limited number of times you have to look through a short list does not alter the overall efficiency. 而且,实际上,只要哈希计算近乎完美,查找仍然是O(1)整体,因为您必须查看短列表的次数有限并不会改变整体效率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM