473,485 Members | 1,390 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Hashtable Contains() - byte arrays as keys -

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
Nov 16 '05 #1
7 6832
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

"Joseph Lee" <jo*********@hotmail.com> wrote in message
news:ed*************@TK2MSFTNGP12.phx.gbl...
Hi All,

I am having problem when i am using hashtable to keep an array of bytes
value as keys.

Nov 16 '05 #2
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" wrote:
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

Nov 16 '05 #3
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

"Bob Grommes" <bo*@bobgrommes.com> wrote in message
news:#V**************@tk2msftngp13.phx.gbl...
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

"Joseph Lee" <jo*********@hotmail.com> wrote in message
news:ed*************@TK2MSFTNGP12.phx.gbl...
Hi All,

I am having problem when i am using hashtable to keep an array of bytes
value as keys.


Nov 16 '05 #4
ic, i will look into it. Thanks

Joey

"Rakesh Rajan" <Ra*********@discussions.microsoft.com> wrote in message
news:87**********************************@microsof t.com...
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" wrote:
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

Nov 16 '05 #5
Joseph Lee wrote:
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

"Rakesh Rajan" <Ra*********@discussions.microsoft.com> wrote in message
news:87**********************************@microsof t.com...
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" wrote:

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


--
mikeb
Nov 16 '05 #6
But that would again mean he will have to iterate and compare each entries
right? Wonder whether there are any other efficient implementations?
"mikeb" wrote:
Joseph Lee wrote:
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

"Rakesh Rajan" <Ra*********@discussions.microsoft.com> wrote in message
news:87**********************************@microsof t.com...
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" wrote:
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


--
mikeb

Nov 16 '05 #7
Rakesh Rajan wrote:
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.


"mikeb" wrote:

Joseph Lee wrote:
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

"Rakesh Rajan" <Ra*********@discussions.microsoft.com> wrote in message
news:87**********************************@micro soft.com...
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" wrote:

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


--
mikeb

--
mikeb
Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
461
by: Peter | last post by:
Hi how can I compare two byte arrays in VB.NET Thank Peter
7
6730
by: War Eagle | last post by:
I have two byte arrays and a char (the letter S) I was to concatenate to one byte array. Here is what code I have. I basically want to send this in a one buffer (byte array?) through a socket. ...
8
4569
by: frekster | last post by:
Hi. I used to be able to do this easily in vb 6 via looping and preserving the source array data/size etc. How can I do this in vb.net? I've been trying for a while now and this should be...
2
11269
by: Tom | last post by:
What's the best way to compare two byte arrays? Right now I am converting them to base64 strings and comparing those, as so: 'result1 and result2 are two existing byte arrays that have been...
6
2726
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
5
40045
by: Oleg Subachev | last post by:
Is there other way of comparing two byte arrays than iterating through the two and compare individual bytes ? Oleg Subachev
0
823
by: Rob | last post by:
Hi all, Ok - I've been toiling with some encyption - and I think I found why I was having a problem, it was because my encrypt and decrypt functions were using different keys....anyway...putting...
0
1860
by: JonJacobs | last post by:
When I add a series of byte arrays to an array list, then I read them back, all the arraylist byte array elements are identical to the last byte array entry. What is wrong? The following code will...
3
4153
by: RobbSadler | last post by:
This post was meant to go at the end of another post named "How to Compare Two Byte Arrays" which was answered by Vadym Stetsyak, but it was closed. I used his code from that post to create this, and...
0
7090
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6960
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6831
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7287
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5420
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4858
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3060
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3064
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1376
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.