Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hashtable 1

Status
Not open for further replies.

stevenk140

Programmer
Nov 20, 2003
34
CA
I have a hashtable and a value that matches up with a key. Is there an easy way of getting the key from the value? (Without search through all values to find the index)

Or is a SortedList better from going back for forward between keys and values?

Steven
 
Hi,

You can get the value from the Hashtable if you know the key name without going through the loop.

Hashtable ht = new Hashtable();

ht.Add("Key1", "Value1");
ht.Add("Key2", "Value2");
ht.Add("Key3", "Value3");
ht.Add("Key4", "Value4");
ht.Add("Key5", "Value5");

Console.WriteLine(h.Item("Key3"));

Before that just make sure the key contins in the Hashtable.
with h.ContainsKey("Key3")


Cheers
Venu
 
Hello,

What if I have the value and want to get the key? (and I don't want to iterate through all values to find the matching key index)

Steven
 
stevenk140 -

In that case you would have to iterate through all the entries in your hashtable.

Have you considered reversing what you use as keys? Or maybe have two hashtables - one with the key as the key, and one with the value as the key?

Of course, depending on how many values you need to store, and how fast you need access to them, a database table might be a good solution.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
As chiph said you should implement your class with two HashTable members: one to map key->value direction and another to map value->key direction.
Implement in the new class functions like Lookup for the key->value direction and ReverseLookup for the value->key direction e.g. mainly what you want.
Your new class should be derived minimum from ICollection, and IEnumerable.
Here is a starting example:
Code:
public class MyHashtable : IDictionary, ICollection, IEnumerable
{
   private Hashtable m_htKey2Val = null;
   private Hashtable m_htVal2Key = null;
   public MyHashtable ()
  {
	m_htKey2Val = new Hashtable();
	m_htVal2Key = new Hashtable();
  }
   private MyHashtable (Hashtable ht)
	{
		m_htKey2Val = ht;
		m_htVal2Key = new Hashtable();
		foreach( object key in ht.Keys )
		{
			m_htVal2Key [ht[key]] = key;
		}
	}
public void Add( object key, object val )
	{
		m_htKey2Val.Add( key, val );
		m_.htVal2Key. Add( val, key );
	}

public void Remove( object key )
	{
		object val = m_htVal2Key [key];
		m_htKey2Val .Remove( key );
		m_htVal2Key .Remove( val );
	}
//This ReverseLookup will do the job.

public object ReverseLookup( object val )
	{
		return m_htVal2Key [val];
	}
 // .... 
}
Hashtable h1=new Hasttable();
h1.Add("k1","George");
MyHasTable hh= new MyHashtable(h1);
string s1 = (string)hh.ReverseLookup("George");
-obislavu-

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top