zoukankan      html  css  js  c++  java
  • C# 字典的扩展方法

            /// <summary>
            /// 根据条件删除字典中所有符合的键值对
            /// </summary>
            /// <returns></returns>
            public static int RemoveAll<TKey, TValue>(this IDictionary<TKey, TValue> source, Func<TKey, TValue, bool> predicate)
            {
                List<TKey> listTKey = new List<TKey>();
                foreach (KeyValuePair<TKey, TValue> kv in source)
                {
                    if (predicate(kv.Key, kv.Value))
                    {
                        listTKey.Add(kv.Key);
                    }
                }
                foreach (TKey k in listTKey)
                {
                    source.Remove(k);
                }
                return listTKey.Count;
            }
    
            /// <summary>
            /// 判断字典中是否存在符合条件的键值对
            /// </summary>
            public static bool Exists<TKey, TValue>(this IDictionary<TKey, TValue> source, Func<TKey, TValue, bool> predicate)
            {
                List<TKey> listTKey = new List<TKey>();
                foreach (KeyValuePair<TKey, TValue> kv in source)
                {
                    if (predicate(kv.Key, kv.Value))
                    {
                        return true;
                    }
                }
                return false;
            }
    
  • 相关阅读:
    java降序排列
    冒泡排序-java
    redis-并发竞争问题
    超卖问题
    算法-题目汇总-6
    算法-题目汇总-4
    算法-题目汇总-1
    算法-二分查找-1
    算法-bst-平衡搜索二叉树-1
    算法-位运算-1
  • 原文地址:https://www.cnblogs.com/bridgew/p/12709074.html
Copyright © 2011-2022 走看看