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 ...
随机推荐
- Luogu 3242 [HNOI2015]接水果
BZOJ4009 权限题 真的不想再写一遍了 大佬blog 假设有果实$(x, y)$,询问$(a, b)$,用$st_i$表示$i$的$dfs$序,用$ed_i$表示所有$i$的子树搜完的$dfs$ ...
- Luogu 1099 树网的核
bzoj1999 数据加强版(n <= 5e5) 较早的noip题,值得研究 重要结论:直径的最长性,任何从直径中离开直径的点到它离开的点的距离,都不会比直径的另一端到它离开的点长(否则就有新的 ...
- php连接redis
$redis = new Redis(); $redis->connect();
- logback 中文手册
摘自:http://aub.iteye.com/blog/1896611 logback 中文手册 博客分类: Log loglogbackloback手册loback中文手册 logback 常 ...
- Entity Framework 6.0 Tutorials(6):Transaction support
Transaction support: Entity Framework by default wraps Insert, Update or Delete operation in a trans ...
- 第16章-使用Spring MVC创建REST API
1 了解REST 1.1 REST的基础知识 REST与RPC几乎没有任何关系.RPC是面向服务的,并关注于行为和动作:而REST是面向资源的,强调描述应用程序的事物和名词. 为了理解REST是什么, ...
- 编写高质量代码改善C#程序的157个建议——建议7: 将0值作为枚举的默认值
建议7: 将0值作为枚举的默认值 允许使用的枚举类型有byte.sbyte.short.ushort.int.uint.long和ulong.应该始终将0值作为枚举类型的默认值.不过,这样做不是因为允 ...
- 【转】开源视频录制库LandscapeVideoCamera
非常强大的android 视频录制库,可以选择视频尺寸以及视频质量,只允许横屏录制. 使用Android自带的Camera应用可以录制视频,只需发送MediaStore.ACTION_VIDEO_CA ...
- 21天学通C++学习笔记(四):数组和字符串
1. 数组 概念 是一组元素 这些元素是相同的数据类型 按顺序存储到内存中 目的是避免在业务需要时去重复声明很多同类型的变量 初始化 分别初始化:int i [5] = {1,2,3,4,5}; 全部 ...
- Mybatis注解开发
mybatis 的常用注解: @Insert:实现新增 @Update:实现更新 @Delete:实现删除 @Select:实现查询 @Result:实现结果集封装 @Results:可以与 @Res ...