473,480 Members | 2,172 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 21628
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
20276
by: Mark | last post by:
I'm using an enumerator to iterate through a HashTable that contains all numeric keys. I'd like to iterarate through the HashTable based on the ordered keys. Is there a quick way to do this?...
2
24594
by: MFRASER | last post by:
How do I go about looping through a hash table and removing items. I know how do this in a collectionbase, but can't iterate through the hash table with out getting an error. Here is my sample...
8
59500
by: SenthilVel | last post by:
how to get the corresponding values for a given Key in hashtable ??
3
9673
by: Fred | last post by:
I'm trying to build a hashtable and a arraylist as object value I'm not able to retrieve stored object from the hashtable. Hashtable mp = new Hashtable(); // THE HASHTABLE ArrayList...
5
2484
by: Victor Paraschiv | last post by:
I need to serialize into an XML file a hashtable. From MSDN at XmlSerializer:...
5
2674
by: Dick | last post by:
Hello, I'm trying to serialize a class with a Hashtable within: ' Class code: Imports System.Collections Class clsOptions Public countID As Integer Public persons As New Hashtable End Class
4
22793
by: Martin Pöpping | last post by:
Hello, I´m trying to implement a "reverse index table" using a hashtable. But in the hashtable class i cannot find any get-Method. The Java class of hashtable for example provides a method:...
15
3072
by: Macca | last post by:
Hi, My app needs to potentially store a large number of custom objects and be able to iterate through them quickly. I was wondering which data structure would be the most efficient to do this,a...
12
3335
by: =?Utf-8?B?SnVsaWEgQg==?= | last post by:
Hi all I'm creating a hashtable of a list of users in AD. The keys are UserID and Password. I want to loop through the table to compare the userID and password with values that a user has...
0
7049
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6912
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7052
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6744
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5348
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4790
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3000
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2989
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
188
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.