473,811 Members | 3,532 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Container Class Question

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


Nov 16 '05 #1
6 1549
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.

Nov 16 '05 #2
A.M
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

""Jeffrey Tan[MSFT]"" <v-*****@online.mi crosoft.com> wrote in message
news:MV******** ******@cpmsftng xa10.phx.gbl...
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.

Nov 16 '05 #3
Hi Alan,

Thanks for your feedback.

First, I want to inform you that ArrayList.Binar ySearch 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(10000 0);
al.Add("abcdef" +no);
if(i==50000)
{
searchno=no;
}
}

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

int pos=-1;
pos=al.BinarySe arch("abcdef"+s earchno);
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(10000 0);
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.Co unt;pos++)
{
if(al[pos].Equals("abcdef "+searchno) )
{
resultno=pos;
ts=DateTime.Now-dt;
break;
}
}
MessageBox.Show (ts.ToString()) ;

At last, if you use Hashtable.Conta insValue, 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(10000 0);
ht.Add(i, "abcdef"+no );
if(i==50000)
{
searchno=no;
}
}

DateTime dt=DateTime.Now ;

bool b=ht.ContainsVa lue("abcdef"+se archno);

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.

Nov 16 '05 #4
A.M
Thank you Jeffrey!
""Jeffrey Tan[MSFT]"" <v-*****@online.mi crosoft.com> wrote in message
news:zX******** ******@cpmsftng xa10.phx.gbl...
Hi Alan,

Thanks for your feedback.

First, I want to inform you that ArrayList.Binar ySearch 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(10000 0);
al.Add("abcdef" +no);
if(i==50000)
{
searchno=no;
}
}

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

int pos=-1;
pos=al.BinarySe arch("abcdef"+s earchno);
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(10000 0);
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.Co unt;pos++)
{
if(al[pos].Equals("abcdef "+searchno) )
{
resultno=pos;
ts=DateTime.Now-dt;
break;
}
}
MessageBox.Show (ts.ToString()) ;

At last, if you use Hashtable.Conta insValue, 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(10000 0);
ht.Add(i, "abcdef"+no );
if(i==50000)
{
searchno=no;
}
}

DateTime dt=DateTime.Now ;

bool b=ht.ContainsVa lue("abcdef"+se archno);

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.

Nov 16 '05 #5
Use a hashtable as you said. Fast. Use a case insensitive comparerer
(example in the doco) if case will not matter.

--
William Stacey, MVP

"A.M" <IH*******@sapm 123.com> wrote in message
news:e9******** ******@TK2MSFTN GP11.phx.gbl...
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


Nov 16 '05 #6
Hi A.M,

You are welcome!!

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.

Nov 16 '05 #7

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

Similar topics

6
1573
by: Michael | last post by:
Hi, In real world, when to use class, container, class & container? It seems to me that container is more useful to number chain, class is for implementing complex porjects? Right? Please give your insights. Thanks in advance, Michael
7
20015
by: toton | last post by:
Hi, I want a circular buffer or queue like container (queue with array implementation). Moreover I want random access over the elements. And addition at tail and remove from head need to be low cost. STL vector is suitable for removing form tail? or it is as costly as removing from middle? any other std container to serve this purpose? (note , I dont need linked list implementation of any container, as I want random access)
2
2011
by: Daniel Lipovetsky | last post by:
I would like for an object to "report" to a container object when a new instance is created or deleted. I could have a container object that is called when a new instance is created, as below. class AnyObject: pass class Container: links = def add(self,other):
36
2046
by: Peter Olcott | last post by:
So far the only way that I found to do this was by making a single global instance of the container class and providing access to the contained class, through this single global instance. Are there any other no-overhead ways that a contained class can access its container? The obvious choice of passing (a pointer or a reference to the container) to the contained class is not a no-overhead solution, it requires both memory and time. I am...
4
2375
by: Bit Byter | last post by:
I want to write a (singleton) container for instances of my class templates, however, I am not too sure on how to: 1). Store the instances 2). How to write the acccesor method (instance()) to retrieve an instance of particular template 3). What type to return an instance as .. Assuming I have the following code:
0
9604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7665
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6882
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5552
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4336
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 we have to send another system
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.