我需要以下内容(为简洁起见,此处使用字符串作为类型):
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);
在我重新发明任何轮子之前,是否有一个本机集合类型可以做到这一点,或者有人知道流行的实现吗?