C# 集合的交集 差集 并集 去重
C# 集合的交集 差集 并集 去重
两个对象list,直接比较是不行的,因为他们存的地址不一样
需要重写GetHashCode()与Equals(object obj)方法告诉电脑
class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
} class CompareStudent : IEqualityComparer<Student>
{
public bool Equals(Student x, Student y)
{
return x.Id == y.Id;
} public int GetHashCode(Student p)
{
if (p == null)
return ;
return p.Id.GetHashCode();
}
} class Program
{
static void Main(string[] args)
{
List<Student> stuA = new List<Student>();
List<Student> stuB = new List<Student>();
stuA.Add(new Student { Id = , Name = "", Age = });
stuA.Add(new Student { Id = , Name = "", Age = });
stuB.Add(new Student { Id = , Name = "", Age = });
stuB.Add(new Student { Id = , Name = "", Age = });
stuB.Add(new Student { Id = , Name = "", Age = });
stuB.Add(new Student { Id = , Name = "", Age = });
var result = stuA.Where(a => !stuB.Exists(b => b.Id == a.Id)); //在A中存在不再B中存在 即求差集
var resc = stuA.Except(stuB, new CompareStudent()); //差集
var resj = stuA.Intersect(stuB, new CompareStudent());// 交集
var resb = stuA.Union(stuB, new CompareStudent()); //并集
var resD = stuB.Distinct(new CompareStudent()); //去重
}
}
C# 集合的交集 差集 并集 去重的更多相关文章
- C# 数组比较--取得两个集合的交集,差集,并集的方法
方法关键字: 交集:Intersect 差集:Except 并集:Union 使用代码: , , , , }; , , , , }; var 交集 = arr1.Intersect(arr2).ToL ...
- python-->(set /dict)交集 差集 并集 补集(功能用来做交差并补的)
# ### 集合 作用:交集 差集 并集 补集(功能用来做交差并补的) '''特征:自动去重 无序''' #定义一个空集合 setvar = set() #set()强制转换成一个空集合的数据类型 p ...
- scala中集合的交集、并集、差集
scala中有一些api设计的很人性化,集合的这几个操作是个代表: 交集: scala> Set(1,2,3) & Set(2,4) // &方法等同于interset方法 sc ...
- js取两个数组的交集|差集|并集|补集|去重示例代码
http://www.jb51.net/article/40385.htm 代码如下: /** * each是一个集合迭代函数,它接受一个函数作为参数和一组可选的参数 * 这个迭代函数依次将集合的每一 ...
- 60-python基础-python3-集合-集合常用方法-交集、并集、差集、对称差集-intersection(&)-union(|)-difference(-)-symmetric_difference()
交集.并集.差集-intersection(&)-union(|)-difference(-) 1-intersection(&) s1.intersection(s2),返回s1和s ...
- java集合(交集,并集,差集)
说明:这里没有求差集的代码,有了交集和并集,差集=并集-交集 package com; import java.util.ArrayList; import java.util.HashS ...
- java求两个集合的交集和并集,比较器
求连个集合的交集: import java.util.ArrayList; import java.util.List; public class TestCollection { public st ...
- C++求集合的交集差集
标准库的<algorithm>头文件中提供了std::set_difference,std::set_intersection和std::set_union用来求两个集合的差集,交集和并集 ...
- 求两个集合的交集和并集C#
我是用hashset<T>来实现的 具体如代码所示 using System; using System.Collections.Generic; using System.Linq; u ...
随机推荐
- Thymeleaf常用语法:模板注释
Thymeleaf模板注释分为标准HTML/XML注释.解析层注释.原型注释三种. 一.注释说明 1.标准HTML/XML注释 直接通过浏览器打开,不显示,Thymeleaf模板引擎解析也不处理,但查 ...
- R Data Frame
https://www.datamentor.io/r-programming/data-frame/ Check if a variable is a data frame or not We ca ...
- ABP入门教程4 - 初始化运行
点这里进入ABP入门教程目录 编译解决方案 重新生成解决方案,确保生成成功. 连接数据库 打开JD.CRS.Web.Host / appsettings.json,修改数据库连接设置Connectio ...
- linux学习(五)用户与组管理命令,以及用户信息文件解释
目录 (1)/etc/passwd文件 (2)/etc/shadow passwd命令 userdel命令 usermod命令 groupadd @(用户与组管理命令) linux是一个多用户多任务的 ...
- swoole1--搭建echo服务器
1.安装swoole :pecl install swoole,然后修改php.ini 开启swoole扩展:extension=swoole.so 2.写一个服务器Server.php 1 < ...
- 面向对象~~类的成员: 私有成员,公有成员, 实例方法, 类方法, 静态方法, 属性(property), isinstance ,issubclass, 元类(type)
一 私有成员公有成员 公有成员: 在任何地方都能访问 私有成员: 只有在类的内部才能访问 类从加载时,只要遇到类中的私有成员,都会在私有成员前面加上_类名 二 实例方法 实例方法就是类的实例能够使用的 ...
- <Array> 54 (高频+hard )45
55. Jump Game 希望知道能否到达末尾,也就是说我们只对最远能到达的位置感兴趣,所以维护一个变量 reach,表示最远能到达的位置,初始化为0.遍历数组中每一个数字,如果当前坐标大于 rea ...
- 给用户提供就医帮助的安卓APP
经过我们的小组的成员讨论,我们确定了我们小组的项目,即是一款给用户提供就医帮助的安卓APP. 项目计划及功能:计划两个月内团队成员共同开发完成此款APP,此款APP提供预约挂号,名医名院咨询, 就医导 ...
- 第04组 Alpha冲刺(3/4)
队名:斗地组 组长博客:地址 作业博客:Alpha冲刺(3/4) 各组员情况 林涛(组长) 过去两天完成了哪些任务: 1.收集各个组员的进度 2.写博客 展示GitHub当日代码/文档签入记录: 接下 ...
- __format__
目录 一.__format__ 一.__format__ 自定制格式化字符串 date_dic = { 'ymd': '{0.year}:{0.month}:{0.day}', 'dmy': '{0. ...