473,396 Members | 1,836 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Accessing a spezific key in a Hashtable

Hello,

I´ve implemented a Hashtable with Int-Keys and Double Values.

Now I want to get the i-th Int-Key of my hashtable.
How do I do that?

I tried it like that:

ICollection intKeys = myHashtable.Keys;
int key = intKeys[i];

But this does not work, because I cannot access an ICollection like an
array with [].

So how do I do it?
Regards,

Martin
Oct 4 '06 #1
8 2057
Martin Pöpping wrote:
Hello,

I´ve implemented a Hashtable with Int-Keys and Double Values.

Now I want to get the i-th Int-Key of my hashtable.
How do I do that?

I tried it like that:

ICollection intKeys = myHashtable.Keys;
int key = intKeys[i];

But this does not work, because I cannot access an ICollection like an
array with [].
Well, first of all you have to identify what you mean by the i-th key.
Hashtable has no such concept: the entries are not in any particular
order.

If you want to enumerate across the entire collection, you can use
foreach with myHashtable, myHashtable.Keys, or myHashtable.Values.

If you're expecting the keys to be in particular order (say, numerical
order by integer key) then use a SortedList, not a Hashtable, and you
will be able to use the [] notation.

If neither of these is your case, then please explain why you want the
i-th key, and what you expect that will return to you.

Oct 4 '06 #2
Bruce Wood schrieb:
If neither of these is your case, then please explain why you want the
i-th key, and what you expect that will return to you.
My int keys consist of a random order of int (1325,2313,2335).
The problem is now, that I want to have for example the 2nd value (2313).

But you are right... I think this would not work with a hashtable
because of the hashing order.

Anyway... thank you very much!
Regards,

Martin

Oct 4 '06 #3
Martin Pöpping wrote:
Bruce Wood schrieb:
If neither of these is your case, then please explain why you want the
i-th key, and what you expect that will return to you.

My int keys consist of a random order of int (1325,2313,2335).
The problem is now, that I want to have for example the 2nd value (2313).

But you are right... I think this would not work with a hashtable
because of the hashing order.
Then use System.Collections.SortedList. SortedList is just a Hashtable
that also maintains the entries sorted by key value. You can do all of
the hashing you used to do, plus index into the i-th entry in order by
key, which is what you want to do.

Oct 4 '06 #4
If this is what you want to do, then a HashTable isn't the datastructure
you're looking for.

There is no concept of "order" in a hash table. To access the "i-th" key,
you would expect this to be the 3rd key/value pair added into the hashtable.
You could also reasonably expect the "i-th" key to be the 3rd smallest
integer in the hashtable (aka: A sorted valued). The hashtable, however, has
no concept of this.

You should look at other datastructures, or using a few datastructures in
conjunction with each other, to solve your problem.

The Sorted List may be more what you're looking for.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins

"Martin Pöpping" <ma******@despammed.comwrote
>
I´ve implemented a Hashtable with Int-Keys and Double Values.

Now I want to get the i-th Int-Key of my hashtable.
How do I do that?

I tried it like that:

ICollection intKeys = myHashtable.Keys;
int key = intKeys[i];

But this does not work, because I cannot access an ICollection like an
array with [].

So how do I do it?
Regards,

Martin

Oct 4 '06 #5
Martin,

I could be wrong, but what it sounds like to me is that you want a
dictionary (meaning key-value pair) that preserves temporal ordering.
If that's the case then you'll have to whip up your own in 1.1 or use
the KeyedCollection in 2.0.

Brian

On Oct 4, 5:43 pm, Martin Pöpping <marti...@despammed.comwrote:
My int keys consist of a random order of int (1325,2313,2335).
The problem is now, that I want to have for example the 2nd value (2313).

But you are right... I think this would not work with a hashtable
because of the hashing order.

Anyway... thank you very much!

Regards,

Martin
Oct 4 '06 #6
Bruce Wood <br*******@canada.comwrote:
>
Then use System.Collections.SortedList. SortedList is just a Hashtable
that also maintains the entries sorted by key value. You can do all of
the hashing you used to do, plus index into the i-th entry in order by
key, which is what you want to do.
I do believe you lose the constant lookup times of a hashtable when using a
sorted list; if that is important to you.

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
Oct 5 '06 #7
Thomas T. Veldhouse <ve*****@yahoo.comwrote:
Bruce Wood <br*******@canada.comwrote:
>>
Then use System.Collections.SortedList. SortedList is just a Hashtable
that also maintains the entries sorted by key value. You can do all of
the hashing you used to do, plus index into the i-th entry in order by
key, which is what you want to do.

I do believe you lose the constant lookup times of a hashtable when using a
sorted list; if that is important to you.
It appears that I was incorrect.

"The SortedList generic class is a binary search tree with O(log n) retrieval,
where n is the number of elements in the dictionary. In this, it is similar to
the SortedDictionary generic class. The two classes have similar object
models, and both have O(log n) retrieval. Where the two classes differ is in
memory use and speed of insertion and removal:

SortedList uses less memory than SortedDictionary.

SortedDictionary has faster insertion and removal operations for unsorted
data, O(log n) as opposed to O(n) for SortedList.

If the list is populated all at once from sorted data, SortedList is faster
than SortedDictionary."

However, I am correct in indicating that simply using a Dictionary (or
Hashtable) will be much faster than using a SortedList.

"The Dictionary generic class provides a mapping from a set of keys to a set
of values. Each addition to the dictionary consists of a value and its
associated key. Retrieving a value by using its key is very fast, close to
O(1), because the Dictionary class is implemented as a hash table."

O(1) is much faster than O(log n) or O(n). Lookup time is constant in all
cases, but with the SortedLists it is based upon the size of the list.

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
Oct 5 '06 #8

Thomas T. Veldhouse wrote:
Thomas T. Veldhouse <ve*****@yahoo.comwrote:
I do believe you lose the constant lookup times of a hashtable when using a
sorted list; if that is important to you.

It appears that I was incorrect.
No, you're right. Hashtable and Dictionary have average-case
retrieval, insertion, and deletion complexity near O(1) and a
SortedList does not. You even said as much at the end your post :)
"The SortedList generic class is a binary search tree with O(log n) retrieval,
where n is the number of elements in the dictionary. In this, it is similar to
the SortedDictionary generic class. The two classes have similar object
models, and both have O(log n) retrieval. Where the two classes differ is in
memory use and speed of insertion and removal:

SortedList uses less memory than SortedDictionary.

SortedDictionary has faster insertion and removal operations for unsorted
data, O(log n) as opposed to O(n) for SortedList.

If the list is populated all at once from sorted data, SortedList is faster
than SortedDictionary."

However, I am correct in indicating that simply using a Dictionary (or
Hashtable) will be much faster than using a SortedList.

"The Dictionary generic class provides a mapping from a set of keys to a set
of values. Each addition to the dictionary consists of a value and its
associated key. Retrieving a value by using its key is very fast, close to
O(1), because the Dictionary class is implemented as a hash table."

O(1) is much faster than O(log n) or O(n). Lookup time is constant in all
cases, but with the SortedLists it is based upon the size of the list.
For what's worth, SortedList is implemented with an array,
SortedDictionary with a red-black binary search tree (probably
top-down), Dictionary with chaining for collision resolution, and
Hashtable with rehashing for collision resolution.

Oct 5 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: crisp99 | last post by:
Hi, I have a SQL Stored Procedure : CREATE PROCEDURE spFilterOne @city varchar(25) AS SELECT * FROM tblCities WHERE tblCities.strCity = @city ORDER BY tblCities.strName
5
by: francois | last post by:
First of all I would to to apologize for resending this post again but I feel like my last post as been spoiled Here I go for my problem: Hi, I have a webservice that I am using and I would...
2
by: George W. | last post by:
In perl I'm used to using hashtables of arrays and accessing a particular element like this: $HoA{flintstones} Simple as that. How do I access and update values of arrays stored in my hashtable...
5
by: Jack Addington | last post by:
How do I go about accessing elements of an Arraylist using something other than the index? I have a class that manages a number of subclasses. Those subclasses are stored in an arraylist. The...
11
by: Yahoo | last post by:
I have a programming issue where I need to know the whole history of the call stack to ensure it was never within a given method (specifically my own method). I am hooking into the XmlDocument...
2
by: Ekul | last post by:
I have an application that allows users to login and logout. I track how many users are logged in and when each individual is logged in. The application will not allow concurrent logins(let a...
6
by: mumrah | last post by:
When getting different elements out of an array in JS, i know array.item(0) and array both return the 0th element. My question, will this work in any instance?
0
by: dougelderz | last post by:
I am integrating a CyberSource Commerce pipeline component to manage my payment transactions. CyberSource provides a hashtable COM object that is used to set data into the orderform before a...
9
by: JohnR | last post by:
I have the name of a control in a string variable and I want to change one of the controls properties. Right now I recursively scan all the controls on the form until I get one whose name matches...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...

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.