根据value来定位key在C#的Dictionary中如何实现?
来源:互联网 发布:域名注册是永久的吗 编辑:程序博客网 时间:2024/05/10 06:06
在C#中Dictionary就是hashtable,只是用起来有泛型更方便,它位于System.Collections.Generic,其定义为:
[SerializableAttribute][ComVisibleAttribute(false)]public class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback
我们常常用key来得到value,例如:
string v = dict[key1];
但如何用value来得到key? 例如:
string k = dict[value1];
这种需求是常见的,当然你可以遍历所有的value来定位key,或者用很炫的语法糖LINQ,像这样:
MyDict.FirstOrDefault(pair => pair.Value == "the value you want").Key;
但酱紫就不大约O(1)时间了。
一种做法是创建一个自定义的数据结构,里面用空间换时间,存储另外一个value,key的Dictionary,代码如下:
/// <summary>/// This is a dictionary guaranteed to have only one of each value and key. /// It may be searched either by TFirst or by TSecond, giving a unique answer because it is 1 to 1./// </summary>/// <typeparam name="TFirst">The type of the "key"</typeparam>/// <typeparam name="TSecond">The type of the "value"</typeparam>public class BiDictionaryOneToOne<TFirst, TSecond>{ IDictionary<TFirst, TSecond> firstToSecond = new Dictionary<TFirst, TSecond>(); IDictionary<TSecond, TFirst> secondToFirst = new Dictionary<TSecond, TFirst>(); #region Exception throwing methods /// <summary> /// Tries to add the pair to the dictionary. /// Throws an exception if either element is already in the dictionary /// </summary> /// <param name="first"></param> /// <param name="second"></param> public void Add(TFirst first, TSecond second) { if (firstToSecond.ContainsKey(first) || secondToFirst.ContainsKey(second)) throw new ArgumentException("Duplicate first or second"); firstToSecond.Add(first, second); secondToFirst.Add(second, first); } /// <summary> /// Find the TSecond corresponding to the TFirst first /// Throws an exception if first is not in the dictionary. /// </summary> /// <param name="first">the key to search for</param> /// <returns>the value corresponding to first</returns> public TSecond GetByFirst(TFirst first) { TSecond second; if (!firstToSecond.TryGetValue(first, out second)) throw new ArgumentException("first"); return second; } /// <summary> /// Find the TFirst corresponing to the Second second. /// Throws an exception if second is not in the dictionary. /// </summary> /// <param name="second">the key to search for</param> /// <returns>the value corresponding to second</returns> public TFirst GetBySecond(TSecond second) { TFirst first; if (!secondToFirst.TryGetValue(second, out first)) throw new ArgumentException("second"); return first; } /// <summary> /// Remove the record containing first. /// If first is not in the dictionary, throws an Exception. /// </summary> /// <param name="first">the key of the record to delete</param> public void RemoveByFirst(TFirst first) { TSecond second; if (!firstToSecond.TryGetValue(first, out second)) throw new ArgumentException("first"); firstToSecond.Remove(first); secondToFirst.Remove(second); } /// <summary> /// Remove the record containing second. /// If second is not in the dictionary, throws an Exception. /// </summary> /// <param name="second">the key of the record to delete</param> public void RemoveBySecond(TSecond second) { TFirst first; if (!secondToFirst.TryGetValue(second, out first)) throw new ArgumentException("second"); secondToFirst.Remove(second); firstToSecond.Remove(first); } #endregion #region Try methods /// <summary> /// Tries to add the pair to the dictionary. /// Returns false if either element is already in the dictionary /// </summary> /// <param name="first"></param> /// <param name="second"></param> /// <returns>true if successfully added, false if either element are already in the dictionary</returns> public Boolean TryAdd(TFirst first, TSecond second) { if (firstToSecond.ContainsKey(first) || secondToFirst.ContainsKey(second)) return false; firstToSecond.Add(first, second); secondToFirst.Add(second, first); return true; } /// <summary> /// Find the TSecond corresponding to the TFirst first. /// Returns false if first is not in the dictionary. /// </summary> /// <param name="first">the key to search for</param> /// <param name="second">the corresponding value</param> /// <returns>true if first is in the dictionary, false otherwise</returns> public Boolean TryGetByFirst(TFirst first, out TSecond second) { return firstToSecond.TryGetValue(first, out second); } /// <summary> /// Find the TFirst corresponding to the TSecond second. /// Returns false if second is not in the dictionary. /// </summary> /// <param name="second">the key to search for</param> /// <param name="first">the corresponding value</param> /// <returns>true if second is in the dictionary, false otherwise</returns> public Boolean TryGetBySecond(TSecond second, out TFirst first) { return secondToFirst.TryGetValue(second, out first); } /// <summary> /// Remove the record containing first, if there is one. /// </summary> /// <param name="first"></param> /// <returns> If first is not in the dictionary, returns false, otherwise true</returns> public Boolean TryRemoveByFirst(TFirst first) { TSecond second; if (!firstToSecond.TryGetValue(first, out second)) return false; firstToSecond.Remove(first); secondToFirst.Remove(second); return true; } /// <summary> /// Remove the record containing second, if there is one. /// </summary> /// <param name="second"></param> /// <returns> If second is not in the dictionary, returns false, otherwise true</returns> public Boolean TryRemoveBySecond(TSecond second) { TFirst first; if (!secondToFirst.TryGetValue(second, out first)) return false; secondToFirst.Remove(second); firstToSecond.Remove(first); return true; } #endregion /// <summary> /// The number of pairs stored in the dictionary /// </summary> public Int32 Count { get { return firstToSecond.Count; } } /// <summary> /// Removes all items from the dictionary. /// </summary> public void Clear() { firstToSecond.Clear(); secondToFirst.Clear(); }}
0 0
- 根据value来定位key在C#的Dictionary中如何实现?
- C#中Dictionary<Key,Value>中[]操作的效率问题
- 根据值(value)获取泛型Dictionary的键(key)
- 根据索引获取Dictionary的key和value值
- iOS 根据某个value找到Dictionary对应的key值
- iOS 根据value取到 Dictionary 对应的key值
- C# Dictionary根据Key排序
- C# Dictionary根据Key排序
- C# Dictionary根据Key排序
- 在map中根据value获取key
- C# Dictionary通过value获取对应的key值
- C# Dictionary获取某项的key或value值
- C# Dictionary通过value获取对应的key值
- PHP的数组中如何根据某一个value值获取其key值
- LINQ 通过Dictionary的Value查找Key
- C#对Dictionary的Value排序功能实现
- 在php中如何实现根据用户的操作系统语言和浏览器语言来屏蔽用户
- redis是key-value存储的,放在内存中,并在磁盘持久化的数据结构存储系统 通过set key value来存储,通过get key来获取值 复制代码 set key value:设
- 15周项目1拓展3
- HTTP GET,线程的使用
- 马的走法
- 算法导论 堆排序 C语言实现
- sqlplus登陆方式
- 根据value来定位key在C#的Dictionary中如何实现?
- 黑马程序员--Java多线程
- 插入排序:表插入
- glPixelStorei
- POJ1015
- 6引擎
- 循环-13. 求特殊方程的正整数解(15)
- 华三工作(二天)
- CentOS 6.3 32位静默安装Oracle 11g r2详细教程