C# DictionaryHelper
1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Core.Common
{
/// <summary>
/// DictionaryHelper
/// </summary>
public static class DictionaryHelper
{
/// <summary>
/// Put 扩展字典方法 存在时更改,不存在时添加
/// </summary>
/// <typeparam name="TKey">Key的类型</typeparam>
/// <typeparam name="TValue">Value的类型</typeparam>
/// <param name="dictionary">Put之前字典</param>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
/// <returns>Put之后字典</returns>
public static Dictionary<TKey, TValue> Put<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
return dictionary.Put(key, value, (v) => true);
} /// <summary>
/// Put 扩展字典方法 存在时更改,不存在时添加
/// </summary>
/// <typeparam name="TKey">Key的类型</typeparam>
/// <typeparam name="TValue">Value的类型</typeparam>
/// <param name="targetDictionary">Put之前字典</param>
/// <param name="sourceDictionary">Key-Value</param>
/// <returns>Put之后字典</returns>
public static Dictionary<TKey, TValue> PutArray<TKey, TValue>(this Dictionary<TKey, TValue> targetDictionary, Dictionary<TKey, TValue> sourceDictionary)
{
return targetDictionary.PutArray(sourceDictionary, (v) => true);
} /// <summary>
/// Put 扩展字典方法 存在时更改,不存在时添加
/// </summary>
/// <typeparam name="TKey">Key的类型</typeparam>
/// <typeparam name="TValue">Value的类型</typeparam>
/// <param name="dictionary">Put之前字典</param>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
/// <param name="ignoreExists">是否忽视已存在项(不更改已存在项)</param>
/// <returns>Put之后字典</returns>
public static Dictionary<TKey, TValue> Put<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value, bool ignoreExists)
{
return dictionary.Put(key, value, (v) => !ignoreExists);
} /// <summary>
/// Put 扩展字典方法 存在时更改,不存在时添加
/// </summary>
/// <typeparam name="TKey">Key的类型</typeparam>
/// <typeparam name="TValue">Value的类型</typeparam>
/// <param name="dictionary">Put之前字典</param>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
/// <param name="replaceExistsValueCondition">替换已存在项的条件函数</param>
/// <returns>Put之后字典</returns>
public static Dictionary<TKey, TValue> Put<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value, Func<TValue, bool> replaceExistsValueCondition)
{
if (dictionary != null)
{
if (dictionary.ContainsKey(key) && replaceExistsValueCondition(value))
{
dictionary[key] = value;
}
else
{
dictionary.Add(key, value);
}
} return dictionary;
} /// <summary>
/// Put 扩展字典方法 存在时更改,不存在时添加
/// </summary>
/// <typeparam name="TKey">Key的类型</typeparam>
/// <typeparam name="TValue">Value的类型</typeparam>
/// <param name="targetDictionary">Put之前字典</param>
/// <param name="sourceDictionary">Key-Value</param>
/// <param name="ignoreExists">是否忽视已存在项(不更改已存在项)</param>
/// <returns>Put之后字典</returns>
public static Dictionary<TKey, TValue> PutArray<TKey, TValue>(this Dictionary<TKey, TValue> targetDictionary, Dictionary<TKey, TValue> sourceDictionary, bool ignoreExists)
{
return targetDictionary.PutArray(sourceDictionary, (v) => !ignoreExists);
} /// <summary>
/// Put 扩展字典方法 存在时更改,不存在时添加
/// </summary>
/// <typeparam name="TKey">Key的类型</typeparam>
/// <typeparam name="TValue">Value的类型</typeparam>
/// <param name="targetDictionary">Put之前字典</param>
/// <param name="sourceDictionary">Key-Value</param>
/// <param name="replaceExistsValueCondition">替换已存在项的条件函数</param>
/// <returns>Put之后字典</returns>
public static Dictionary<TKey, TValue> PutArray<TKey, TValue>(this Dictionary<TKey, TValue> targetDictionary, Dictionary<TKey, TValue> sourceDictionary, Func<TValue, bool> replaceExistsValueCondition)
{
if (sourceDictionary == null)
{
return targetDictionary;
} foreach (var keyValuePair in sourceDictionary)
{
targetDictionary.Put(keyValuePair.Key, keyValuePair.Value, replaceExistsValueCondition);
} return targetDictionary;
} /// <summary>
/// Put 扩展字典方法 存在时更改,不存在时添加
/// </summary>
/// <typeparam name="TKey">Key的类型</typeparam>
/// <typeparam name="TValue">Value的类型</typeparam>
/// <param name="dictionary">Put之前字典</param>
/// <param name="keyValuePair">Key-Value</param>
/// <returns>Put之后字典</returns>
public static Dictionary<TKey, TValue> Put<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, KeyValuePair<TKey, TValue> keyValuePair)
{
return dictionary.Put(keyValuePair.Key, keyValuePair.Value);
} /// <summary>
/// Put 扩展字典方法 存在时更改,不存在时添加
/// </summary>
/// <typeparam name="TKey">Key的类型</typeparam>
/// <typeparam name="TValue">Value的类型</typeparam>
/// <param name="dictionary">字典</param>
/// <param name="key">Key</param>
/// <returns>Value</returns>
public static TValue GetObjectWithoutException<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key) where TValue : class
{
if (dictionary != null && dictionary.ContainsKey(key))
{
return dictionary[key];
} return null;
} /// <summary>
/// 删除 扩展字典方法 存在时更改,不存在时添加
/// </summary>
/// <typeparam name="TKey">Key的类型</typeparam>
/// <typeparam name="TValue">Value的类型</typeparam>
/// <param name="dictionary">字典</param>
/// <param name="key">Key</param>
/// <returns>Value</returns>
public static void RemoveObjectWithoutException<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key) where TValue : class
{
if (dictionary != null && dictionary.ContainsKey(key))
{
dictionary.Remove(key);
}
} /// <summary>
/// 获取之后删除(类似队列的Pop) 扩展字典方法 存在时更改,不存在时添加
/// </summary>
/// <typeparam name="TKey">Key的类型</typeparam>
/// <typeparam name="TValue">Value的类型</typeparam>
/// <param name="dictionary">字典</param>
/// <param name="key">Key</param>
/// <returns>Value</returns>
public static TValue PopWithoutException<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key) where TValue : class
{
if (dictionary != null && dictionary.ContainsKey(key))
{
TValue item = dictionary[key];
dictionary.Remove(key);
return item;
} return null;
} /// <summary>
/// Put 扩展字典方法 存在时更改,不存在时添加
/// </summary>
/// <typeparam name="TKey">Key的类型</typeparam>
/// <typeparam name="TValue">Value的类型</typeparam>
/// <param name="dictionary">字典</param>
/// <param name="key">Key</param>
/// <returns>Value</returns>
public static TValue GetStructWithoutException<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key) where TValue : struct
{
if (dictionary != null && dictionary.ContainsKey(key))
{
return dictionary[key];
} return default(TValue);
} /// <summary>
/// 列表转换成字典(重复数据自动忽略)
/// </summary>
/// <typeparam name="TModel">Model的类型</typeparam>
/// <typeparam name="TKey">Key的类型</typeparam>
/// <typeparam name="TValue">Value的类型</typeparam>
/// <param name="list">列表</param>
/// <param name="keySelector">用于从每个元素中提取键的函数</param>
/// <param name="elementSelector">用于从每个元素产生结果元素值的转换函数</param>
/// <returns>Value</returns>
public static Dictionary<TKey, TValue> ToDictionaryWithoutException<TModel, TKey, TValue>(this IEnumerable<TModel> list, Func<TModel, TKey> keySelector, Func<TModel, TValue> elementSelector)
{
var dict = new Dictionary<TKey, TValue>();
if (list != null)
{
foreach (var model in list)
{
var key = keySelector(model);
var value = elementSelector(model);
dict.Put(key, value);
}
} return dict;
}
}
}
/// <summary>
/// 字典的字符串值
/// </summary>
/// <typeparam name="TKey">Key的类型</typeparam>
/// <typeparam name="TValue">Value的类型</typeparam>
/// <param name="dict">源列表</param>
/// <returns>字符串值</returns>
public static string ToStandardString<TKey, TValue>(this IDictionary<TKey, TValue> dict)
{
if (dict == null)
{
return "null";
} var enumerator = dict.GetEnumerator();
if (!enumerator.MoveNext())
{
return "{}";
} var sb = new StringBuilder();
sb.Append("{");
do
{
TKey key = enumerator.Current.Key;
TValue value = enumerator.Current.Value; sb.AppendFormat("\"{0}\":\"{1}\"", object.ReferenceEquals(key, dict) ? "(this Map)" : key.ToString(), object.ReferenceEquals(value, dict) ? "(this Map)" : enumerator.Current.Value.ToString());
if (enumerator.MoveNext())
{
sb.Append(",");
}
else
{
break;
}
} while (true); sb.Append("}"); return sb.ToString();
}
C# DictionaryHelper的更多相关文章
- DictionaryHelper
/// <summary> /// DictionaryHelper /// </summary> public static class DictionaryHelper { ...
- cocoStudio UI编辑器 学习总结
一.控件 控件基类 UIWidget:所有UI控件的基类 addChild:添加UIWidget类型的节点 addRenderer:添加CCNode类型的节点 所有UIWidget,都可以设置成触摸s ...
- cocos2d_x 问题汇总
1.生成so文件时,报“No rule to make target ”错误 解决方法:将.\xxx[appname]\proj.android\obj\local\armeabi\objs中的文件全 ...
- cocostudio内存释放
在使用cocostudio时,在释放内存时能够这样做: 在onExit()方法里加入例如以下: void LoadLayer::onExit() { // 释放本对象自己 removeFromPare ...
- C#文件夹权限操作工具类
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Sec ...
- jaron插件的用法
一.dict字典插件的基本用法: <%@ taglib prefix="dict" uri="http://www.evan.jaron.com/tags/dict ...
- DictionaryHelper2
/// <summary> /// DictionaryHelper /// </summary> public static class DictionaryHelper { ...
- C#工具类之字典扩展类
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...
- 3个N加上各种运算符号结果等于6(纯属娱乐)C#
网上的题目: 题有点难 但都有解 2 2 2 = 6 3 3 3 = 6 4 4 4 = 6 5 5 5 = 6 6 6 ...
随机推荐
- p5156 [USACO18DEC]Sort It Out
传送门 分析 我们发现对于没有发现的点相对位置不会发生改变 于是我们可以吧问题转化为求一个lis 于是我们字典序第k小的答案就是字典序第k大的lis 代码 #include<iostream&g ...
- 235D Graph Game
传送门 题目大意 https://www.luogu.org/problemnew/show/CF235D 分析 我们先考虑它是树的情况 我们设$event(x,y)$表示删除点x是y与x联通这件事对 ...
- oracle数据库列的操作
本章和大家分享一下如何在数据库中进行列的一些相关操作. 1.增加列名 (我们先来看一个原始版本) 下面我们增加一个列名tel 记住,增加列时需要把列对应的数据类型要说明,不然会报错. alter t ...
- Linux相关常用工具
Xshell Xshell可以在Windows界面下用来访问远端不同系统下的服务器,从而比较好的达到远程控制终端的目的. 通常需要通过vpn访问.建立vpn隧道可以通过FortiClient 或者 I ...
- 【2008nmj】Logistic回归二元分类感知器算法.docx
给你一堆样本数据(xi,yi),并标上标签[0,1],让你建立模型(分类感知器二元),对于新给的测试数据进行分类. 要将两种数据分开,这是一个分类问题,建立数学模型,(x,y,z),z指示[0,1], ...
- jquery操作select(取值,设置选中) 基础
每一次操作select的时候,总是要出来翻一下资料,不如自己总结一下,以后就翻这里了. 比如<select class="selector"></select&g ...
- cmake安装方法
由于Ubuntu14.04的cmake版本为2.8.x,而如果需要cmake3.x版本时,无法生成makefile,有两种方法可以安装cmake3.10.0: 方法1: sudo apt-get in ...
- java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit(IILjava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)V
异常完整信息 严重: Servlet.service() for servlet RegServlet threw exception java.lang.NoSuchMethodError: org ...
- Sharepoint2013搜索学习笔记之自定义结果显示模板(九)
搜索结果通过套用定义好的显示模板来展示结果,显示模板由js和html组成,我们可以通过修改显示模板,然后将修改好的显示模板跟搜索结果绑定起来,来修改搜索结果的显示效果,例子如下图: 修改前 修改后 第 ...
- Win7下:编译器错误信息: CS0016
解决办法: 原因是由于系统目录下的Temp目录无相应的权限所致,具体操作如下: 来到C:/Windows目录,修改temp文件夹的属性. 在安全页设置IIS-IUSRS的权限,赋予修改.读取.写入等权 ...