looking for somehting like a hashing table, with only KEYS

R

Ron M. Newman

Hi group,

I need similar functionality I'm getting from the hashtable, but without the
value .all I need is keys. this of course negates the need for "hashing",
but I wanted to know if there's a class for that in C#.

I need to:

- Set keys
- look for the existence of keys
- remove keys.

anything for this purpose?

- Ron
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I'm not clear of what you want, if you just want to hold a series of values
( the "keys" ) then you can use any collection.

Maybe a little more details will be helpful
 
N

Nicholas Paldino [.NET/C# MVP]

Ron,

Why not just use a boolean value of "true", or use null. That's what I
do when I need this functionality.

If I had to guess, I would say you are looking for a way to prevent
duplicates.

Hope this helps.
 
R

Ron M. Newman

Hi,

It's really simple. I need to maintain a list of objects. I want to search
of the list of the objects (using other objects) and remove them at will.

sort of like using a hashtable, only without values, I do not need those.

Ron
 
B

Barry Kelly

Ron M. Newman said:
I need similar functionality I'm getting from the hashtable, but without the
value .all I need is keys. this of course negates the need for "hashing",
but I wanted to know if there's a class for that in C#.

It sounds like you want something that implements the Java Set
interface, but with hash performance - i.e. like Java HashSet. There
isn't an exact match. Like the others say, use some arbitrary value -
null with Hashtable, bool or object with Dictionary<,>.

-- Barry
 
C

cp

Ron said:
Hi group,

I need similar functionality I'm getting from the hashtable, but without the
value .all I need is keys. this of course negates the need for "hashing",
but I wanted to know if there's a class for that in C#.

I need to:

- Set keys
- look for the existence of keys
- remove keys.

anything for this purpose?

- Ron

You could use ArrayList with something like:

ArrayList alist = new ArrayList();
alist.Add(key); // add key
bool found = alist.Contains(key); // look for key
alist.Remove(key); // remove key

... or better still you could derive you own class.

Hope that helps.

cp
 
C

cp

Nicholas said:
Ron,

Why not just use a boolean value of "true", or use null. That's what I
do when I need this functionality.

If I had to guess, I would say you are looking for a way to prevent
duplicates.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ron M. Newman said:
Hi group,

I need similar functionality I'm getting from the hashtable, but without
the value .all I need is keys. this of course negates the need for
"hashing", but I wanted to know if there's a class for that in C#.

I need to:

- Set keys
- look for the existence of keys
- remove keys.

anything for this purpose?

- Ron

there are 3 types of collections in .net, ordered (stack/queue),
indexed (arraylist) and keyed (hashtable).
the indexed collections are based on an IList whereas the keyed ones
are based on an IDictionary. the ordered ones are based on ICollection.

aside from using arraylist directly you could also create your own
class and implement the ilist members - that would give you a type-safe
version for your keys. arraylist would let you pretty much add whatever
you like!

cp
 
N

Nicholas Paldino [.NET/C# MVP]

True, but it's a VERY, VERY bad idea to use an ArrayList when you have
the typed information, since it is not type-safe.

Also, an ArrayList isn't the best bet here, depending on the nature of
the keys. If you have a tremendous number of keys, and ArrayList is a bad
choice since you will have to iterate through the list to perform a lookup,
which could have a perf impact.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

cp said:
Ron,

Why not just use a boolean value of "true", or use null. That's what
I
do when I need this functionality.

If I had to guess, I would say you are looking for a way to prevent
duplicates.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ron M. Newman said:
Hi group,

I need similar functionality I'm getting from the hashtable, but
without
the value .all I need is keys. this of course negates the need for
"hashing", but I wanted to know if there's a class for that in C#.

I need to:

- Set keys
- look for the existence of keys
- remove keys.

anything for this purpose?

- Ron

there are 3 types of collections in .net, ordered (stack/queue),
indexed (arraylist) and keyed (hashtable).
the indexed collections are based on an IList whereas the keyed ones
are based on an IDictionary. the ordered ones are based on ICollection.

aside from using arraylist directly you could also create your own
class and implement the ilist members - that would give you a type-safe
version for your keys. arraylist would let you pretty much add whatever
you like!

cp
 
A

adebaene

Ron M. Newman a écrit :
Hi group,

I need similar functionality I'm getting from the hashtable, but without the
value .all I need is keys. this of course negates the need for "hashing",
but I wanted to know if there's a class for that in C#.

I need to:

- Set keys
- look for the existence of keys
- remove keys.

anything for this purpose?

- Do you forbid duplicates?
- Do you need the data to be sorted (for faster lookup) ?

Those are 2 properties of Hashtable (although a Hashtable is sorted on
hash value, not on key value), but you didn't say wether they are
important for you.

If they aren't , an ArrayList is what you want.

Arnaud
MVP - VC
 
C

cp

Nicholas said:
True, but it's a VERY, VERY bad idea to use an ArrayList when you have
the typed information, since it is not type-safe.

Also, an ArrayList isn't the best bet here, depending on the nature of
the keys. If you have a tremendous number of keys, and ArrayList is a bad
choice since you will have to iterate through the list to perform a lookup,
which could have a perf impact.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

cp said:
Ron,

Why not just use a boolean value of "true", or use null. That's what
I
do when I need this functionality.

If I had to guess, I would say you are looking for a way to prevent
duplicates.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hi group,

I need similar functionality I'm getting from the hashtable, but
without
the value .all I need is keys. this of course negates the need for
"hashing", but I wanted to know if there's a class for that in C#.

I need to:

- Set keys
- look for the existence of keys
- remove keys.

anything for this purpose?

- Ron

there are 3 types of collections in .net, ordered (stack/queue),
indexed (arraylist) and keyed (hashtable).
the indexed collections are based on an IList whereas the keyed ones
are based on an IDictionary. the ordered ones are based on ICollection.

aside from using arraylist directly you could also create your own
class and implement the ilist members - that would give you a type-safe
version for your keys. arraylist would let you pretty much add whatever
you like!

cp

..net generics can be used to make a type-safe arraylist (or a custom
IList implementation as mentioned).
I'm not sure about performance but the Contains method works fairly
quickly on several thousand entries.
collections and type safety in earlier versions of c# never really was
state of the art.

cp
 
T

Tom Spink

Ron said:
Hi,

It's really simple. I need to maintain a list of objects. I want to search
of the list of the objects (using other objects) and remove them at will.

sort of like using a hashtable, only without values, I do not need those.

Ron

Hi Ron,

How about List<T> (.NET 2.0), is that what you want?

List<T>.Add() will add a value
List<T>.Contains() check to see if a value exists
List<T>.Remove() will remove a value.
 
B

Bruce Wood

Ron said:
Hi group,

I need similar functionality I'm getting from the hashtable, but without the
value .all I need is keys. this of course negates the need for "hashing",
but I wanted to know if there's a class for that in C#.

I need to:

- Set keys
- look for the existence of keys
- remove keys.

anything for this purpose?

When this has happened to me, the keys were reference types (either
strings or class instances). In that case, I just hashed the same value
as the key and the value, like this:

myHashTable.Add(keyObject, keyObject);

It doesn't use up any more space, as both the key and the value contain
a reference (pointer) to the same object.

If the keys are value types then I don't recommend this because it will
result in the value being boxed twice: once for the key and once for
the value. In this case I would probably use null for the value; I
believe that Hashtable allows null values. If not, just instantiate a
dummy object of some type and add it as the value every time.

As several posters pointed out, you can encapsulate this whole thing
inside your own, simpler class that hides the fact that you're abusing
Hashtable slightly. :)
 
B

Bill Butler

Ron M. Newman said:
Hi group,

I need similar functionality I'm getting from the hashtable, but
without the value .all I need is keys. this of course negates the need
for "hashing", but I wanted to know if there's a class for that in C#.
<snip>

You are mistaken...it is precisely the hashing that you need. That is
the part that gives rapid search.

You could use an ArrayList and call BinarySearch() to find elements but,
inserts and deletes are not as speedy as a Hashtable.
Personally I would stick with a Hashtable and simply stick the key in
the value (or put whatever you want in there).
- Set keys
hash[key] = key;
- look for the existence of keys
if (hash.ContainsKey(key))
Foo(key);
- remove keys.
hash.Remove(key);

Bill
 
S

Sjaakie

Ron M. Newman schreef:
Hi group,

I need similar functionality I'm getting from the hashtable, but without the
value .all I need is keys. this of course negates the need for "hashing",
but I wanted to know if there's a class for that in C#.

I need to:

- Set keys
- look for the existence of keys
- remove keys.

anything for this purpose?

- Ron

I would use an ArrayList.
 
K

ktrvnbq02

Ron said:
Hi group,

I need similar functionality I'm getting from the hashtable, but without the
value .all I need is keys. this of course negates the need for "hashing",
but I wanted to know if there's a class for that in C#.

I need to:

- Set keys
- look for the existence of keys
- remove keys.

anything for this purpose?

- Ron

Two possibilities:

Wintellect PowerCollections (.NET 2.0 only)
http://www.wintellect.com/MemberOnly/PowerCollections.aspx

This provides many different types of fast collection objects -- you'll
need to register at the site to download.

Other than that, as others have said, use a Hashtable or Dictionary<>
in .NET 2.0 and add values against a dummy object:

private static readonly DummyObject = new object();

....

myHashtable.Add(val, DummyObject);


I've used this method before when needing to add objects to a
collection and quickly test for presence etc. Note that adding against
a value type such as a bool would incur a boxing penalty unless you're
using generics, so don't do that.


regards,

Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top