通俗的解释一下这句话? (与哈希表有关)
Explain this sentence in layman's terms? (Has to do with hash tables)
句子的上下文:
"A hash table, put simply, is an abstraction of an array that allows any value to be used as an index. While an array requires that indices be integers, a hash table can use a floating-point value, a string, another array, or even a structure as the index. This index is called the key, and the contents of the array element at that index is called the value. So a hash table is a data structure that stores key/value pairs and can be quickly searched by the key.."
需要解释的句子:
"To achieve this magic, a hash table uses a helper function that converts any object into an integral index suitable for subscripting the array."
^通俗地说这是什么意思?积分指数?订阅?请解释一下,我对哈希表的工作原理一无所知,目前正试图理解它们。
谢谢!
这意味着散列table键可以是任何对象需要被翻译成一个整数,可以用作数组的索引。数组项是与键关联的值
示例:
Map<String, String> m = new HashMap<>();
m.put("name", "Sharon");
幕后所做的是 "name" 被神奇地转换为整数(例如 10)并且 "Sharon" 被放入第 10 个索引处的 String[] 数组
神奇之处在于翻译需要不同的键会产生不同的索引
a hash table uses a helper function
这里说的是hashCode()
函数。 Java中的每个对象都有这个功能。
that converts any object into an integral index
hashCode()
函数 returns 基于对象中值的整数。例如,String
的哈希码基于 String
.
中的字符
suitable for subscripting the array.
然后可以将该整数用作数组的索引。
这是 HashMap
或 Hashtable
如何在后台工作的基础知识。您通常不必担心这些细节,但大多数计算机科学专业的学生在某些时候需要阅读它们。
让我们试着用一个例子来理解它——
假设我需要一个 hashtable
可以将 <Key,Value>
对存储为 <DeptName,DeptObj>
.
所以,首先我插入一对作为 "Science"
(字符串键),Dept("Science")
(某些部门对象)。
现在要存储此 dept
对象,程序必须计算 array
的 index
,以便可以将此对象存储在 index
处。它应用一种称为 hashcode()
的方法,该方法根据其内容将键(在我们的例子中为 Science
)转换为整数。假设 "Science"
的 hashcode
是 1234。
因此,Dept("Science")
将存储在索引为 1234 的数组中。
现在,如果我想插入另一个元素作为 English,Dept("English")
,它将计算 "English"
的 hashcode
(假设为 2345)并将 Dept("English")
存储在该位置(索引)。
现在,如果我们这样做,我们将需要一个巨大的数组(这将非常稀疏,这在实践中无法实现。
因此,In Actual 使用了 Modulus
函数。
因此,当 "Science"
解析为 1234 时,它除以 16,得到模数 2(这是存储 Dept("Science")
的实际索引,"English"
也是如此。
因此,简而言之,hashcode
应用于将其转换为整数的键,然后采用模数将其滚动到所需的边界(这也会导致冲突)。
句子的上下文: "A hash table, put simply, is an abstraction of an array that allows any value to be used as an index. While an array requires that indices be integers, a hash table can use a floating-point value, a string, another array, or even a structure as the index. This index is called the key, and the contents of the array element at that index is called the value. So a hash table is a data structure that stores key/value pairs and can be quickly searched by the key.."
需要解释的句子:
"To achieve this magic, a hash table uses a helper function that converts any object into an integral index suitable for subscripting the array."
^通俗地说这是什么意思?积分指数?订阅?请解释一下,我对哈希表的工作原理一无所知,目前正试图理解它们。
谢谢!
这意味着散列table键可以是任何对象需要被翻译成一个整数,可以用作数组的索引。数组项是与键关联的值
示例:
Map<String, String> m = new HashMap<>();
m.put("name", "Sharon");
幕后所做的是 "name" 被神奇地转换为整数(例如 10)并且 "Sharon" 被放入第 10 个索引处的 String[] 数组 神奇之处在于翻译需要不同的键会产生不同的索引
a hash table uses a helper function
这里说的是hashCode()
函数。 Java中的每个对象都有这个功能。
that converts any object into an integral index
hashCode()
函数 returns 基于对象中值的整数。例如,String
的哈希码基于 String
.
suitable for subscripting the array.
然后可以将该整数用作数组的索引。
这是 HashMap
或 Hashtable
如何在后台工作的基础知识。您通常不必担心这些细节,但大多数计算机科学专业的学生在某些时候需要阅读它们。
让我们试着用一个例子来理解它——
假设我需要一个 hashtable
可以将 <Key,Value>
对存储为 <DeptName,DeptObj>
.
所以,首先我插入一对作为 "Science"
(字符串键),Dept("Science")
(某些部门对象)。
现在要存储此 dept
对象,程序必须计算 array
的 index
,以便可以将此对象存储在 index
处。它应用一种称为 hashcode()
的方法,该方法根据其内容将键(在我们的例子中为 Science
)转换为整数。假设 "Science"
的 hashcode
是 1234。
因此,Dept("Science")
将存储在索引为 1234 的数组中。
现在,如果我想插入另一个元素作为 English,Dept("English")
,它将计算 "English"
的 hashcode
(假设为 2345)并将 Dept("English")
存储在该位置(索引)。
现在,如果我们这样做,我们将需要一个巨大的数组(这将非常稀疏,这在实践中无法实现。
因此,In Actual 使用了 Modulus
函数。
因此,当 "Science"
解析为 1234 时,它除以 16,得到模数 2(这是存储 Dept("Science")
的实际索引,"English"
也是如此。
因此,简而言之,hashcode
应用于将其转换为整数的键,然后采用模数将其滚动到所需的边界(这也会导致冲突)。