473,672 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Two way collection

I need to do a mapping between two strings. I want to be able to look
up either string from the other. So if I know String A, I want to get
String B. If I know String B I want to get String A. I usually use
something like a hashtable, but that is one way. What should I use to
get a two way collection?

Thanks
Chris
Apr 18 '06 #1
5 2222

Chris wrote:
I need to do a mapping between two strings. I want to be able to look
up either string from the other. So if I know String A, I want to get
String B. If I know String B I want to get String A. I usually use
something like a hashtable, but that is one way. What should I use to
get a two way collection?


Nothing built-in I can think of, but building your own wouldn't be too
hard - since both 'keys' and 'values' are Strings, just inherit from
Hashtable and override Add to also insert (b, a) whenever you insert
(a, b). Unless you need to know 'which way round' A and B were
originally supplied, in which case you'd need to do something like
inherit from DictionaryBase, and maintain two Hashtables internally,
one for 'original way round' mapping and one for 'the other way round'
mapping.

--
Larry Lard
Replies to group please

Apr 18 '06 #2
Larry Lard wrote:
Chris wrote:
I need to do a mapping between two strings. I want to be able to look
up either string from the other. So if I know String A, I want to get
String B. If I know String B I want to get String A. I usually use
something like a hashtable, but that is one way. What should I use to
get a two way collection?

Nothing built-in I can think of, but building your own wouldn't be too
hard - since both 'keys' and 'values' are Strings, just inherit from
Hashtable and override Add to also insert (b, a) whenever you insert
(a, b). Unless you need to know 'which way round' A and B were
originally supplied, in which case you'd need to do something like
inherit from DictionaryBase, and maintain two Hashtables internally,
one for 'original way round' mapping and one for 'the other way round'
mapping.


I just hoped there was a way to do it w/o using two hashtables.

Thanks for the input.
Chris
Apr 18 '06 #3
use an exhaustive search the other way around

"Chris" <no@spam.com> wrote in message
news:O5******** ******@TK2MSFTN GP05.phx.gbl...
Larry Lard wrote:
Chris wrote:
I need to do a mapping between two strings. I want to be able to look
up either string from the other. So if I know String A, I want to get
String B. If I know String B I want to get String A. I usually use
something like a hashtable, but that is one way. What should I use to
get a two way collection?

Nothing built-in I can think of, but building your own wouldn't be too
hard - since both 'keys' and 'values' are Strings, just inherit from
Hashtable and override Add to also insert (b, a) whenever you insert
(a, b). Unless you need to know 'which way round' A and B were
originally supplied, in which case you'd need to do something like
inherit from DictionaryBase, and maintain two Hashtables internally,
one for 'original way round' mapping and one for 'the other way round'
mapping.


I just hoped there was a way to do it w/o using two hashtables.

Thanks for the input.
Chris

Apr 18 '06 #4

"Jeff Dillon" <je********@hot mail.com> wrote in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
use an exhaustive search the other way around

"Chris" <no@spam.com> wrote in message
news:O5******** ******@TK2MSFTN GP05.phx.gbl...
Larry Lard wrote:
Chris wrote:

I need to do a mapping between two strings. I want to be able to look
up either string from the other. So if I know String A, I want to get
String B. If I know String B I want to get String A. I usually use
something like a hashtable, but that is one way. What should I use to
get a two way collection?
Nothing built-in I can think of, but building your own wouldn't be too
hard - since both 'keys' and 'values' are Strings, just inherit from
Hashtable and override Add to also insert (b, a) whenever you insert
(a, b). Unless you need to know 'which way round' A and B were
originally supplied, in which case you'd need to do something like
inherit from DictionaryBase, and maintain two Hashtables internally,
one for 'original way round' mapping and one for 'the other way round'
mapping.


I just hoped there was a way to do it w/o using two hashtables.

Thanks for the input.
Chris



If you aren't too worried about speed, you can use a StringCollectio n as a
base for something like the following collection class:

public class MyCollection : NameValueCollec tion
{
public string GetKeyByValue(s tring Value)
{
string[] keys = base.BaseGetAll Keys();
foreach (string key in keys) {
if (Array.IndexOf( base.GetValues( key), Value) >= 0) {
return key;
}
}
return null;
}
}

hth :)

Mythran

Apr 18 '06 #5
How about putting both:
(key=A, value=B)
and
(key=B, value=A) [if A<>B]

in the same hashtable? This way you could use the same hashtable to
lookup both ways (from A get B, from B get A)... mmm right?

-tom

Apr 19 '06 #6

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

Similar topics

8
2203
by: Generic Usenet Account | last post by:
To settle the dispute regarding what happens when an "erase" method is invoked on an STL container (i.e. whether the element is merely removed from the container or whether it also gets deleted in the process), I looked up the STL code. Erase certainly does not delete the memory associated with the element. However, it appears that the destructor on the element is invoked. I wonder why it has to be this way. In my opinion, this renders...
5
2722
by: Kurt Bauer | last post by:
I have an ASP group calendar application which pulls calendar data from Exchange via webdav into an XML string. I then loop the XML nodes to populate a collection of appointments. Finally I use the appointment collection to populate the calendar control. The performance getting the XML data is fine, but loading the data into the collection is slow. My question/problem is should I be using the collection, a dataset, or something else to...
18
5739
by: Scott | last post by:
I have a collection where the items in the collection are dates. I want to iterate over the collection and build a value list string for the rowsource of a listbox. The dates in the collection are not in chronological order. Is there a way to first sort the collection and put the dates in chronological order before creating the value list string? Or, how would I iterate over the collection pulling out the dates in chronological order? ...
11
18701
by: Pavils Jurjans | last post by:
Hello, There's some confusion about the purpose and difference between these handy classes... First, both of them are holding number of key - value pairs, right? Then, I see that there may be some difference in terms of data types allowed for keys and values, perhaps? I read the following in MSDN about "CollectionBase" class and
2
3171
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the problems that I have encountered to date and the solutions (if any) that I found. http://users.adelphia.net/~brianpclab/ServerControlCollectionIssues.htm This page also has all of the source code in a compressed file that you are free to download...
6
4835
by: Michael D. Ober | last post by:
In VB 6, the loop iterator v in the following code must be a variant. dim v as variant dim c as new collection for each v in collection ... next v What is the general translation in VB 7.1 and VB 8 Beta 1? Also, is there an easy way to force "v" to be an early bound variable. In VB 6 this can be
3
2147
by: Matt Michael | last post by:
I have a listview control and a collection object right now that I'm trying to pass information to and from. Whenever I click on the checkbox, I want it to remove certain listview items and add them to the collection. Whenever I uncheck the checkbox, I want to add the items back into the listview from the collection, and remove them from the collection so I can do the process multiple times. The listview tag is being used as a key, so I...
10
2764
by: Chet Cromer | last post by:
I am creating a set of base classes and sub classes to use throughout a program I'm developing. The base class represents a generic "lookup table" from my database that contains lists of things like manufacturers, makes, modes, etc. of cars. I have created a generic "datacollection" class and a generic "dataobject" class to represent the table and the rows within that table as a collection of objects with generic properties for...
6
13408
by: Scott M. Lyon | last post by:
As I mentioned in my other post, I'm attempting to, using COM Interop so I can update existing VB6 code to (for several specific functions) return a Hashtable from a .NET library. I've had very little luck processing the Hashtable itself in VB6 (I can add a reference to the project so it knows what a Hashtable is, but I'm not having much luck looping through all objects in the Hashtable), so I decided to try a different idea.
6
7206
by: Arthur Dent | last post by:
How do you sort a generic collection derived from System.Collections.ObjectModel.Collection? Thanks in advance, - Arthur Dent
0
8404
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,...
0
8931
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, 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...
0
8828
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 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...
0
8680
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7446
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, 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...
0
5705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4418
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2819
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
2
1816
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.