Hashtable Contains() - byte arrays as keys -

  • Thread starter Thread starter Joseph Lee
  • Start date Start date
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
 
You are correct; any array or collection object would test for the object
reference, not the contents. A match of an arbitrary number of items in a
collection would be cost-prohibitive.

Why do you need to match a byte array? What are you trying to accomplish?
Maybe there's a different approach.

--Bob
 
As Bob said, it's the reference that is being tested and not the contents. If
I remember right, searching in the lines of overriding Equals and GetHashCode
might help. Sorry, I can't recollect them now...
 
I have a list of encrypted keywords and all of them are in an array of byte.
So I am trying to keep those keyword as keys in a hashtable for fast lookup,
then i will be able retrieve some information which is in the value part of
the hashtable

Joey
 
ic, i will look into it. Thanks

Joey

Rakesh Rajan said:
As Bob said, it's the reference that is being tested and not the contents. If
I remember right, searching in the lines of overriding Equals and GetHashCode
might help. Sorry, I can't recollect them now...


Joseph Lee said:
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
 
Joseph said:
ic, i will look into it. Thanks

You can create your Hashtable with custom Hashcode provider and comparer.

Look at the Hashtable( IHashCodeProvider, IComparer) constructor.

With a Hashtable created with that constructor you can provide your own
methods for comparing the byte arrays instead of using the default
methods provided by the Array class.
Joey

As Bob said, it's the reference that is being tested and not the contents.
If

I remember right, searching in the lines of overriding Equals and
GetHashCode

might help. Sorry, I can't recollect them now...


:

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();
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
 
But that would again mean he will have to iterate and compare each entries
right? Wonder whether there are any other efficient implementations?


mikeb said:
Joseph said:
ic, i will look into it. Thanks

You can create your Hashtable with custom Hashcode provider and comparer.

Look at the Hashtable( IHashCodeProvider, IComparer) constructor.

With a Hashtable created with that constructor you can provide your own
methods for comparing the byte arrays instead of using the default
methods provided by the Array class.
Joey

As Bob said, it's the reference that is being tested and not the contents.
If

I remember right, searching in the lines of overriding Equals and
GetHashCode

might help. Sorry, I can't recollect them now...


:


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
 
Rakesh said:
But that would again mean he will have to iterate and compare each entries
right? Wonder whether there are any other efficient implementations?

Assuming I understand his problem (that he needs the byte arrays to
perform a 'deep' compare of the contents of the arrays), then yes -
these interface implementations would need to generate a hash value that
was dependent on the contents of the byte array and to compare the
entries of the arrays.

The check for equality could certainly have some short cuts so that a
member-by-member comparison might not always be needed:

1) if there's reference equality, the arrays are by definition equal
2) if the array lengths are different, then the arrays must not be
equal.

The hashcode might be cacheable if the byte arrays are not changed once
they are created. Indeed, once the byte arrays are added to the
hashtable, it would be a bug to modify them in such a way that the
hashcode (or equality test) would render different results.

There may be other techniques that can improve the efficiency
(particularly if unsafe code can be used). However, my main point was
that there's a way for the OP to get the behavior he needs without too
much work.

Performance is of no consequence if things don't even work correctly in
the first place.
:

Joseph said:
ic, i will look into it. Thanks

You can create your Hashtable with custom Hashcode provider and comparer.

Look at the Hashtable( IHashCodeProvider, IComparer) constructor.

With a Hashtable created with that constructor you can provide your own
methods for comparing the byte arrays instead of using the default
methods provided by the Array class.

Joey



As Bob said, it's the reference that is being tested and not the contents.

If


I remember right, searching in the lines of overriding Equals and

GetHashCode


might help. Sorry, I can't recollect them now...


:



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
 
Back
Top