hash增加键值 ruby hash表建立
转载
如何创建和使用哈希表
本示例阐释如何创建和使用哈希表。哈希表是由键-值组合构成的集合,组织到"存储桶"中以供快速搜索。可以通过键或关联的值搜索哈希表;但是如果快速搜索是您的目标,按键搜索通常更快。
如同介绍中提到的,哈希表是一种集合。当填充哈希表时,为它提供一个要添加到表中的键,并提供伴随该键的值。在下面的示例中,雇员 ID 号用于键,然后是雇员姓名,以帮助您了解可使用的组合类型。注意键不要求是数值:任何对象都可接受。
哈希表为什么很有用?当向哈希表添加这些键-值对之一时,哈希表将查找该键并给它一个哈希代码,此代码是标识该键的数字(所添加的每个键都会得到一个哈希代码)。然后将这些哈希代码放入“存储桶”以帮助组织表中各项。这对以后尝试在表中进行查找时会有帮助。
暂时假想不用这些存储桶。如果想在数组中查找某元素,则必须遍历数组的每个元素来查找该元素。您不知道它在哪里,因此没有搜索各项的快速方法。相反,哈希表可以较快地搜索特定键,因为一旦为它提供了键,它便知道要在哪个存储桶中查找。这意味着它只须查找所有元素的一个子集。
有一点很重要,请记住这种较快的搜索机制仅当通过键搜索时可用。可以通过值搜索,但它可能要比键搜索慢。因此,如果可能则应执行键搜索。
若要向哈希表添加元素,请调用 Add
// Here we'll create a hashtable of employee numbers and employee names
Hashtable table = new Hashtable();
// now, we will add elements to the hashtable, as key-value pairs
table.Add(5123, "Jay");
table.Add(1829, "Tom");
table.Add(2882, "Matt");
' Here we'll create a hashtable of employee numbers and employee names
Dim table As Hashtable = New Hashtable()
' now, we will add elements to the hashtable, as key-value pairs
table.Add(5123, "Jay")
table.Add(1829, "Tom")
table.Add(2882, "Matt")
|
C# | VB |
|
现在已填充了哈希表,便可以在其中搜索元素了。将通过键和值进行搜索。注意在实际示例中,您将从用户处接受要查找的值或键,因此其实现略有不同,但概念相同。
// SEARCHING BY VALUE. Use the method ContainsValue
String valueToFind = "Jay";
if (table.ContainsValue(valueToFind)) {
Console.WriteLine("Found {0} in the list.", valueToFind );
}
// SEARCHING BY KEY. Use the method Contains
int keyToFind = 5123;
if (table.Contains(keyToFind)) {
Console.WriteLine("Found {0} in the list.", keyToFind);
}
' SEARCHING BY VALUE. Use the method ContainsValue
Dim valueToFind as String = "Jay"
If table.ContainsValue(valueToFind) Then
Console.WriteLine("Found {0} in the list.", valueToFind )
End If
' SEARCHING BY KEY. Use the method Contains
Dim keyToFind As Integer = 5123
If table.Contains(keyToFind) Then
Console.WriteLine("Found {0} in the list.", keyToFind)
End If
|
C# | VB |
|
当然,您可以做的不仅仅是搜索。可以使用 Remove
// using the Remove method to delete a particular item...
table.Remove(5123);
// enumerating over the collection with a foreach statement
// Note the alternate of iterating through by value is demonstrated
// foreach (Object o in table.Values) {
foreach (Object o in table.Keys) {
Console.WriteLine(o.ToString());
}
' using the Remove method to delete a particular item...
table.Remove(5123);
Dim o As Object
' enumerating over the collection with a foreach statement
' Note the alternate of iterating through by value is demonstrated
' For Each o In table.Values
For Each o In table.Keys
Console.WriteLine(o.ToString())
Next
|
C# | VB |
|
这些是使用哈希表的基本操作。用项填充了哈希表以后,了解如何迭代通过哈希表很重要。此重要主题在如何迭代通过集合一节中讲述。下面的代码向您展示如何使用 Foreach(VB 中为 For Each)语句迭代通过列表,并向屏幕输出每个键-值组合。若要这样做,您还需要引用每个元素的 Key 和 Value
// if you are wondering where DictionaryEntry comes from, it is
// the generic object type of each element in the hashtable
foreach (DictionaryEntry d in table) {
// use the Key and Value properties
Console.WriteLine ("{0}\t{1}", d.Key, d.Value);
}
' if you are wondering where DictionaryEntry comes from, it is
' the generic object type of each element in the hashtable
Dim d As DictionaryEntry
For Each d in table
' use the Key and Value properties. Note chr(9) is a tab
Console.WriteLine ("{0}" & chr(9) & "{1}", d.Key, d.Value)
Next d
|
C# | VB |
|
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。