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 ...
随机推荐
- Java反射03 : 获取Class的注解、修饰符、父类、接口、字段、构造器和方法
java.lang.Class类提供了获取类的各种信息对象的静态方法. 本文转载自:https://blog.csdn.net/hanchao5272/article/details/79363921 ...
- RAC_多路径配置
多路径配置 http://blog.itpub.net/31397003/viewspace-2143390/ 挂盘/配置好yum源 2.程序包的安装 device-mapper-1.02.95-2. ...
- Scrapy 下载图片时 ModuleNotFoundError: No module named'PIL'
使用scrapy的下载模块需要PIL(python图像处理模块)的支持,使用pip安装即可
- Centos7下的zabbix安装与部署
目录: 1.Zabbix介绍 2.LAMP/LNMP介绍 3.Zabbix安装与部署 1.Zabbix介绍 zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. ...
- Python入门基础学习(文件与异常处理)
Python基础学习笔记(七) 捕获异常的语法格式: 文件的基本操作: 打开文件 读.写文件 关闭文件 read方法 --读取文件: open函数的第一个参数是要打开的文件名(文件名区分大小写) 如果 ...
- 使用java代码操作Redis
1导入pom.xml依赖 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis ...
- licode(1) Basic Example 客户端解析
整体 在浏览其中输入https://dst_host_domain:13004后, 请求了index.html,该文件在licode\extras\basic_example\public\index ...
- Mybatis----传入参数parameterType类型详解
Mybatis----传入参数parameterType类型详解 前言 Mybatis的Mapper文件中的select.insert.update.delete元素中有一个parameterType ...
- 【python爬虫】正则表达式
一.数据的分类 1.结构化数据 特点:数据以行为单位,每一个数据表示一个实体.每一行数据的属性都是一样的. 举例:关系型数据库中的表就是结构化数据. 处理方法:sql 2.半结构化数据 特点:结构化数 ...
- python中append的使用
没有系统地学习过python,最近在append的使用上遇到了大问题,吃到了苦头 之前一直单纯地认为通过append把数添加到list中,不需要提前开空间,非常便利,但却没有意识到这个过程并不是值传递 ...