C# 判断两个集合(List)是否相等
1.两个list如果有重复元素(如List1: a,b,a List2: b,b,a) 是无法通过包含关系来判断是否相等的.
有两个办法,其一是两个List排序后再按顺序比较.另一个办法就是计算各元素的重复项再进行比较
第一种方案劣势太明显,时间复杂度过大
第二种以空间换时间,只需要遍历无需排序即可.
/// <summary>
/// 判断两个集合是否是相等的(所有的元素及数量都相等)
/// </summary>
/// <typeparam name="T">集合元素类型</typeparam>
/// <param name="sourceCollection">源集合列表</param>
/// <param name="targetCollection">目标集合列表</param>
/// <returns>两个集合相等则返回True,否则返回False</returns>
public static bool EqualList<T>(this IList<T> sourceCollection, IList<T> targetCollection) where T : IEquatable<T>
{
//空集合直接返回False,即使是两个都是空集合,也返回False
if (sourceCollection == null || targetCollection == null)
{
return false;
} if (object.ReferenceEquals(sourceCollection, targetCollection))
{
return true;
} if (sourceCollection.Count != targetCollection.Count)
{
return false;
} var sourceCollectionStaticsDict = sourceCollection.StatisticRepetition();
var targetCollectionStaticsDict = targetCollection.StatisticRepetition(); return sourceCollectionStaticsDict.EqualDictionary(targetCollectionStaticsDict);
} /// <summary>
/// 判断两个字典是否是相等的(所有的字典项对应的值都相等)
/// </summary>
/// <typeparam name="TKey">字典项类型</typeparam>
/// <typeparam name="TValue">字典值类型</typeparam>
/// <param name="sourceDictionary">源字典</param>
/// <param name="targetDictionary">目标字典</param>
/// <returns>两个字典相等则返回True,否则返回False</returns>
public static bool EqualDictionary<TKey, TValue>(this Dictionary<TKey, TValue> sourceDictionary, Dictionary<TKey, TValue> targetDictionary)
where TKey : IEquatable<TKey>
where TValue : IEquatable<TValue>
{
//空字典直接返回False,即使是两个都是空字典,也返回False
if (sourceDictionary == null || targetDictionary == null)
{
return false;
} if (object.ReferenceEquals(sourceDictionary, targetDictionary))
{
return true;
} if (sourceDictionary.Count != targetDictionary.Count)
{
return false;
} //比较两个字典的Key与Value
foreach (var item in sourceDictionary)
{
//如果目标字典不包含源字典任意一项,则不相等
if (!targetDictionary.ContainsKey(item.Key))
{
return false;
} //如果同一个字典项的值不相等,则不相等
if (!targetDictionary[item.Key].Equals(item.Value))
{
return false;
}
} return true;
} /// <summary>
/// 统计集合的重复项,并返回一个字典
/// </summary>
/// <typeparam name="T">集合元素类型</typeparam>
/// <param name="sourceCollection">统计集合列表</param>
/// <returns>返回一个集合元素及重复数量的字典</returns>
private static Dictionary<T, int> StatisticRepetition<T>(this IEnumerable<T> sourceCollection) where T : IEquatable<T>
{
var collectionStaticsDict = new Dictionary<T, int>();
foreach (var item in sourceCollection)
{
if (collectionStaticsDict.ContainsKey(item))
{
collectionStaticsDict[item]++;
}
else
{
collectionStaticsDict.Add(item, );
}
} return collectionStaticsDict;
}
2
public class CommonTest
{
/// <summary>
/// 集合相等比较
/// </summary>
[Fact]
public void ListEqual_Tests()
{
var sourceList = new List<string>()
{
"a",
"b",
"a"
}; var targetList = new List<string>()
{
"b",
"b",
"a"
}; var resp = sourceList.EqualList(targetList);
Assert.False(resp );
} /// <summary>
/// 集合相等比较
/// </summary>
[Fact]
public void ListEqual2_Tests()
{
var sourceList = new List<string>()
{
"a",
"b",
}; var targetList = new List<string>()
{
"b",
"a"
}; var resp = sourceList.EqualList(targetList);
Assert.True(resp);
}
}
C# 判断两个集合(List)是否相等的更多相关文章
- c# 判断两个集合是否有交集
/// <summary> /// 判断是否有交集 /// </summary> /// <typeparam name="T"></ty ...
- 计算两个集合的差集——第六期 Power8 算法挑战赛
第六期Power8大赛 1.1 比赛题目 题目: 计算两个集合的差集: 详细说明: 分别有集合A和B两个大数集合,求解集合A与B的差集(A中有,但B中无的元素),并将结果保存在集合C中,要求集合C中的 ...
- 【java】【反射】反射实现判断发生了修改操作,判断两个对象是否发生属性值的变更,判断两个List集合内对象的属性值是否发生变更
java的反射实现: 判断发生了修改操作,判断两个对象是否发生属性值的变更,判断两个List集合内对象的属性值是否发生变更 今日份代码: package com.sxd.streamTest; imp ...
- 求两个集合的交集和并集C#
我是用hashset<T>来实现的 具体如代码所示 using System; using System.Collections.Generic; using System.Linq; u ...
- 一个diff工具,用于判断两个目录下所有的改动(比较新旧版本文件夹)
需求: 编写一个diff工具,用于判断两个目录下所有的改动 详细介绍: 有A和B两个目录,目录所在位置及层级均不确定 需要以B为基准找出两个目录中所有有改动的文件(文件或内容增加.修改.删除),将有改 ...
- 51Nod 1557 两个集合(二分)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1557 题意: 小X有n个互不相同的整数: p1,p2,...,pn .他 ...
- poj 1127:Jack Straws(判断两线段相交 + 并查集)
Jack Straws Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2911 Accepted: 1322 Descr ...
- c#封装DBHelper类 c# 图片加水印 (摘)C#生成随机数的三种方法 使用LINQ、Lambda 表达式 、委托快速比较两个集合,找出需要新增、修改、删除的对象 c# 制作正方形图片 JavaScript 事件循环及异步原理(完全指北)
c#封装DBHelper类 public enum EffentNextType { /// <summary> /// 对其他语句无任何影响 /// </summary> ...
- 51nod 1557 两个集合 (严谨的逻辑题)
题目: 1557 两个集合 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 小X有n个互不相同的整数: p1,p2,...,pn ...
随机推荐
- 20-取石子动态规则(hdu2516 斐波那契博弈)
http://acm.hdu.edu.cn/showproblem.php?pid=2516 取石子游戏 Time Limit: 2000/1000 MS (Java/Others) Memor ...
- linux ssh使用深度解析(key登录详解)
linux ssh使用深度解析(key登录详解) SSH全称Secure SHell,顾名思义就是非常安全的shell的意思,SSH协议是IETF(Internet Engineering Task ...
- python操作Redis缓存
python操作Redis缓存 https://www.cnblogs.com/guotianbao/p/8683037.html 学习资料:电子书资源 联系邮箱:gmu1592618@gmail.c ...
- Python字符编码详解,str,bytes
什么是明文 “明文”是可以是文本,音乐,可以编码成mp3文件.明文可以是图像的,可以编码为gif.png或jpg文件.明文是电影的,可以编码成wmv文件.不一而足. 什么是编码?把明文变成计算机语言 ...
- python2.7响应数据中unicode转中文
print ("响应结果:%s" % r.content.decode('unicode_escape')) 一. 在爬虫抓取网页信息时常需要将类似"\u4eba\u75 ...
- Django工程中使用echarts怎么循环遍历显示数据
前言: 后面要开发测试管理平台,需要用到数据可视化,所以研究了一下 先看下最后的图吧,单击最上方的按钮可以控制柱状图显隐 views.py # -*- coding: utf-8 -*- from _ ...
- Sobel算法
最近看了一些Sobel算法,并试了一下,源码如下: private void Sobel(Bitmap img) { int width = img.Width; int height = img.H ...
- Topshelf + Quartz2.5 创建基于windows服务
1.创建一个定时调度Quartz类 using Quartz; using Quartz.Impl; using System; using System.Collections.Generic; u ...
- html5 video使用autoplay属性时,声音混乱
html5 video使用autoplay属性时,声音混乱 页面代码 Index.html <html xmlns="http://www.w3.org/1999/xhtml" ...
- 二、安装Node.js和npm
1.Note的各个版本官方下载地址: https://nodejs.org/en/download/releases/ 这里我们选择7.6版本为例进行下载安装: 根据自己的情况下载对应的msi安装包 ...