任何现有的字典>其中的值集合不能包含键

本文关键字:包含键 集合 不能 列表 字典 任何现 | 更新日期: 2023-09-27 18:31:57

我需要以下内容(为简洁起见,此处使用字符串作为类型):

MyDictionary : IDictionary<string, List<string>>
{
    private readonly Dictionary<string, List<string>> _collection;
    ...
    // The collection cannot contain the key
    public void Add(string key, List<string> value)
    {
        _collection.Add(key, new List<string>(value.RemoveAll(p => p == key));
    }
}

因此,在使用中,您可能有:

string myName = "Superstringcheese";
List<string> myFriends = new List<string> {"John", "Jane", "Jerry"};
string yourName = "Someone";
List<string> yourFriends = new List<string> {"John", "Bill", "Ted"};
var myDic = new MyDictionary();
// It's okay that each of us (each key) has some friends that are the same. It's not okay for me to have myself as a friend. The add method would check if the key is contained by the value collection and remove it if necessary.
myDic.Add(myName, myFriends);
myDic.Add(yourName, yourFriends);

在我重新发明任何轮子之前,是否有执行此操作的本机集合类型,或者有人知道流行的实现吗?

任何现有的字典<T,列表<T>>其中的值集合不能包含键

好吧,我可以在这方面提供帮助:

是否有执行此操作的本机集合类型?

不。