Collection where I can search by key or value

  • Thread starter Nathan Sokalski
  • Start date
N

Nathan Sokalski

I am looking for a collection where I can find the key based on the value or
the value based on the key. All the classes I have found in the
System.Collections namespace only allow you to get the value based on the
key, but not vice-versa. Is there such a class? I would prefer not to write
my own class or extend one of the collections classes to do this, because I
am only using it one time, but it is the only way I can do what I need to
do. Any help would be appreciated.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Nathan said:
I am looking for a collection where I can find the key based on the value or
the value based on the key. All the classes I have found in the
System.Collections namespace only allow you to get the value based on the
key, but not vice-versa. Is there such a class? I would prefer not to write
my own class or extend one of the collections classes to do this, because I
am only using it one time, but it is the only way I can do what I need to
do. Any help would be appreciated.

I am afraid that you have to build it yourself.

If the values are also unique, you can make a class that contains a
Dictionary<KeyType, ValueType> and a Dictionary<ValueType, KeyType>. If
the values are not unique, you can use a Dictionary<ValueType,
List<KeyType>> and add the keys for the same value in the list.
 
C

Cor Ligthert[MVP]

Nathan,

I have once used two classes to realise this, one extra to find the key.

If you update them in the same method, than it is a piece of cake.

Cor
 
M

Michel Posseth [MCP]

AFAIK
the hashtable object poseses these requested features , it is also verry
fast in iterations

michel
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Michel said:
To be more precise

http://msdn2.microsoft.com/en-us/library/system.collections.hashtable(vs.80).aspx

also note on the left the methods section

you wil discover there the containskey and containsvalue methods

I almost never use "normall" collections the Hashtable is the most efective
in my opinion ( especially for large quntity`s of data it is almost
unbeatable in speed )

regards

Michel Posseth

There is a huge speed difference between the ContainsKey and
ContainsValue methods, though. ContainsKey approaches an O(1) operation,
while ContainsValue is an O(n) operation, where n is Count.

In other words, locating by key is a fast operation and the speed varies
very little depending on the number of items. Locating by value is a
slow operation, as it has to iterate through the values, so the average
search time is proportional to the size of the collection.
 
M

Michel Posseth [MCP]

Yes true ,

If this is a problem for the TS , there is always the possibility of 2
hashtables :)

Michel
 
G

Guest

I am looking for a collection where I can find the key based on the
value or the value based on the key. All the classes I have found in
the System.Collections namespace only allow you to get the value based
on the key, but not vice-versa. Is there such a class? I would prefer
not to write my own class or extend one of the collections classes to
do this, because I am only using it one time, but it is the only way I
can do what I need to do. Any help would be appreciated.

..NET 2.0 Generics do have a find function you can use.

You can attach your own delegate to the find function and it will search
based on your criteria.
 
B

Brian Gideon

I am looking for a collection where I can find the key based on the value or
the value based on the key. All the classes I have found in the
System.Collections namespace only allow you to get the value based on the
key, but not vice-versa. Is there such a class? I would prefer not to write
my own class or extend one of the collections classes to do this, because I
am only using it one time, but it is the only way I can do what I need to
do. Any help would be appreciated.

What are you speed requirements for both searches?
 
N

Nathan Sokalski

As long as it's less than about 5 seconds (and since the number of
keys/values that I have is pretty short, I don't think that's a problem). I
would not have a problem writing code that iterates through them, it is not
a complicated process. My reason for looking for an existing class that has
this feature is so that I don't have to write an extra function that I will
be using so little of. Although after hearing everything so far from this
thread, I have a bit of a feeling that there currently is no such class. The
only thing missing from most classes that would make them what I need is a
FindByValue() function. I will probably end up writing my own function for
now, and hope that someday I find a class that meets my needs (hopefully
there is one that exists, because I doubt that I'm the only one that has
ever needed this functionality).
 
C

Cor Ligthert[MVP]

Nathan,

As I said, two classes, and yes I have used the findby in that.
(I thought about 6 lines of code extra above one class, you can use the
second class overloaded to call the methods of the first one.)

Cor
 
M

Michel Posseth [MCP]

Huh ??? :-(
As long as it's less than about 5 seconds (and since the number of
keys/values that I have is pretty short, I don't think that's a problem).

The hashtable object sounds then perfect in your situation , it would only
become slow with a few hundred thousands of items stored ( i even used them
with millions of items ) if you would search on a value instead of an key

in the case of a key it is unbeatable in speed ( Balena did some nice tests
to prove this )

In this case you would not need to write anny line of code since the methods
are built in to the hashtable object type


Regards
Michel
 
B

Brian Gideon

Nathan,

As I said, two classes, and yes I have used the findby in that.
(I thought about 6 lines of code extra above one class, you can use the
second class overloaded to call the methods of the first one.)

Cor

There's no need for two classes here. The Dictionary collection
already has the ContainsKey and ContainsValue methods. ContainsValue
should return within the time constraints the OP provided unless there
are a *lot* of items in the collection.
 

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