473,382 Members | 1,376 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 6822
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
by: Peter | last post by:
Hi how can I compare two byte arrays in VB.NET Thank Peter
7
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
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
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
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
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
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
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.