Hi all, is there a key value collection in .Net that maintains the
ordering in which I add items.
I'm finding that I'm creating a number of classes that contain a name
and then some object. I would prefer just to use some collection that
maintains the ordering in which I add things (like ArrayList), but that
maps a key to a value.
I thought NameValueCollec tion was the right one - but it doesn't
guarantee ordering.
If it doesn't exist, why not? If it doesn't what is the "right" way to
build it? Just have wrap your class around an instance of ArrayList
and have some internal class that takes the key given in the Add method
and instantiates this generic class with two members (a string key and
an object value), then add that to the internal ArrayList?
Thanks,
Novice
May 2 '06
23 11326
I can't use .Net 2 yet... I'm stuck with my current version of .Net -
but thanks for the suggestion none the less.
Novice
But the OP asked that the entries be maintained in their original
order, not in key order. That's why I didn't suggest SortedList.
Why not add a Hashtable to that and avoid the iteration over the
ArrayList looking for the item? A linear search is terribly slow.
> What if you add something with the same key twice? It will show up in the hashtable just once but in the ArrayList twice. That is probably not what he's looking for.
There's a simple solution to that:
public void Add(object key, object item)
{
object existingItem = this._hash[key];
if (existingItem != null)
{
this._array.Rem ove(existingIte m);
}
this._hash[key] = item;
this._array.Add (item);
}
Much better than doing a linear search every time you add an item.
If the OP wants instead a collection that permits duplicates (two items
with the same key) then it's no longer clear what the indexer should
return.
Novice,
Bruce's idea is really the best one IMO. Here's a link to an
implementation.
<http://www.codeproject .com/csharp/keyedlist.asp#x x701821xx>
Brian il***********@g mail.com wrote: Hi all, is there a key value collection in .Net that maintains the ordering in which I add items.
I'm finding that I'm creating a number of classes that contain a name and then some object. I would prefer just to use some collection that maintains the ordering in which I add things (like ArrayList), but that maps a key to a value.
I thought NameValueCollec tion was the right one - but it doesn't guarantee ordering.
If it doesn't exist, why not? If it doesn't what is the "right" way to build it? Just have wrap your class around an instance of ArrayList and have some internal class that takes the key given in the Add method and instantiates this generic class with two members (a string key and an object value), then add that to the internal ArrayList?
Thanks, Novice
So in case anyone is interested I did something even more simple than
Bruce's idea. I.E. I just wrapped ArrayList. The reason I don't also
use Hashtable is two fold:
1. My Collection also needs to be capable of containing other instances
of itself - i.e. I need a collection that can contain regular
key/value pairs, but any of those values could potentially be another
instance of itself. Therefore, I may end up with nested keys that are
identical - hence hashtable wouldn't work - unless I also embed
Hashtables in Hashtables - but that defeats the purpose of using
Hashtable for searching
2. Really just restating the last bit of point 1 - that is, a Hashtable
can't search on nested instances of itself - i.e. if I add a Hashtable
Object like this:
Hashtable hashtable = getHashtable1() ;
Hashtable hashtable2 = getHashtable2() ;
hashtable.Add(" blah", hashtable2);
Now searching on hashtable2 won't be done.
However, after writing all that - perhaps I could come up with a clever
naming system, whereby nested instances get characters in them much
like folders/directories in the various OS'.
So I could use some illegal character for my names as a separator and
then search using that...
Hmm... that could work,
Novice
That's simple to solve. Use the data as the key. If you want to keep them
in the original order, any of the array list and collection objects will do
this by default - it's the way they're implemented.
Mike Ober.
"Bruce Wood" <br*******@cana da.com> wrote in message
news:11******** **************@ g10g2000cwb.goo glegroups.com.. . But the OP asked that the entries be maintained in their original order, not in key order. That's why I didn't suggest SortedList.
jeremiah johnson wrote: Bruce Wood wrote:
I just whipped something up in Java. Its untested, unsynchronized,
Java have a LinkedHashMap (or something like that) which does exactly
what OP want's.
The "good" (TM :) solution is to store double-linked-lists with
insertion-order and let the values in the hash contain references to
their linked-list representation.
This allows O(1) insert,update and remove, as well as ordered traversal
(forward and backward) and is exactly what the correspoding JAVA class does.
--
Helge
Michael D. Ober wrote: That's simple to solve. Use the data as the key. If you want to keep them in the original order, any of the array list and collection objects will do this by default - it's the way they're implemented.
IDictionary have no ordering, which was what OP wanted.
You *could* embed the ordering into the keys, and use SortedList, but I
don't like the idea of embedding collection-containment info into the
contained objects. For one thing each object can only be contained in
one such list.
A better alternative is to do linked-list-hashtables.
--
Helge il***********@g mail.com wrote: So in case anyone is interested I did something even more simple than Bruce's idea. I.E. I just wrapped ArrayList. The reason I don't also use Hashtable is two fold:
So you've chosen linear lookup-time.
1. My Collection also needs to be capable of containing other instances of itself - i.e. I need a collection that can contain regular key/value pairs, but any of those values could potentially be another instance of itself. Therefore, I may end up with nested keys that are identical - hence hashtable wouldn't work - unless I also embed
*Nested* identical keys shouldn't be a problem. They are contained in
separate data-structures.
Hashtables in Hashtables - but that defeats the purpose of using Hashtable for searching
Well,... atleast in multiple layers. Why don't you just have
IDictionary'es, and then use a hashtable for the "big" ones and
something else for the "smaller" ones?
2. Really just restating the last bit of point 1 - that is, a Hashtable can't search on nested instances of itself - i.e. if I add a Hashtable Object like this: Hashtable hashtable = getHashtable1() ; Hashtable hashtable2 = getHashtable2() ; hashtable.Add(" blah", hashtable2);
Now searching on hashtable2 won't be done.
If you have a flat data-structure encoded in a recursive data-structure
(for performance) the recursion shouldn't be visible to the user.
If you have an inherently recursive data-structure you should probably
show that to the user, and do recursive lookup, and everything would be
fine.
What exactly is the type of your keys? which type are they? single:
"foo", or tupled: ("x", "y"), or list-like: ["x", "y", ...] ?
However, after writing all that - perhaps I could come up with a clever naming system, whereby nested instances get characters in them much like folders/directories in the various OS'.
Thats called linearization. Perhaps you *are* having a recursive
data-structure and you're just not doing lookup in a way corresponding
to that?
It sounds a lot like a tree with indexed children to me :)
So I could use some illegal character for my names as a separator and then search using that...
Hmm... that could work,
Watch out, along that bumpy road lies all the dangers of linearization:
- escaping
- delinearization
- parsing, and error-handling of unparseable data
- formulation of searches
If your data *is* recursively structured, you are probably best off
storing the recursively and doing recursive lookups:
void object lookup(params object[] keys) {
IDictionary d = TopDict;
for ( int i = 0; i < keys.Length - 1; ++i )
d = (IDictionary)d[keys[i]];
return d[keys[keys.Length - 1]];
}
dict.lookup("fo o", "foo", "baz");
--
Helge This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: m. pollack. |
last post by:
Hi all,
Is there any information to be had about the "Object
Collection Editor" that appears when you click on a
collection property in the PropertyGrid control? I have a
class that maintains a collection of instances of the same
class, and I would like the user to be able to expand the
nested instances and view their properties.
For example, if I have a class called TestClass that has
|
by: keweiming |
last post by:
I have a project which needs to open hundreds to thousands of files for
writing. The following is a simplified test program I wrote to see if I
can use a map<string, ofstream> object to keep the list of ofstreams. I
need to have them open simultaneously for writing -- I have millions of
rows of data to write to them so opening and closing all the time will
be unacceptable in efficiency.
The following program compiles but failed to run....
|
by: Project2501a |
last post by:
hey guys,
Question about the internal workings of Access. How are controls added
in the Form.Controls collection? I mean, in which order?
the order place them on the form? is there a way to re-arrange them as
i please? My solution is to remove the objects from the Form.Controls
collection, place them in a tmpCollection, arrange tmpCollection as I
please, and then re-add them back into the Form.Controls collection.
thanks
|
by: Frank |
last post by:
Our system maintains session state using the ASP.NET State Server service. We expect some of our session state objects to be over 85K, which categorizes them to be VLO's (very large objects) in terms of .NET memory allocations. This means that these objects will be allocated in a special managed heap for large objects. This makes sense since this would require some work for the garbage collector to move and compact these objects during a GC...
|
by: marcuslm |
last post by:
I need help figuring out which collection to use. Maybe I need to create my
own but here's what I need.
A collection that...
1. provides ability to access values via a key
2. maintains the order in which I add it to the collection (ie when I
iterate through it, they come out the same order in which I put them in)
Help please. Thanks!!!
| |
by: Michael D. Ober |
last post by:
In the code below, the IComparator function is never called. What am I
missing?
Public Class ArchiveInfo
Implements System.IComparable(Of ArchiveInfo)
Public FullName As String = ""
Public AccountNumber As String = ""
Public Sub New(ByRef Account As String, ByVal fname As String)
|
by: Martin R |
last post by:
Hi,
How to find first not null value in column whitout chacking whole table
(if there is a not null value then show me it and stop searching, the
table is quite big)?
thx,
Martin
*** Sent via Developersdex http://www.developersdex.com ***
|
by: evanburen |
last post by:
When my page loads, I check for the existence of a cookie value through
readCookie(). If there is a value present for the cookie, I would like
that to be the default value in function ordering() and that value to
be the SELECTED value in ddlProfileNames. In other words, if there is
no cookie value, then function ordering will default to
ordering("Div1,Div2,Div3,Div4"). If there is a value, then that should
be used in function ordering...
|
by: zephyr |
last post by:
Hi,
I have a hidden field in my form:
<input type="hidden" name="checkBoxesCollection" value="">
If I submit the form I call a javascript function:
function storeCheckboxFieldNames(theForm){
var inputElements = theForm.getElementsByTagName("input");
var collection = document.getElementsByName("checkBoxesCollection");
alert(collection.value);
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |