我想知道C#中是否有内置类型,就像' Dictionary'但是TKey和TValue都必须是唯一的。
例如::
d.Add(1, "1");
d.Add(2, "1"); // This would not be OK because "1" has already been used as a value.
我知道这有点奇特,但似乎因为BCL中有大约10亿个集合类型,它可能存在。有什么想法吗?
答案 0 :(得分:14)
如何使用Dictionary和HashSet / secondary reverse Dictionary - 它将解决问题,并且比单个Dictionary上的检查表现更好。
像这样的东西,包装成类:
HashSet<string> secondary = new HashSet<string>(/*StringComparer.InvariantCultureIgnoreCase*/);
Dictionary<int, string>dictionary = new Dictionary<int, string>();
object syncer = new object();
public override void Add(int key, string value)
{
lock(syncer)
{
if(dictionary.ContainsKey(key))
{
throw new Exception("Key already exists");
}
if(secondary.Add(value)
{
throw new Exception("Value already exists");
}
dictionary.Add(key, value);
}
}
答案 1 :(得分:1)
我通过将数据存储为Dictionary<TKey, HashSet<TValue>>
来解决了这个问题。
如果您想要一个具有2个主键的值,可以用另一个Dictionary替换HashSet。
Dictionary<int, HashSet<int>> _myUniquePairOfIntegerKeys;
// OR
Dictionary<string, Dictionary<string, bool>> _myUniquePairOfStringKeysWithABooleanValue;
答案 2 :(得分:1)
对于内部目的,我写了BiDictionary
。它不是防弹的,我不会将它暴露给用户,所以它对我来说很好。它允许我获得任何一个键,因为我需要。
KeyPair<,>
必须能够实现IEnumerable<,>
,从而实现Add
方法,以便我们可以使用对象初始值设定项。
internal class KeyPair<TKey1, TKey2>
{
public TKey1 Key1 { get; set; }
public TKey2 Key2 { get; set; }
}
这是作为动态对象的主类,因此我们可以在检索值时在其上使用键名:
internal class BiDictionary<TKey1, TKey2> : DynamicObject, IEnumerable<KeyPair<TKey1, TKey2>>
{
private readonly Dictionary<TKey1, TKey2> _K1K2 = new Dictionary<TKey1, TKey2>();
private readonly Dictionary<TKey2, TKey1> _K2K1 = new Dictionary<TKey2, TKey1>();
private readonly string _key1Name;
private readonly string _key2Name;
public BiDictionary(string key1Name, string key2Name)
{
_key1Name = key1Name;
_key2Name = key2Name;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
if (binder.Name == _key1Name)
{
result = _K1K2;
return true;
}
if (binder.Name == _key2Name)
{
result = _K2K1;
return true;
}
result = null;
return false;
}
public void Add(TKey1 key1, TKey2 key2)
{
_K1K2.Add(key1, key2);
_K2K1.Add(key2, key1);
}
public IEnumerator<KeyPair<TKey1, TKey2>> GetEnumerator()
{
return _K1K2.Zip(_K2K1, (d1, d2) => new KeyPair<TKey1, TKey2>
{
Key1 = d1.Key,
Key2 = d2.Key
}).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
示例:
dynamic bidic = new BiDictionary<string, string>("Key1", "Key2")
{
{ "foo", "bar" },
{ "baz", "qux" }
};
var bar = bidic.Key1["foo"];
var foo = bidic.Key2["bar"];
如果修改外部的任何词典,它们可能会不同步。为此,我使用ObservableDictionary
,以便我可以更新另一个,如果一个更改,但为了简单起见,我删除了这部分代码,以便说明主逻辑。
答案 3 :(得分:0)
有一个位于here的项目有类似的类型。它被称为PairDictionary,它工作得很好。不是最好的答案,但对于需要那个自定义课程的人来说。
答案 4 :(得分:0)
您可以实现一个扩展方法,如果给定值已经存在,该方法将跳过添加到字典的操作。
static class Extensions
{
public static void AddSafe(this Dictionary<int, string> dictionary, int key, string value)
{
if (!dictionary.ContainsValue(value))
dictionary.Add(key, value);
}
}
像普通函数一样调用
d.AddSafe(1, "1");
d.AddSafe(2, "1"); // This would not add anything