本文整理汇总了C#中HashTable类的典型用法代码示例。如果您正苦于以下问题:C# HashTable类的具体用法?C# HashTable怎么用?C# HashTable使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HashTable类属于命名空间,在下文中一共展示了HashTable类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Add_OneItem
public void Add_OneItem()
{
HashTable<string, int> hashTable = new HashTable<string, int>();
hashTable.Add("Pesho", 6);
Assert.IsTrue(hashTable.Count == 1);
}
示例2: Main
private static void Main()
{
const int elementsCount = 30000;
var rand = new Random();
var beforeHash = GC.GetTotalMemory(true);
var hashTest = new HashTable<int, int>();
var stopWatch = new Stopwatch();
stopWatch.Start();
for (var i = 0; i < elementsCount; i++)
{
var currentNum = rand.Next();
hashTest.Add(currentNum, currentNum);
hashTest.Contains(currentNum);
}
stopWatch.Stop();
var afterHash = GC.GetTotalMemory(true);
Console.WriteLine(
"The time needed to add {0} items in the HashTalbe was {1}",
hashTest.Count,
stopWatch.Elapsed);
Console.WriteLine("Current capacity is {0} items", hashTest.Capacity);
Console.WriteLine("{0} kb used", (afterHash - beforeHash) / 1024);
}
示例3: Main
public static void Main()
{
var table = new HashTable<int, int>();
table.Add(1, 11);
table.Add(2, 12);
table.Add(3, 13);
table.Add(4, 14);
table.Add(5, 15);
table.Add(6, 16);
table.Add(7, 17);
table.Add(8, 18);
//var four = table.Find(4);
Console.WriteLine();
Console.WriteLine(table.Count);
table.Remove(4);
table.Remove(4);
Console.WriteLine();
Console.WriteLine(table.Count);
Console.WriteLine();
var secondfour = table.Find(4);
Console.WriteLine(table.Count);
Console.WriteLine(secondfour);
Console.WriteLine(table[1]);
table[1] = 15;
Console.WriteLine(table[1]);
Console.WriteLine(string.Join(", ", table.Keys));
}
示例4: RemoveShouldReturnFalseIfTheKeyIsNotFound
public void RemoveShouldReturnFalseIfTheKeyIsNotFound()
{
var testHashTable = new HashTable<int, int>();
testHashTable.Set(1, 1);
var removed = testHashTable.Remove(2);
Assert.AreEqual(false, removed);
}
示例5: Main
static void Main()
{
string input = Console.ReadLine();
var phonebook = new HashTable<string, string>();
while (input != "search")
{
string[] record = input.Split( new char[] {'-'}, StringSplitOptions.RemoveEmptyEntries);
phonebook.AddOrReplace(record[0], record[1]);
input = Console.ReadLine();
}
input = Console.ReadLine();
while (input != "")
{
if (phonebook.ContainsKey(input))
{
Console.WriteLine("{0} -> {1}", input, phonebook[input]);
}
else
{
Console.WriteLine("Contact {0} does not exist.", input);
}
input = Console.ReadLine();
}
}
示例6: SetShouldReplaceTheExistingValueWithTheNewOneWhenKeyExists
public void SetShouldReplaceTheExistingValueWithTheNewOneWhenKeyExists()
{
var testHashTable = new HashTable<int, int>();
testHashTable.Set(1, 1);
testHashTable.Set(1, 2);
Assert.AreEqual(2, testHashTable[1]);
}
示例7: HashTableShouldWorkProperlyWithCollectionValues
public void HashTableShouldWorkProperlyWithCollectionValues()
{
var testHashTable = new HashTable<int, List<int>>();
testHashTable.Set(1, new List<int> {1, 2, 3});
Assert.AreEqual(3, testHashTable[1][2]);
}
示例8: AddingByIndexShouldAddCorrectly
public void AddingByIndexShouldAddCorrectly()
{
var table = new HashTable<string, int>();
table["pesho"] = 2;
Assert.AreEqual(2, table["pesho"]);
}
示例9: Main
static void Main()
{
HashTable<string, int> hash = new HashTable<string, int>(32);
hash.Add("Pesho", 6);
hash.Add("Pesho", 6);
hash.Add("Pesho", 3);
hash.Add("Gosho", 9);
hash.Add("Tosho", 4);
hash.Add("Viko", 7);
hash.Add("Viko", 7);
hash.Add("high 5", 7);
hash["Johnatan"] = 3333;
hash["viko"] = 1111111;
hash.Remove("Tosho");
foreach (var item in hash)
{
Console.WriteLine("{0} -> {1}", item.Key, item.Value);
}
hash.Clear();
foreach (var item in hash)
{
Console.WriteLine("{0} -> {1}", item.Key, item.Value);
}
}
示例10: Main
public static void Main()
{
var hashtable = new HashTable<int, string>();
hashtable.Add(5, "five");
hashtable.Add(3, "tree");
hashtable.Add(-6, "minus six");
Console.WriteLine(hashtable.Find(-6));
Console.WriteLine("All elements");
foreach (var item in hashtable)
{
Console.WriteLine("Key: {0} => Value: {1}", item.Key, item.Value);
}
hashtable.Remove(3);
Console.WriteLine("3 removed from table");
try
{
Console.WriteLine("Searching for 3 in table");
hashtable.Find(3);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine("Hash table has {0} elements", hashtable.Count);
}
示例11: Main
/* Task 4:
* Implement the data structure "hash table" in a class HashTable<K,T>.
* Keep the data in array of lists of key-value pairs (LinkedList<KeyValuePair<K,T>>[])
* with initial capacity of 16. When the hash table load runs over 75%, perform resizing to 2
* times larger capacity. Implement the following methods and properties: Add(key, value), Find(key)value,
* Remove( key), Count, Clear(), this[], Keys. Try to make the hash table to support iterating over its elements
* with foreach.
*/
//Few functionalities are missing. If I have time I will add them as well.
static void Main(string[] args)
{
var hashTable = new HashTable<string, int>();
for (int i = 0; i < 100; i++)
{
hashTable.Add("Entri" + i, i);
}
Console.WriteLine(hashTable.Count());
Console.WriteLine(hashTable.Find("Entri50"));
for (int i = 50; i < 100; i++)
{
hashTable.Remove("Entri" + i);
}
//Will throw an exception
//Console.WriteLine(hashTable.Find("Entri50"));
var keys = hashTable.Keys();
foreach (var key in keys)
{
Console.WriteLine(key);
}
hashTable.Clear();
Console.WriteLine(hashTable.Count());
}
示例12: DeleteTest
public void DeleteTest()
{
HashTable ht = new HashTable(35);
ht.AddToHashTable("GUITAR");
ht.DeleteFromHashTable("GUITAR");
Assert.IsFalse(ht.Search("GUITAR"));
}
示例13: Initialize
public void Initialize()
{
hashFunction1 = new HashFunction1();
hashFunction2 = new HashFunction2();
hashTable1 = new HashTable(5, hashFunction1);
hashTable2 = new HashTable(5, hashFunction2);
}
示例14: Main
static void Main(string[] args)
{
HashTable<int, string> table = new HashTable<int, string>();
for (int i = 0; i < 17; i++)
{
table.Add(i, (i + 1).ToString());
}
Console.WriteLine(table[15]);
table.Clear();
for (int i = 0; i < 3000; i++)
{
table.Add(i, (i + 1).ToString());
}
Console.WriteLine(table[1457]);
//table.Add(1, "test");
//table.Add(2, "test");
////table.Remove(1);
//Console.WriteLine(table[1]);
}
示例15: Main
public static void Main()
{
HashTable<int, string> table = new HashTable<int, string>();
table.Add(2, "Pesho");
table.Add(3, "Jhon");
table.Add(67, "Evlogi");
table.Add(23, "Jhon");
table.Add(27, "Pesho");
table.Add(35, "Jhon");
table.Add(66, "Evlogi");
table.Add(11, "Jhon");
table.Add(32, "Pesho");
table.Add(19, "Jhon");
table.Add(12, "Evlogi");
table.Add(13, "Jhon");
table.Add(14, "Pesho");
table.Add(30, "Jhon");
table.Add(44, "Evlogi");
foreach (var item in table)
{
Console.WriteLine(item.Value);
}
}