Urgent help required--C# equivalent to Java HashMap
-
Monday, December 05, 2005 9:47 AM
Java HashMap equivalent in C# is HashTable.
But in java the HashMap can have a null key ie key is a null reference and there can be duplicate keys ie basically keys with the same name.
I want a class that provides the above feature ie
1)stores in key value pairs
2) key can be null
3)Keys with the same name can be added.or if there is another util class that provides the above mentioned features.
Please help me with this.
Thanks a lot in advance
All Replies
-
Monday, December 05, 2005 10:29 AMModerator
There's no .net class that provides this functionality. Hower, you can write your own "HashMap" class with the logic you need, by implementing ICollection interface.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemcollectionsicollectionclasstopic.asp -
Monday, December 05, 2005 10:43 AMModerator
Java's HashMap doesn't actually allow multiple keys - it just allows you to replace the value for a particular key, which is the same for Hashtable in .NET, if you use the indexer rather than the Add method.
As for supporting a null key - you could implement your own IDictionary which allows this, using a Hashtable for everything other than the null key.
Jon
-
Monday, December 05, 2005 10:44 AM
What you want is the Wintellect Power Collections, a set of collection classes developed by Peter Gould (who helped to design the C#/.Net libraries). This is an officially sanctioned library.
Get it at http://www.wintellect.com/powercollections
I can't speak too highly of that library!
For what you want, you'd use a "MultiDictionary". The code goes like this:
Wintellect.PowerCollections.MultiDictionary<string, string> test =
new Wintellect.PowerCollections.MultiDictionary<string, string>(true);
test.Add(null, "Null Test1");
test.Add(null, "Null Test2");
test.Add("Key", "Key Test1");
test.Add("Key", "Key Test2");
ICollection<string> null_key_values = test[null];
foreach (string s in null_key_values)
MessageBox.Show(s, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
ICollection<string> key_values = test["Key"];
foreach (string s in key_values)
MessageBox.Show(s, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
-
Monday, December 05, 2005 11:12 AMModeratorI think we need to check with the OP where he actually wants multi-valued collections or not - Java's HashMap is not multivalued - but its put() method doesn't complain if the key is already present (which Hashtable's Add method does). In this case, just using the indexer instead is the right way to go.
However, it looks like the Power Collections support null keys, so it solves that problem neatly.
Jon -
Monday, December 05, 2005 12:00 PMD'oh! I read the OP's request for "multiple keys" to mean "multiple values per key". My brain couldn't conceve the idea of having anything OTHER than multiple keys in a collection, so I misread the intent.
Anyway, unfortunately there isn't a plain Dictionary<> class in PowerCollections, presumably because there's already one in the standard libraries. Alas, the standard one doesn't allow null keys (as we already discussed), so we can't use that...
Using a MultiDictionary<> to store single key/value pairs seems a little overkill, but perhaps it'll work.
-
Tuesday, December 06, 2005 5:50 PM
Data
MySchool StudentID Key Value
West College 10 Name Ricky
West College 10 Surmane Martin
West College 10 Adress DownStreet
West College 10 Zip 555
West College 20 Name Sharone
West College 20 Surmane Stone
West College 20 Adress 5th Ave.
West College 20 Zip 444
East College 30 Name Marry
East College 30 Surmane Christmas
East College 30 Adress UpStreet
East College 30 Zip 111
_____________________________
HashTable Structure
myHashTable [ MySchool, HashTable2]
myHasTable2 [ StudentID,HashTable3]
myHashTable3 [Key,Value]
___________________________
CODE
IDictionaryEnumerator
listEnumerator = myHastable.GetEnumerator(); while (listEnumerator.MoveNext()){
Response.Write(listEnumerator.Key.ToString());
Response.Write(
"--->");Response.Write(listEnumerator.Value.ToString());
Response.Write("<br>");
}
___________________________________________
OutPut
West College ---> System.Collections.Hashtable
East College ---> System.Collections.Hashtable
____________________________________________
Question / BrainStorming ?????
How inner hashtable datas can be retrieved into a GridView??
( That is the difference Java HashMap vs to C# HashTables.) -
Wednesday, January 17, 2007 1:17 PMI needed a Dictionary which accepted a "null" key, but there seems to be no native one, so I have written my own. It's very simple, actually. I inherited from Dictionary, added a private field to hold the value for the "null" key, then overwritten the indexer. It goes like this :
public class NullableDictionnary : Dictionary<string, string>{
string null_value;
public StringDictionary this[string key]{
get{
if ( key == null )
{return null_value;}
return base[key];
}
set{
if ( key == null )
{null_value = value;}
else
{base[key] = value;}
}
}
}
Hope this helps someone in the future. -
Monday, January 31, 2011 8:38 PM
DonkeyMaster,
This did help me and I posted your answer to another forum I found seeking a similar resolve:
http://stackoverflow.com/questions/1273139/c-java-hashmap-equivalent/4855802#4855802
Thx

