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 ...
随机推荐
- Shell +Cygwinterminal+WinMySQL 传参数授权
前言:新公司因为部分业务原因有好几百组win机器装MySQL授权登录比较麻烦,简单的写了一个shell传值自动授权的脚本,保存复用. #!/bin/bash #author liding@zlhy.c ...
- libcurl用法
本文以向百度搜索开放平台搜索关键字所对应的推荐搜索条目为例子: url:http://m.baidu.com/su?wd=%s&action=opensearch&ie=utf-8 ( ...
- 模板模式和Comparable类
模板模式中,父类规定好了一些算法的流程,并且空出一些步骤(方法)留给子类填充 Java的数组类中静态方法sort()就是一个模板,它空出了一个compareTo的方法,留给子类填充,用来规定什么是大于 ...
- 权限管理RBAC
四张表: 1.module:id/name //模块 2.action:id /module_id/name //权限 3.user:id/name //用户表 4.group:id/user_id ...
- python,itertools模块
itertools模块的使用: # coding=utf-8 """ itertools模块 """ import itertools im ...
- Monkey测试异常信息解读
查看包名 1.cmd 下面输入 adb locat > D:\test.txt 2.ctrl+c 停掉刚刚 1 运行的进程 3.打开test.txt文件--搜索 Displayed 对应的内 ...
- App测试从入门到精通之UI测试
UI(user interface用户界面)的简称.UI测试也是APP测试中需要考虑的一个层面.用户至上,这个太重要了.一个好的App在界面的UI层设计上应该要满足简洁.美观.大气(这个是自己感觉的哈 ...
- 线程同步synchronized,wait,notifyAll 测试示例
https://www.cnblogs.com/LipeiNet/p/6475851.html 一 synchronized synchronized中文解释是同步,那么什么是同步呢,解释就是程序中 ...
- WinForm中自定义搜索框(水印、清空按钮、加载中图标)
public partial class CustomSearchBar : TextBox { private readonly Label lblwaterText = new Label(); ...
- NSEnumerator迭代器
前言 Xcode 7 对系统中常用的一系列容器类型都增加了泛型支持(),有了泛型后就可以指定容器类中对象的类型了. 假如向泛型容器中加入错误的对象,编译器会报警告. __covariant:协变性,子 ...