網站首頁 編程語言 正文
一、實現效果
1.1實現的功能
①添加信息到字典中;
②根據鍵獲取值;
③根據值獲取鍵;
④修改指定鍵的值;
⑤修改指定值為相同信息;
⑥根據鍵移除信息;
⑦根據值移除信息;
1.2實現的功能效果圖
二、實現核心
/***
* Title:"容器" 項目
* 主題:Dictionary的幫助類
* Description:
* 功能:
* ①添加信息到字典中
* ②根據鍵獲取值
* ③根據值獲取鍵
* ④修改指定鍵的值
* ⑤修改指定值為相同信息
* ⑥根據鍵移除信息
* ⑦根據值移除信息
* Version:0.1版本
* Author:Coffee
* Modify Recoder:
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Utils
{
public class DictionaryHelper
{
/// <summary>
/// 添加信息到字典中
/// </summary>
/// <typeparam name="TKey">鍵類型</typeparam>
/// <typeparam name="TValue">值類型</typeparam>
/// <param name="dic">字典</param>
/// <param name="key">需添加的鍵</param>
/// <param name="value">需添加的值</param>
public static void AddInfoToDic<TKey, TValue>(Dictionary<TKey, TValue> dic, TKey key, TValue value)
{
if (dic == null)
{
dic = new Dictionary<TKey, TValue>();
}
if (dic.ContainsKey(key))
{
dic[key] = value;
}
else
{
dic.Add(key, value);
}
}
/// <summary>
/// 根據鍵獲取值
/// </summary>
/// <typeparam name="TKey">鍵類型</typeparam>
/// <typeparam name="TValue">值類型</typeparam>
/// <param name="dic">字典</param>
/// <param name="key">鍵</param>
/// <returns>返回鍵對應的值</returns>
public static TValue GetValueOfKey<TKey, TValue>(Dictionary<TKey, TValue> dic, TKey key)
{
TValue tmpValue = default(TValue);
if (dic != null && dic.Count > 0)
{
if (dic.ContainsKey(key))
{
tmpValue = dic[key];
}
}
return tmpValue;
}
/// <summary>
/// 根據值獲取鍵
/// </summary>
/// <typeparam name="TKey">鍵類型</typeparam>
/// <typeparam name="TValue">值類型</typeparam>
/// <param name="dic">字典</param>
/// <param name="value">值</param>
/// <returns>返回值對應的所有鍵</returns>
public static List<TKey> GetKeyOfValue<TKey, TValue>(Dictionary<TKey, TValue> dic, TValue value)
{
List<TKey> keyList = new List<TKey>();
foreach (KeyValuePair<TKey, TValue> kv in dic)
{
if (kv.Value.Equals(value))
{
TKey tmpKey = kv.Key;
keyList.Add(tmpKey);
}
}
return keyList;
}
/// <summary>
/// 修改指定鍵的值
/// </summary>
/// <typeparam name="TKey">鍵類型</typeparam>
/// <typeparam name="TValue">值類型</typeparam>
/// <param name="dic">字典</param>
/// <param name="needModifyKey">需要修改的鍵</param>
/// <param name="replaceValue">需要替換的值</param>
/// <returns>返回修改結果(true:表示成功)</returns>
public static bool ModifyInfoOfKey<TKey, TValue>(Dictionary<TKey, TValue> dic, TKey needModifyKey, TValue replaceValue)
{
if (dic == null || dic.Count < 1) return false;
if (dic.ContainsKey(needModifyKey))
{
dic[needModifyKey] = replaceValue;
return true;
}
else
{
return false;
}
}
/// <summary>
/// 修改指定值為相同信息
/// </summary>
/// <typeparam name="TKey">鍵類型</typeparam>
/// <typeparam name="TValue">值類型</typeparam>
/// <param name="dic">字典</param>
/// <param name="needModifyValue">需要修改的值</param>
/// <param name="replaceValue">需要替換的值</param>
public static void ModifyInfoOfValue<TKey, TValue>(Dictionary<TKey, TValue> dic, TValue needModifyValue, TValue replaceValue)
{
if (dic == null || dic.Count < 1) return;
for (int i = 0; i < dic.Count;)
{
TValue tmpValue = dic.ElementAt(i).Value;
if (tmpValue.Equals(needModifyValue))
{
TKey tmpKey = dic.ElementAt(i).Key;
dic[tmpKey] = replaceValue;
i = 0;
}
else
{
i++;
}
}
}
/// <summary>
/// 根據鍵移除信息
/// </summary>
/// <typeparam name="TKey">鍵類型</typeparam>
/// <typeparam name="TValue">值類型</typeparam>
/// <param name="dic">字典</param>
/// <param name="needDeleteKey">需要刪除的鍵</param>
/// <returns>返回移除結果(true:表示成功)</returns>
public static bool RemoveInfoOfKey<TKey, TValue>(Dictionary<TKey, TValue> dic,TKey needDeleteKey)
{
if (dic.ContainsKey(needDeleteKey))
{
dic.Remove(needDeleteKey);
return true;
}
else
{
return false;
}
}
/// <summary>
/// 根據值移除信息
/// </summary>
/// <typeparam name="TKey">鍵類型</typeparam>
/// <typeparam name="TValue">值類型</typeparam>
/// <param name="dic">字典</param>
/// <param name="needDeleteValue">需要刪除的值</param>
/// <returns>返回結果(true:表示成功)</returns>
public static bool RemoveInfoOfValue<TKey, TValue>(Dictionary<TKey, TValue> dic, TValue needDeleteValue)
{
if (dic == null || dic.Count < 1) return false;
int initCount = dic.Count;
for (int i = 0; i < dic.Count;)
{
TValue tmpValue = dic.ElementAt(i).Value;
if (tmpValue.Equals(needDeleteValue))
{
TKey tmpKey = dic.ElementAt(i).Key;
dic.Remove(tmpKey);
i = 0;
}
else
{
i++;
}
}
if (initCount > dic.Count)
{
return true;
}
else
{
return false;
}
}
}//Class_end
}
三、使用方法
3.1引用命名空間
using Utils;
3.2使用示例
using System;
using System.Collections.Generic;
using Utils;
namespace Test_Dictionary
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
//獲取到字典信息
Dictionary<string, string> dic = GetDictionary();
Console.WriteLine($"1-開始獲取到的字典的所有信息如下:");
ShowInfoOfDic(dic);
//根據鍵獲取到對應的值
string queryKey = "L1";
Console.WriteLine($"當前查詢的鍵是:{queryKey}");
string tmpValue = DictionaryHelper.GetValueOfKey(dic,queryKey);
Console.WriteLine($"2-獲取到——鍵:L1對應的值是:{tmpValue}");
//根據值獲取到對應的所有鍵
string queryValue = "23.4";
Console.WriteLine($"當前查詢的值是:{queryValue}");
List<string> tmpKey = DictionaryHelper.GetKeyOfValue(dic, queryValue);
ShowInfoOfList(tmpKey);
//修改指定鍵的值
string needModifyKey = "L4";
string replaceValue1 = "66";
Console.WriteLine($"當前需要修改的鍵是:{needModifyKey}_替換為的值是:{replaceValue1}");
DictionaryHelper.ModifyInfoOfKey(dic, needModifyKey, replaceValue1);
Console.WriteLine($"修改的鍵是:{needModifyKey}_替換為的值是:{replaceValue1}后所有內容如下:");
ShowInfoOfDic(dic);
//修改指定值為相同信息
string needModifyValue = "23.6";
string replaceValue = "33.9";
Console.WriteLine($"當前需要修改的值是:{needModifyValue}_替換為的值是:{replaceValue}");
DictionaryHelper.ModifyInfoOfValue(dic,needModifyValue,replaceValue);
Console.WriteLine($"修改的值是:{needModifyValue}_替換為的值是:{replaceValue}后所有內容如下:");
ShowInfoOfDic(dic);
//根據鍵移除信息
string curRemoveKey = "L3";
Console.WriteLine($"當前移除的鍵是:{curRemoveKey}");
DictionaryHelper.RemoveInfoOfKey(dic,curRemoveKey);
Console.WriteLine($"移除的鍵是:{curRemoveKey}后所有內容如下:");
ShowInfoOfDic(dic);
//根據值移除信息
string curRemoveValue = "23.4";
Console.WriteLine($"當前移除的值是:{curRemoveValue}");
DictionaryHelper.RemoveInfoOfValue(dic, curRemoveValue);
Console.WriteLine($"移除的值是:{curRemoveValue}后所有內容如下:");
ShowInfoOfDic(dic);
Console.ReadLine();
}
//獲取一個字典
public static Dictionary<string, string> GetDictionary()
{
Dictionary<string, string> dic = new Dictionary<string, string>();
DictionaryHelper.AddInfoToDic(dic, "L1","23.4");
DictionaryHelper.AddInfoToDic(dic, "L2", "23.6");
DictionaryHelper.AddInfoToDic(dic, "L3", "23.8");
DictionaryHelper.AddInfoToDic(dic, "L4", "23.4");
DictionaryHelper.AddInfoToDic(dic, "L5", "23.6");
DictionaryHelper.AddInfoToDic(dic, "L6", "23.4");
return dic;
}
//顯示字典中的所有信息
private static void ShowInfoOfDic(Dictionary<string,string> dic)
{
if (dic == null || dic.Count < 1) return;
foreach (var item in dic)
{
Console.WriteLine($"鍵:{item.Key} 值:{item.Value}");
}
Console.WriteLine($"--------------顯示信息完成______當前字典:{dic.GetType().Name} 共有數據:{dic.Count} 條\r\n");
}
//顯示列表信息
private static void ShowInfoOfList(List<string> list)
{
if (list == null || list.Count < 1) return;
foreach (var item in list)
{
Console.WriteLine($"對應內容:{item}");
}
Console.WriteLine($"--------------顯示信息完成______當前列表:{list.GetType().Name} 共有數據:{list.Count} 條\r\n");
}
}//Class_end
}
原文鏈接:https://coffeemilk.blog.csdn.net/article/details/123827824
相關推薦
- 2022-07-19 Ribbon負載均衡深入探究
- 2022-03-07 C++使用TinyXML解析XML_C 語言
- 2022-11-13 kvm?透傳顯卡至win10虛擬機的方法_Kvm
- 2022-09-12 python?通過dict(zip)和{}的方式構造字典的方法_python
- 2022-05-10 Element-ui 中<template slot-scope=“scope“> 的用法問題以及剖
- 2024-01-10 Maven導入org.apache.commons.lang3.StringUtils
- 2022-08-16 在WPF中使用Interaction.Triggers_C#教程
- 2022-05-08 python如何生成密碼字典_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支