473,387 Members | 1,673 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,387 software developers and data experts.

Remove from Hashtable? [easy one]

I'm often in a situation where I have a Hashtable full of objects. I'm
then presented with an object and *if* that object exists in the Hashtable,
I need to remove it from the table. I therefore have a lot of code that
looks like this:

if(ht.Contains(obj)){
ht.Remove(obj);
}

It strikes me that this code is somewhat inefficient because the
Hasthtable has to be searched *twice* : once for the Contains() call and
once for the Remove() call. So I thought maybe I should approach the
situation this way:

try{
ht.Remove(obj);
}
catch(Exception){}

Now I'm only searching the Hashtable once... but is the overhead of
throwing the exception cancelling out any benefit that I achieved by not
calling Contains()? To answer this question, you probably need to know how
frequently 'obj' is actually found in the Hashtable. For the sake of
argument, let's say 50%.

Thanks,

Jules
Nov 17 '05 #1
3 2337
Thanks for the tips, guys. I won't concern myself with trivial
optimizations like this unless I see that there's a performance bottleneck.

Jules
Nov 17 '05 #2
Jules-

i am confused at what you're doing here.

According to everything in my experience (including reading reading the
docs), Hashtable.Remove only throws an exception if the argument is null, the
table is read-only, or the table has a fixed size.

A Simple Element-Not-Found does NOT throw an exception.

Just Remove away!

now... if you were trying to properly catch all exceptions, then you should
have also looked for ArgumentNullException as well as NotSupportedException,
but that's immaterial to the examples you've presented.

No performance penalty at all =)
"Jules Winfield" wrote:
I'm often in a situation where I have a Hashtable full of objects. I'm
then presented with an object and *if* that object exists in the Hashtable,
I need to remove it from the table. I therefore have a lot of code that
looks like this:

if(ht.Contains(obj)){
ht.Remove(obj);
}

It strikes me that this code is somewhat inefficient because the
Hasthtable has to be searched *twice* : once for the Contains() call and
once for the Remove() call. So I thought maybe I should approach the
situation this way:

try{
ht.Remove(obj);
}
catch(Exception){}

Now I'm only searching the Hashtable once... but is the overhead of
throwing the exception cancelling out any benefit that I achieved by not
calling Contains()? To answer this question, you probably need to know how
frequently 'obj' is actually found in the Hashtable. For the sake of
argument, let's say 50%.

Thanks,

Jules

Nov 17 '05 #3
> A Simple Element-Not-Found does NOT throw an exception.

Ha! You're right!

I've been calling Contains() before removing since the tech preview of the
original .NET back in like 2000 or 2001 or so. I've been doing it because I
read in a tutorial that you were supposed to do this. Either the tutorial
was completely wrong or the beta version of .NET that I was using back then
actually did throw an exception when Remove() was called with an keythat
doesn't exist in the Hashtable. It's funny how a bad habit learned back in
2001 stuck with me all of these years.

Despite the moot point re:Remove(), a similar issue caught my attention
recently with the IDictionary<T> Item (that is, [] indexer) property. It
throws an exception when the key isn't found, unlike it's non-generic
counterpart which simply returns null. It looks like Microsoft addressed the
issue with a new method called TryGetValue(), which acts a lot like the []
indexer but won't throw an exception when the key isn't found.

Jules
Nov 17 '05 #4

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

Similar topics

1
by: Chandu | last post by:
Is there a way to optimize the use of Hashtable in java in terms of Memory and lookups what exactly are collisions in a hastable and how does it affect performance. Any help would be greatly...
6
by: Jules Winfield | last post by:
I'm often in a situation where I have a Hashtable full of objects. I'm then presented with an object and *if* that object exists in the Hashtable, I need to remove it from the table. I therefore...
2
by: William Stacey [MVP] | last post by:
I am doing a dns zone type of project which has an inverted tree like you all know. test.com. (top node. value contains arraylist of all test.com records) www.test.com. (node. value contains...
2
by: MFRASER | last post by:
How do I go about looping through a hash table and removing items. I know how do this in a collectionbase, but can't iterate through the hash table with out getting an error. Here is my sample...
4
by: Anders Borum | last post by:
Hello! I'm am currently working on making a central cache component threadsafe, and was looking at the synchronized implementation of the Hashtable. I was wondering why you'd really want to...
8
by: SenthilVel | last post by:
how to get the corresponding values for a given Key in hashtable ??
7
by: Kalyani | last post by:
Hi, I have a web service method which needs to return a hashtable.But it gives errror as, The type System.Collections.Hashtable is not supported because it implements IDictionary. Is there...
2
by: D. Shane Fowlkes | last post by:
I've been reading up on Arrays in ASP.NET. I'm going to create an two dimensional array of some type to contain 5 columns but a variable amount of rows. I read up on the ArrayList function and...
10
by: pamelafluente | last post by:
Hi I have a sorted list with several thousands items. In my case, but this is not important, objects are stored only in Keys, Values are all Nothing. Several of the stored objects (might be a...
4
by: Mark S. | last post by:
Much to my surprised the code below compiled and ran. I just don't know enough about threading to know for sure if this is too good to be true. I'm attempting to isolate the Hashtable lock to...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.