.NET基类库中是否存在允许使用重复键的字典类?我找到的唯一解决方案是创建,例如,一个类:

Dictionary<string, List<object>>

但实际上用起来很烦人。在Java中,我相信MultiMap可以实现这一点,但在. net中找不到类似的东西。


当前回答

如果你正在使用>= .NET 4,那么你可以使用元组类:

// declaration
var list = new List<Tuple<string, List<object>>>();

// to add an item to the list
var item = Tuple<string, List<object>>("key", new List<object>);
list.Add(item);

// to iterate
foreach(var i in list)
{
    Console.WriteLine(i.Item1.ToString());
}

其他回答

如果同时使用字符串作为键和值,则可以使用System.Collections.Specialized。它将通过GetValues(string key)方法返回一个字符串值数组。

你是说完全一致而不是完全重复吗?否则哈希表将无法工作。

同余意味着两个单独的键可以哈希到等价的值,但键不相等。

例如:假设你的哈希表的哈希函数是hashval = key mod 3。1和4都映射到1,但是是不同的值。这就是列表的概念发挥作用的地方。

当需要查找1时,该值被哈希为1,遍历列表,直到找到Key = 1。

如果允许插入重复的键,则无法区分哪些键映射到哪些值。

关于使用Lookup的非常重要的注意事项:

你可以通过调用一个实现IEnumerable(T)的对象上的Lookup(TKey, TElement)实例来创建一个Lookup(TKey, TElement)实例。

没有公共构造函数来创建Lookup(TKey、TElement)的新实例。此外,Lookup(TKey, TElement)对象是不可变的,也就是说,在创建Lookup(TKey, TElement)对象后,您不能从它添加或删除元素或键。

(从MSDN)

我认为这对大多数人来说都是一种阻碍。

重复的键将破坏Dictionary的整个契约。在字典中,每个键都是唯一的,并映射到单个值。如果您想要将一个对象链接到任意数量的其他对象,最好的方法可能是类似于DataSet的东西(通常的说法是表)。把键放在一列,值放在另一列。这比字典要慢得多,但这是为了失去散列键对象的能力而付出的代价。

“滚动您自己的”版本的字典,允许“重复键”条目,这很容易。下面是一个粗略的简单实现。您可能需要考虑添加对dictionary <T>的基本大部分(如果不是全部)的支持。

public class MultiMap<TKey,TValue>
{
    private readonly Dictionary<TKey,IList<TValue>> storage;

    public MultiMap()
    {
        storage = new Dictionary<TKey,IList<TValue>>();
    }

    public void Add(TKey key, TValue value)
    {
        if (!storage.ContainsKey(key)) storage.Add(key, new List<TValue>());
        storage[key].Add(value);
    }

    public IEnumerable<TKey> Keys
    {
        get { return storage.Keys; }
    }

    public bool ContainsKey(TKey key)
    {
        return storage.ContainsKey(key);
    }

    public IList<TValue> this[TKey key]
    {
        get
        {
            if (!storage.ContainsKey(key))
                throw new KeyNotFoundException(
                    string.Format(
                        "The given key {0} was not found in the collection.", key));
            return storage[key];
        }
    }
}

一个关于如何使用它的简单例子:

const string key = "supported_encodings";
var map = new MultiMap<string,Encoding>();
map.Add(key, Encoding.ASCII);
map.Add(key, Encoding.UTF8);
map.Add(key, Encoding.Unicode);

foreach (var existingKey in map.Keys)
{
    var values = map[existingKey];
    Console.WriteLine(string.Join(",", values));
}