using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace G2.Utils
{
/// <summary>
/// 字典帮助类
/// </summary>
public static class DictionaryHelper
{
/// <summary>
/// 字典排序(默认排序)
/// </summary>
/// <typeparam name="TKey">Key类型</typeparam>
/// <typeparam name="TValue">Value类型</typeparam>
/// <param name="dictionary">无序字典</param>
/// <returns>有序字典</returns>
public static IDictionary<TKey, TValue> Sort<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
{
if (dictionary == null)
{
throw new ArgumentNullException("字典为空,不能排序");
} return new SortedDictionary<TKey, TValue>(dictionary);
} /// <summary>
/// 字典排序(自定义排序)
/// </summary>
/// <typeparam name="TKey">Key类型</typeparam>
/// <typeparam name="TValue">Value类型</typeparam>
/// <param name="dictionary">无序字典</param>
/// <param name="comparer">自定义排序方法</param>
/// <returns>有序字典</returns>
public static IDictionary<TKey, TValue> Sort<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, IComparer<TKey> comparer)
{
if (dictionary == null)
{
throw new ArgumentNullException("字典为空,不能排序");
} if (comparer == null)
{
throw new ArgumentNullException("自定义排序方法为空,不能排序");
} return new SortedDictionary<TKey, TValue>(dictionary, comparer);
} /// <summary>
/// 字典排序(依据值默认排序)
/// </summary>
/// <typeparam name="TKey">Key类型</typeparam>
/// <typeparam name="TValue">Value类型</typeparam>
/// <param name="dictionary">无序字典</param>
/// <returns>有序字典</returns>
public static IDictionary<TKey, TValue> SortByValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
{
return dictionary.OrderBy(v => v.Value).ToDictionary(item => item.Key, item => item.Value);
} /// <summary>
/// 字典排序(依据值自定义排序)
/// </summary>
/// <typeparam name="TKey">Key类型</typeparam>
/// <typeparam name="TValue">Value类型</typeparam>
/// <param name="dictionary">无序字典</param>
/// <param name="comparer">自定义排序方法</param>
/// <returns>有序字典</returns>
public static IDictionary<TKey, TValue> SortByValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, IComparer<TValue> comparer)
{
return dictionary.OrderBy(v => v.Value, comparer).ToDictionary(item => item.Key, item => item.Value);
} /// <summary>
/// 判断字典是否为空或者无数据
/// </summary>
/// <param name="dictionary">字典</param>
/// <returns>True:字典为空或没有数据;否则为False</returns>
public static bool IsEmpty<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
{
if (dictionary == null)
{
throw new ArgumentNullException("字典为空");
} return dictionary.Count == ;
} /// <summary>
/// 判断字典是否为空或者无数据
/// </summary>
/// <param name="dictionary">字典</param>
/// <returns>True:字典为空或没有数据;否则为False</returns>
public static bool IsNullOrEmpty<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
{
return dictionary == null || dictionary.Count == ;
} /// <summary>
/// 获取字典值(如果不包含该值则由自定义方法返回)
/// </summary>
/// <typeparam name="TKey">Key类型</typeparam>
/// <typeparam name="TValue">Value类型</typeparam>
/// <param name="dictionary">字典</param>
/// <param name="key">Key值</param>
/// <param name="function">自定义方法</param>
/// <returns>字典里的值或者自定义方法返回值</returns>
public static TValue GetOrGetByFunction<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TValue> function)
{
if (dictionary == null)
{
throw new ArgumentNullException("字典为空");
} if (dictionary.ContainsKey(key))
{
return dictionary[key];
} if (function == null)
{
throw new ArgumentNullException("自定义取值方法为空,不能获取值");
} return function();
} /// <summary>
/// 新增或重写(如果没有该Key值,则新增;否则重写该值)
/// </summary>
/// <typeparam name="TKey">Key类型</typeparam>
/// <typeparam name="TValue">Value类型</typeparam>
/// <param name="dictionary">字典</param>
/// <param name="key">Key值</param>
/// <param name="value">Value新值</param>
/// <returns>字典</returns>
public static IDictionary<TKey, TValue> AddOrSet<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
if (dictionary == null)
{
throw new ArgumentNullException("字典为空");
} if (dictionary.ContainsKey(key))
{
dictionary[key] = value;
}
else
{
dictionary.Add(new KeyValuePair<TKey, TValue>(key, value));
} return dictionary;
}
}
}

C#工具类之字典扩展类的更多相关文章

  1. [Django REST framework - 视图组件之视图基类、视图扩展类、视图子类、视图集]

    [Django REST framework - 视图组件之视图基类.视图扩展类.视图子类.视图集] 视图继承关系 详图见文章末尾 视图组件可点我查看 两个视图基类:APIView.GenericAP ...

  2. UI(UGUI)框架(二)-------------UIManager单例模式与开发BasePanel面板基类/UIManage统一管理UI面板的实例化/开发字典扩展类

    UIManage单实例: /// 单例模式的核心 /// 1,定义一个静态的对象 在外界访问 在内部构造 /// 2,构造方法私有化 private static UIManager _instanc ...

  3. C#工具类之字符串扩展类

    /// <summary> /// 字典串帮忙类 /// </summary> public static class StringHelper { /// <summa ...

  4. C#工具类之素数扩展类

    /// <summary> /// 素数帮忙类 /// 本类是从.net源码 类 internal static class HashHelpers 类里抽取相应的代码 /// https ...

  5. C#工具类之XmlNode扩展类

    using System; using System.Linq; using System.Xml; /// <summary> /// XmlNodeHelper /// </su ...

  6. C#工具类之日期扩展类

    /// <summary> /// DateTimeHelper /// </summary> public static class DateTimeHelper { /// ...

  7. c#工具类之Int扩展类

    public static class IntHelper { /// <summary> /// 转换为2进制字符串 /// </summary> /// <param ...

  8. DRF框架(五)——context传参,二次封装Response类,两个视图基类(APIView/GenericAPIView),视图扩展类(mixins),子类视图(工具视图),视图集(viewsets),工具视图集

    复习 1.整体修改与局部修改 # 序列化get (给前端传递参数) #查询 ser_obj = ModelSerializer(model_obj) #只传递一个参数,默认是instance的参数,查 ...

  9. ios开发总结:Utils常用方法等收集,添加扩展类,工具类方法,拥有很多方便快捷功能(不断更新中。。。)

    BOBUtils 工具大全 本人github开源和收集功能地址:https://github.com/niexiaobo [对ios新手或者工作一年以内开发人员很有用处] 常用方法等收集.添加扩展类. ...

随机推荐

  1. Hbase批量插入优化记录

    2016年5月11日10:08:29 hbase原本的put方式是一条一条的put,在客户端修改 AutoFlush 将HTable的setAutoFlush设为false,可以支持客户端批量更新.即 ...

  2. 跨域Ajax原理以及浏览器同源策略

  3. 2-4 zookeeper配置文件介绍,运行zk

    心跳机制就是超过一定的时间之后,那么这个从节点就会被抛弃. zookeeper需要存储的数据,比如说事务文件等等,它都会存到这个dataDir目录下. 如果是伪分布式的集群环境,那么它的端口肯定是要变 ...

  4. koa2 原生链接mysql

    1.安装mysql $ npm install mysql 2.代码示例: const mysql = require("mysql"); // mysql.Promise = g ...

  5. 【Java学习】Java迭代器

    迭代器是一种模式,它可以使得对于序列类型的数据结构的遍历行为与被遍历的对象分离,即我们无需关心该序列的底层结构是什么样子的.只要拿到这个对象,使用迭代器就可以遍历这个对象的内部. 1.Iterator ...

  6. Java50道经典习题-程序26 求星期

    题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续 判断第二个字母.分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母.周一至周日的英文单词 ...

  7. SVM浅析

    系列博客机器学习总结,主要参考书目<统计学习方法>--李航,涉及数学公式较多,以图片的形式表现.SVM是经典的线性分类方法,通过线性映射投射到希尔伯特空间(完备的赋范内积空间)得到了无穷维 ...

  8. 818. Race Car

    Your car starts at position 0 and speed +1 on an infinite number line.  (Your car can go into negati ...

  9. jest+vue-test-utils初步实践

    一.起步 1. jest Jest是 Facebook 的一套开源的 JavaScript 测试框架, 它自动集成了断言.JSDom.覆盖率报告等开发者所需要的所有测试工具,配置较少,对vue框架友好 ...

  10. JavaScript之DOM实践

    前言 HTML的DOM是JavaScript有能力对HTML作出反应,有时候,我们需要一些网页效果,为了更好地适应用户的效果,比如,我们平时接触,点击某个按钮,按下去的瞬间会变色,再比如,有时候鼠标经 ...