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 ...
随机推荐
- lucene和solr
我们为什么要用solr呢? 1.solr已经将整个索引操作功能封装好了的搜索引擎系统(企业级搜索引擎产品) 2.solr可以部署到单独的服务器上(WEB服务),它可以提供服务,我们的业务系统就只要发送 ...
- ubuntu16.04 ARM平台移植xmlrpc-c1.39.12
1. xmlrpc-c依赖与libcurl 参考另外一篇随笔:https://www.cnblogs.com/flyinggod/p/10148228.html 2. 下载源代码 http://xml ...
- jQuery中关于toggle的使用
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>t ...
- Mysql CURD复习(数据库、表、数据)
###############################数据库的CURD:C: create database if not exists tp5_test default charset ut ...
- javax.servlet.jsp.PageContext cannot be resolved to a type
<dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifa ...
- 查询某张表被哪些存储过程或者视图用到的sql语句
/*查询某张表被哪些存储过程或者视图用到的sql语句*/select distinct object_name(id) from syscomments where id in (select id ...
- 个人项目:wc程序(java)
Github项目地址:https://github.com/jat0824/wc.git 项目相关要求 wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写一个命令行 ...
- C#ADO.NET基础一
简介 使用SQLite进行讲解 1.基础类: SQLiteConnection 连接数据库 SQLiteCommand 执行命令(增,删,改,查),或存储过程 SQLiteDataReader 读取查 ...
- angular 事件绑定
<button (click)="onClick($event)">点我</button> import { Component, OnInit } fro ...
- 【ARC069F】Flags 2-sat+线段树优化建图+二分
Description 数轴上有 n 个旗子,第 ii 个可以插在坐标 xi或者 yi,最大化两两旗子之间的最小距离. Input 第一行一个整数 N. 接下来 N 行每行两个整数 xi, ...