using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Sample2
{
class Program
{
static void Main(string[] args)
{
//List之Union(),Intersect(),Except() 即并集,交集,差集运算
IList<Student> Students1 = new List<Student>();
Students1.Add(new Student(1, false, "张三", "深圳"));
Students1.Add(new Student(2, false, "李四", "广州"));
Students1.Add(new Student(3, false, "王五", "珠海"));
Students1.Add(new Student(4, false, "赵六", "东莞")); IList<Student> Students2 = new List<Student>();
Students2.Add(new Student(1, false, "张三", "深圳"));
Students2.Add(new Student(5, false, "李七", "广州"));
Students2.Add(new Student(6, false, "孙八", "深圳"));
Students2.Add(new Student(7, true, "赵燕", "深圳")); //自定义比较规则
var bingji = Students1.Union(Students2, new StudentListEquality()).ToList();//并集(AB集合所有项)
var jiaoji = Students1.Intersect(Students2, new StudentListEquality()).ToList();//交集 (AB集合共同项)
var chaji = Students1.Except(Students2, new StudentListEquality()).ToList();//差集(A集合有,B没有) Console.WriteLine("以下是并集的结果");
bingji.ForEach(x =>
{
Console.WriteLine(x.Id.ToString() + " " + x.Sex.ToString() + " " + x.Name.ToString() + " " + x.Address.ToString()); }); Console.WriteLine("以下是交集的结果");
jiaoji.ForEach(x =>
{
Console.WriteLine(x.Id.ToString() + " " + x.Sex.ToString() + " " + x.Name.ToString() + " " + x.Address.ToString()); }); Console.WriteLine("以下是差集的结果");
chaji.ForEach(x =>
{
Console.WriteLine(x.Id.ToString() + " " + x.Sex.ToString() + " " + x.Name.ToString() + " " + x.Address.ToString()); });
Console.ReadKey();
}
} public class Student
{
public Student(int id, bool sex, String name, String address)
{
this.Id = id;
this.Sex = sex;
this.Name = name;
this.Address = address;
}
public int Id { get; set; }
public bool Sex { get; set; }
public String Name { get; set; }
public String Address { get; set; } } public class StudentListEquality : IEqualityComparer<Student>
{
public bool Equals(Student x, Student y)
{
return x.Id == y.Id && x.Name == y.Name && x.Address == y.Address && x.Sex == y.Sex;
}
public int GetHashCode(Student obj)
{
return (obj == null) ? 0 : obj.ToString().GetHashCode();
}
}
}

List之Union(),Intersect(),Except() 即并集,交集,差集运算。的更多相关文章

  1. spark 集合交集差集运算

    intersect except是spark提供的集合差集运算, 但是要求参与运算的两个dataframe,有相同的data Schema. 如果我想从 集合1(attribute1, attribu ...

  2. java数组并集/交集/差集(补集)

    1.说明 使用java容器类的性质选择容器 2.实现 package com.wish.datastrustudy; import java.util.HashSet; import java.uti ...

  3. 【Set】Set集合求并集,交集,差集

    /** * @author: Sam.yang * @date: 2020/11/16 11:14 * @desc: Set集合操作工具类 */ public class SetOptUtils { ...

  4. python求两个列表的并集.交集.差集

    求两个列表的差集 >>> a = [1,2,3] >>> b=[1,2] >>> ################################ ...

  5. [Linux] 取两个文件的并集/交集/差集

    uniq -d是只打印重复行 -u是只打印独一无二的行文件A : abcd文件B: cdef取并集:A + B sort A B|uniq 取交集: sort A B|uniq -d 取差集:A - ...

  6. 60-python基础-python3-集合-集合常用方法-交集、并集、差集、对称差集-intersection(&)-union(|)-difference(-)-symmetric_difference()

    交集.并集.差集-intersection(&)-union(|)-difference(-) 1-intersection(&) s1.intersection(s2),返回s1和s ...

  7. scala中集合的交集、并集、差集

    scala中有一些api设计的很人性化,集合的这几个操作是个代表: 交集: scala> Set(1,2,3) & Set(2,4) // &方法等同于interset方法 sc ...

  8. List之Union(),Intersect(),Except()

    http://www.cnblogs.com/qinpengming/archive/2012/12/03/2800202.html List之Union(),Intersect(),Except() ...

  9. C# List 集合 交集、并集、差集、去重, 对象集合、 对象、引用类型、交并差补、List<T>

    关键词:C#  List 集合 交集.并集.差集.去重, 对象集合. 对象.引用类型.交并差.List<T> 有时候看官网文档是最高效的学习方式! 一.简单集合 Intersect 交集, ...

随机推荐

  1. 裸机LCD驱动配置

    横屏4.3寸LCD为480*272(行:480个像素点        列:272个行) 1.1  LCD原理图 : Pin1:Von  电源正(这里由硬件自动控制) Pin2:VM/VDEN 数据使能 ...

  2. POJ 3463 最(次)短路条数

    Sightseeing Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9497   Accepted: 3340 Descr ...

  3. nrm的安装 、定义和用法

    因为npm包管理工具是属于国外的,所以在中国使用它下载东西的时候比较慢.这时我们就想用国内的淘宝镜像.也有别的,所以当你想切换下载源的时候就会用到nrm了. ###首先,nrm是什么呢? 开发的npm ...

  4. Java的构造器

    初始化和清理是涉及安全的两个问题.C++和Java都引入了构造器(constructor)的概念,这是一个在创建对象时被自动调用的特殊方法. 可以假想为编写的每个类都定义一个initialize()方 ...

  5. NHibernate教程(18)--对象状态

    本节内容 引入 对象状态 对象状态转换 结语 引入 在程序运行过程中使用对象的方式对数据库进行操作,这必然会产生一系列的持久化类的实例对象.这些对象可能是刚刚创建并准备存储的,也可能是从数据库中查询的 ...

  6. 学号:201521123116 《java程序设计》第四周学习总结

    1. 本周学习总结 2. 书面作业 Q1. 注释的应用使用类的注释与方法的注释为前面编写的类与方法进行注释,并在Eclipse中查看.(截图)答:注释的插入:注释以/开始,以/结束类注释/**.... ...

  7. 201521123008《Java程序设计》第二周实验总结

    本周学习总结 ① 数据类型,其中char是占用两个字节的内存空间,其他和以前学过的一样.除了十进制位,整型也可以用八进制或者十六进制表示.浮点型不精确. ②运算符,算术,赋值,逻辑,位运算. ③str ...

  8. 201521123055 《Java程序设计》第12周学习总结

    1. 本章学习总结 2. 书面作业 将Student对象(属性:int id, String name,int age,double grade)写入文件student.data.从文件读出显示. Q ...

  9. 201521123039《java程序设计》第十四周学习总结

    1. 本周学习总结 2. 书面作业 1. MySQL数据库基本操作 建立数据库,将自己的姓名.学号作为一条记录插入.(截图,需出现自己的学号.姓名) 在自己建立的数据库上执行常见SQL语句(截图) 2 ...

  10. 201521123032 《Java程序设计》第9周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集异常 1.常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自己 ...