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)是否相等的更多相关文章

  1. c# 判断两个集合是否有交集

    /// <summary> /// 判断是否有交集 /// </summary> /// <typeparam name="T"></ty ...

  2. 计算两个集合的差集——第六期 Power8 算法挑战赛

    第六期Power8大赛 1.1 比赛题目 题目: 计算两个集合的差集: 详细说明: 分别有集合A和B两个大数集合,求解集合A与B的差集(A中有,但B中无的元素),并将结果保存在集合C中,要求集合C中的 ...

  3. 【java】【反射】反射实现判断发生了修改操作,判断两个对象是否发生属性值的变更,判断两个List集合内对象的属性值是否发生变更

    java的反射实现: 判断发生了修改操作,判断两个对象是否发生属性值的变更,判断两个List集合内对象的属性值是否发生变更 今日份代码: package com.sxd.streamTest; imp ...

  4. 求两个集合的交集和并集C#

    我是用hashset<T>来实现的 具体如代码所示 using System; using System.Collections.Generic; using System.Linq; u ...

  5. 一个diff工具,用于判断两个目录下所有的改动(比较新旧版本文件夹)

    需求: 编写一个diff工具,用于判断两个目录下所有的改动 详细介绍: 有A和B两个目录,目录所在位置及层级均不确定 需要以B为基准找出两个目录中所有有改动的文件(文件或内容增加.修改.删除),将有改 ...

  6. 51Nod 1557 两个集合(二分)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1557 题意: 小X有n个互不相同的整数: p1,p2,...,pn .他 ...

  7. poj 1127:Jack Straws(判断两线段相交 + 并查集)

    Jack Straws Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2911   Accepted: 1322 Descr ...

  8. c#封装DBHelper类 c# 图片加水印 (摘)C#生成随机数的三种方法 使用LINQ、Lambda 表达式 、委托快速比较两个集合,找出需要新增、修改、删除的对象 c# 制作正方形图片 JavaScript 事件循环及异步原理(完全指北)

    c#封装DBHelper类   public enum EffentNextType { /// <summary> /// 对其他语句无任何影响 /// </summary> ...

  9. 51nod 1557 两个集合 (严谨的逻辑题)

    题目: 1557 两个集合 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 小X有n个互不相同的整数: p1,p2,...,pn  ...

随机推荐

  1. openebula vm无法获取IP问题解决

    http://archives.opennebula.org/documentation:archives:rel2.2:cong Contextualizing Virtual Machines 2 ...

  2. Solidity开发、测试、部署

    这篇文章很详细的列举了几种方式来开始solidity开发: https://medium.com/@davekaj/solidity-tips-and-tricks-for-beginners-bui ...

  3. SQLAlchemy(ORM框架)

    SQLAlchemy SQLAlchemy概述 2 3 4 5 6 7 8 9 10 11 12 13 MySQL-Python     mysql+mysqldb://<user>:&l ...

  4. SQLite在php中的接口

    sqlite是一种比较轻型的嵌入式数据库,它与 SQL 之间的不同,为什么需要它,以及它的应用程序数据库处理方式.SQLite是一个软件库,实现了自给自足的.无服务器的.零配置的.事务性的 SQL 数 ...

  5. 第十八课 Gazebo仿真器

    1.Gazebo概述 在Gazebo中的模拟效果是非常好的. 它的特性 Dynamics Simulation 直接控制物理引擎参数 Building Editor 无需代码即可在Gazebo中创建机 ...

  6. Asp.Net程序目录下文件夹或文件操作导致Session失效的解决方案

    1.配置web.config <system.web> <sessionState mode="StateServer" stateConnectionStrin ...

  7. Java 设计模式 和七大设计原则

    创建型模式 抽象工厂模式(Abstract factory pattern): 提供一个接口, 用于创建相关或依赖对象的家族, 而不需要指定具体类. 生成器模式(Builder pattern): 使 ...

  8. Kernel的意义

    在第7章最后一段讲到Kernel,Kernel就是用向量表示元素的和的乘积. Back in our discussion of linear regression, we had a problem ...

  9. [转]不完美解决V社游戏的中文支持问题

    先安装安装文泉驿正黑:sudo apt-get install fonts-wqy-zenhei 然后sudo gedit /etc/fonts/conf.avail/25-wqy-zenhei.co ...

  10. C#三层架构搭建

    一.简介 主要分为:界面层(User Interface layer),业务逻辑层(Business Logic Layer),数据访问层(Data access layer) 1.作用 界面层(UI ...