given a Hashtable and an Index, how do I retrieve the key?

  • Thread starter Thread starter Andrew Robinson
  • Start date Start date
A

Andrew Robinson

given a Hashtable and an Index, how do I retrieve the key?

Hashtable h = new Hashtable();
h.Add("red", 3);
h.Add("blue", 99);
h.Add("green", 33);

how do I get the key at index 2?

h.Keys[2] should equal "green".

thanks,

-Andrew
 
Andrew Robinson said:
given a Hashtable and an Index, how do I
retrieve the key?

A Hashtable is not intrinsically ordered. Perhaps you should be using
SortedList.

P.
 
Object LookupFirstKey(IDictionary dict, Object value) {
´ foreach ( IDictionaryEntry e in table )
if ( object.Equals(e.Value, value) )
return e.Value
throw new IndexOutOfBoundsException();
}

If that isn't fast enough, you can maintain two hash-tables, one mapping
each way, and expose that in your own class, kind of like:

public class TwowayHashtable: IDictionary {
IDictionary forward;
IDictionary reverse;
void Add(Object left, Object right) {
forward.Add(left, right);
try {
reverse.Add(left, right);
catch ( Exception e ) {
forward.Remove(left);
throw;
}
}
// Allow choosing either forward or reverse "image" of
// the hashtable
IDictionary Forward { get { return new Image(this, forward); } }
IDictionary Reverse { get { return new Image(this, reverse); } }
class Image: IDictionary {
public TwowayHashtable table;
public IDictionary dict;
public Image(TwowayHashtable table, IDictionary dict)
{ this.table = table; this.dict = dict; }
// Mutation goes to table
public void Add(Object key, Object item) { table.Add(key,item); }
...
// Iteration goes to dict
public IEnumerator GetEnumerator() { return dict.Enumerator(); }
...
// Synchronization goes to table
public Object SyncRoot { get { return table.SyncRoot; } }
...
}
// For iteration, expose any of foreard/reverse
public IEnumarator GetEnumerator() { return forward.Enumerator(); }
public ICollection Keys { get { return forward.Keys; } }
...

}
A Hashtable is not intrinsically ordered. Perhaps you should be using
SortedList.

That wouldn't help, he's asking to to reverse lookup (as i understand it).
 
Damn, read the code before pressing send :)
Object LookupFirstKey(IDictionary dict, Object value) {
´ foreach ( IDictionaryEntry e in table )
if ( object.Equals(e.Value, value) )
return e.Value
^^^^^^^^^^^^^^
return e.Key;
 
Andrew,

You cannot retrieve a value or a key from the Hashtable by specifying
an index. The SortedList will not work either. Using your example the
key in slot 2 of a SortedList would be "red". Plus, you lose the O(1)
insert, delete, and search operations. The following article may be
helpful.

<http://www.codeproject.com/csharp/KeyedList.asp>

Brian
 
Helge Jensen said:
That wouldn't help, he's asking to to reverse lookup (as i understand it).

I think you've missed the point of the OP's question. If you look at
his original post, he's trying to find the key with "index" 2 - meaning
the key of the third entry which was added. As Paul said, hashtables
don't maintain the order of insertion, so the OP can't do what he wants
using just Hashtable.
 
I think you've missed the point of the OP's question. If you look at
his original post, he's trying to find the key with "index" 2 - meaning

Right you are!
 
Back
Top