J
Joseph Lee
Hi All,
I am having problem when i am using hashtable to keep an array of bytes
value as keys.
Take a look at the code snippet below
---------------------------------------------------
ASCIIEncoding asciiEncoder = new ASCIIEncoding();
byte[] bArray = asciiEncoder.GetBytes("Test");
Hashtable ht = new Hashtable();
ht.Add(bArray,"Some value");
byte[] bArrayNew = asciiEncoder.GetBytes("Test");
Console.WriteLine("Result : "+ ht.Contains(bArray)); //true
Console.WriteLine("Result : "+ ht.Contains(bArrayNew)); //false
Console.Read();
--------------------------------------------------
As seen here, by using the same object , the contains() will return true,
while using a new object with same 'value' returns false. If I am correct
the contains() command does not look at the values in an object. As for
primitive types like normal string, int and etc, it does not have any
problem.
So I was wondering if there is anyway i can keep byte arrays as keys in
hashtable and still find it in the time complexity of O(1). If i read all
the keys out and make a byte comparison it would be O(n) time complexity,
thus defeating the purpose of a hash table.
Is there any other command in hashtable or some functions i can overwrite?
Would prefer a much simpler way if possible.
Thanks in advance
Joseph Lee
I am having problem when i am using hashtable to keep an array of bytes
value as keys.
Take a look at the code snippet below
---------------------------------------------------
ASCIIEncoding asciiEncoder = new ASCIIEncoding();
byte[] bArray = asciiEncoder.GetBytes("Test");
Hashtable ht = new Hashtable();
ht.Add(bArray,"Some value");
byte[] bArrayNew = asciiEncoder.GetBytes("Test");
Console.WriteLine("Result : "+ ht.Contains(bArray)); //true
Console.WriteLine("Result : "+ ht.Contains(bArrayNew)); //false
Console.Read();
--------------------------------------------------
As seen here, by using the same object , the contains() will return true,
while using a new object with same 'value' returns false. If I am correct
the contains() command does not look at the values in an object. As for
primitive types like normal string, int and etc, it does not have any
problem.
So I was wondering if there is anyway i can keep byte arrays as keys in
hashtable and still find it in the time complexity of O(1). If i read all
the keys out and make a byte comparison it would be O(n) time complexity,
thus defeating the purpose of a hash table.
Is there any other command in hashtable or some functions i can overwrite?
Would prefer a much simpler way if possible.
Thanks in advance
Joseph Lee