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 ...
随机推荐
- jsb闭包
1.什么是闭包? anw:能够读取其他函数内部变量的函数 本质:将函数内部与函数外部连接起来 2.由于在js中,只有函数内部的子函数才能读取局部变量,因此可以把闭包简单理解成’定义一个在函数内部的函数 ...
- [b0044] numpy_快速上手
1 概念理清 2 创建数组 2.1 f1= np.array( [ [1,2,3,4], [2,3,4,5], [3,4,5,6] ]) 其他代码 a= np.array([ [ [3.4,5,6,8 ...
- Linux下新增和使用系统调用
关键词:__SYSCALL().SYSCALL_DEFINEx().syscall()等等. 1. 为什么使用syscall 内核和用户空间数据交换有很多种方式:sysfs.proc.信号等等. 但是 ...
- CodeForces - 1243D (思维+并查集)
题意 https://vjudge.net/problem/CodeForces-1243D 有一张完全图,n个节点 有m条边的边权为1,其余的都为0 这m条边会给你 问你这张图的最小生成树的权值 思 ...
- APK更新集成实践
任务目标:将内网APK打包后最新下载链接.更新时间.更改日志显示在一个我自己制作的APP里 任务作用:我们在内网测试时更新下载APK更加便捷,并且能够清楚目标APK的版本情况,回归.验证做到有的放矢 ...
- Codeforces Round #584
传送门 A. Paint the Numbers 签到. Code #include <bits/stdc++.h> using namespace std; typedef long l ...
- JS运动---运动基础(匀速运动)
[一]运动基础 (2)基础运动案例 <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...
- LeetCode 5273. 搜索推荐系统 Search Suggestions System
地址 https://leetcode-cn.com/problems/search-suggestions-system/ 题目描述给你一个产品数组 products 和一个字符串 searchWo ...
- Apriori关联分析详解
------------恢复内容开始------------ 一. Apriori关联分析概述 选择物品之间的关联规则也就是要找出物品之间的关系,要找到这种关系有两步 找出频繁一起出现的物品集的集合, ...
- Batch Normalization、Layer Normalization、Instance Normalization、Group Normalization、Switchable Normalization比较
深度神经网络难训练一个重要的原因就是深度神经网络涉及很多层的叠加,每一层的参数变化都会导致下一层输入数据分布的变化,随着层数的增加,高层输入数据分布变化会非常剧烈,这就使得高层需要不断适应低层的参数更 ...