469,327 Members | 1,159 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

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

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


Nov 16 '05 #1
6 20773
"Andrew Robinson" <ne****@hotmail.com> wrote:
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.
Nov 16 '05 #2
Paul E Collins wrote:
"Andrew Robinson" <ne****@hotmail.com> wrote:
given a Hashtable and an Index, how do I
retrieve the key?


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).

--
Helge
Nov 16 '05 #3
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;
throw new IndexOutOfBoundsException();

Nov 16 '05 #4
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

Nov 16 '05 #5
Helge Jensen <he**********@slog.dk> wrote:
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).


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Jon Skeet [C# MVP] wrote:
Helge Jensen <he**********@slog.dk> wrote:
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).

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!

--
Helge
Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mark | last post: by
2 posts views Thread by MFRASER | last post: by
8 posts views Thread by SenthilVel | last post: by
3 posts views Thread by Fred | last post: by
5 posts views Thread by Victor Paraschiv | last post: by
5 posts views Thread by Dick | last post: by
4 posts views Thread by Martin Pöpping | last post: by
15 posts views Thread by Macca | last post: by
12 posts views Thread by =?Utf-8?B?SnVsaWEgQg==?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.