Container Class Question

  • Thread starter Thread starter A.M
  • Start date Start date
A

A.M

Hi,

I have a list of strings (40-90 items) and i want to check if a specific
string exists in the list or not.
I can use a hash table, if the table returns null then the item doesn't
exist in the list.

Is it the best way to check existance of an item in a list ?

Thanks,
Alan
 
Hi Alan,

Based on my understanding, you want to determine if a string item exists in
an array.

Actually, you have to loop through this array, then compare to determine if
such a string item exists. Do you have any concern on the checking?

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Jeffrey,

I am looking for an alternative data structure or container class which does
that for me more efficient.

At this I can think of this 2 solution:

1) We can use a HashTable indexer todo that. It returns null if the Table
doesn't contain the object.
2) We can use an ArrayList. It has a method called BinarySearch to find if
it contain an specific object.

Which one is faster? Do I have a better choice ?

Than you,
Alan
 
Hi Alan,

Thanks for your feedback.

First, I want to inform you that ArrayList.BinarySearch method only can
apply to an array that has been sorted. The entire sort is a heavy
operation for you. So it does not meet your need. The below sample code
cost about 2.3~2.5 seconds, which is poor performance:

ArrayList al=new ArrayList();
Random r=new Random();
int no, searchno=-1;
for(int i=0;i<100000;i++)
{
no=r.Next(100000);
al.Add("abcdef"+no);
if(i==50000)
{
searchno=no;
}
}

DateTime dt=DateTime.Now;
al.Sort();

int pos=-1;
pos=al.BinarySearch("abcdef"+searchno);
TimeSpan ts=DateTime.Now-dt;
MessageBox.Show(ts.ToString());

Then, for doing personal search in ArrayList, it costs about 0.25 seconds,
like this:

ArrayList al=new ArrayList();
Random r=new Random();
int no, searchno=-1;
for(int i=0;i<100000;i++)
{
no=r.Next(100000);
al.Add("abcdef"+no);
if(i==50000)
{
searchno=no;
}
}

DateTime dt=DateTime.Now;

string str;
int resultno=-1;
TimeSpan ts=new TimeSpan(0);
for(int pos=0;pos<al.Count;pos++)
{
if(al[pos].Equals("abcdef"+searchno))
{
resultno=pos;
ts=DateTime.Now-dt;
break;
}
}
MessageBox.Show(ts.ToString());

At last, if you use Hashtable.ContainsValue, it will be fastest, about 0.03
seconds, like this:
Hashtable ht=new Hashtable();
Random r=new Random();
int no, searchno=-1;
for(int i=0;i<100000;i++)
{
no=r.Next(100000);
ht.Add(i, "abcdef"+no);
if(i==50000)
{
searchno=no;
}
}

DateTime dt=DateTime.Now;

bool b=ht.ContainsValue("abcdef"+searchno);

TimeSpan ts=DateTime.Now-dt;
MessageBox.Show(ts.ToString());

So if you only want to know if certain item existed in a collection, the
Hashtable may be good for you. But it does not give you a definitely
position number in the collection. If you want the exactly position number,
you still have to do the compare yourself.

All the test is on my machine, which performance is not very well :-)

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Use a hashtable as you said. Fast. Use a case insensitive comparerer
(example in the doco) if case will not matter.
 
Back
Top