具有单个键的多个值的哈希表

我想在单个键中存储多个值,如:

HashTable obj = new HashTable(); obj.Add("1", "test"); obj.Add("1", "Test1"); 

现在这会引发错误。

你可以将你的test,test1,test2,...放在一个表中,然后将这个表放在一个Hashtable中作为键的值,对于所有它们都是相同的。

例如尝试这样的事情:

 List list = new List(); list.Add("test"); list.Add("test1"); 

然后:

 HashTable obj = new HashTable(); obj.Add("1", list); 

您不能在Dictionary / Hashtable中使用相同的键。 我想你想为每个键使用List,例如(VB.NET):

 Dim dic As New Dictionary(Of String, List(Of String)) Dim myValues As New List(Of String) myValues.Add("test") myValues.Add("Test1") dic.Add("1", myValues) 

C#:

 Dictionary> dic = new Dictionary>(); List myValues = new List(); myValues.Add("test"); myValues.Add("Test1"); dic.Add("1", myValues); 

我正在使用自己的MultiDictionary类。 它基于Dictionary>但提供了一些语法糖。 应该很容易扩展Entry来实现IList

 public class MultiDictionary { private Dictionary> data = new Dictionary>(); public struct Entry : IEnumerable { private readonly MultiDictionary mDictionary; private readonly TKey mKey; public TKey Key { get { return mKey; } } public bool IsEmpty { get { return !mDictionary.data.ContainsKey(Key); } } public void Add(TValue value) { List list; if (!mDictionary.data.TryGetValue(Key, out list)) list = new List(); list.Add(value); mDictionary.data[Key] = list; } public bool Remove(TValue value) { List list; if (!mDictionary.data.TryGetValue(Key, out list)) return false; bool result = list.Remove(value); if (list.Count == 0) mDictionary.data.Remove(Key); return result; } public void Clear() { mDictionary.data.Remove(Key); } internal Entry(MultiDictionary dictionary, TKey key) { mDictionary = dictionary; mKey = key; } public IEnumerator GetEnumerator() { List list; if (!mDictionary.data.TryGetValue(Key, out list)) return Enumerable.Empty().GetEnumerator(); else return list.GetEnumerator(); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); } } public Entry this[TKey key] { get { return new Entry(this, key); } } } 

你可以使用字典。

实际上,您刚刚描述的是Dictionary集合的理想用法。 它应该包含键:值对,而不管值的类型。 通过将值设为自己的类,如果需要,您将能够在将来轻松扩展它。

示例代码:

 class MappedValue { public string SomeString { get; set; } public bool SomeBool { get; set; } } Dictionary myList = new Dictionary; 

这会引发错误,因为您要添加两次相同的密钥。 尝试使用Dictionary而不是HashTable

 Dictionary> values = new Dictionary>(); IList list = new List() { "test", "Test1" }; values.Add(1, list); 

可能是4年后,但我希望它能在以后帮助别人。 正如前面在post中提到的, 不可能在Hashtable(键,值)中对不同的值使用相同的键 。 虽然,您可以在HashTable的键/值对中创建List或某个对象作为值。

 //instantiate new Hashtable Hashtable hashtable = new Hashtable(); //create a class that would represent a value in the HashTable public class SomeObject { public string value1 { get; set;} public string value2 { get; set;} } //create a List that would store our objects List list = new List(); //add new items to the created list list.Add(new SomeObject() { value1 = "test", value2 = "test1" }); list.Add(new SomeObject() { value1 = "secondObject_value1" value2 = "secondObject_value2" }) //add key/value pairs to the Hashtable. hashTable.Add("1", list[0]); hashTable.Add("2", list[1]); 

然后检索这些数据:

 //retrieve the value for the key "1" SomeObject firstObj = (SomeObject)hashTable[1]; //retrieve the value for the key "2" SomeObject secondObj = (SomeObject)hashTable[2]; Console.WriteLine("Values of the first object are: {0} and {1}", firstObj.value1,firstObj.value2); Console.WriteLine("Values of the second object are {0} and {1}", secondObj.value1, secondObj.value2); // output for the WriteLine: Values of the first object are: test and test1 Values of the second object are secondObject_value1 and secondObject_value2 

将列表存储在哈希表中:

 obj.Add("1",new List()); (obj["1"] as List).Add("test"); (obj["1"] as List).Add("test1"); 

这是一个常见的伎俩。

JFYI,您可以这样声明您的dic:

 Dictionary> dic = new { { 1, new List { "Test1", "test1" }, { 2, new List { "Test2", "test2" } }; 

您正在寻找一个Lookup ,它可以为每个键本机存储多个值。

正如所指出的,这仅适用于固定列表,因为一旦创建了条目,就无法向查找添加条目。

 public class LookupEntry { public string Key { get; set; } public string Value { get; set; } } var list = new List(new LookupEntry [] { new LookupEntry() {Key="1", Value="Car" }, new LookupEntry() {Key="1", Value="Truck"}, new LookupEntry() {Key="2", Value="Duck"} }); var lookup = list.ToLookup(x => x.Key, x => x.Value); var all1s = lookup["1"].ToList(); 

您可以使用NameValueCollection – 与hashtable的工作方式相同,并具有“GetValues()”。

你最好使用我在这个库中使用的两个哈希表