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. 第1阶段——u-boot分析之make 100ask24x0_config指令(1)

    本文学习目标:         掌握"make 100ask24x0_config"指令在Makefile和mkconfig文件中是怎么实现配置芯片选型 1.执行make 100a ...

  2. VHDL学习:利用Quartus自带库3步快速完成状态机

    Quartus自带库里面有各种编程语言的模板,供开发者参考. 初学者利用VHDL实现状态机比较生疏的情况下,可以调出该模板,适当修改即可. 本文将描述如何利用Quartus自带库调出状态机模板,并适当 ...

  3. OSI与TCP/IP网络模型分层

      学习linux的人,都会接触到一些网络方面的知识.作为一个linux方面的萌新,今天,小编就接触了OSI模型和TCP/IP协议栈,那么什么是OSI模型呢?     OSI模型,开放式系统互联通信参 ...

  4. 【beta】阶段 第六次 Scrum Meeting

    每日任务 1.本次会议为第六次 Meeting会议: 2.本次会议在周六上午大课间,在陆大楼召开,召开本次会议为15分钟. 一.今日站立式会议照片 二.每个人的工作 (有work item 的ID) ...

  5. Swing-BoxLayout用法-入门

    注:本文内容源于http://www.java3z.com/cwbwebhome/article/article20/200016.html?id=4797:细节内容根据笔者理解有修改. BoxLay ...

  6. 201521123079《java程序设计》第7周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 1.ArrayList代码分析 1.1 解释ArrayList的contains源代码 public boo ...

  7. 201521123044 《Java程序设计》第4周学习总结

    1. 本章学习总结 2. 书面作业 1. 注释的应用 使用类的注释与方法的注释为前面编写的类与方法进行注释,并在Eclipse中查看.(截图) 答: 2. 面向对象设计(大作业1,非常重要) 2.1 ...

  8. 201521123018 《Java程序设计》第4周学习总结

    1. 本章学习总结 2. 书面作业 Q1.注释的应用:使用类的注释与方法的注释为前面编写的类与方法进行注释,并在Eclipse中查看.(截图) Q2.面向对象设计(大作业1-非常重要) 2.1 讲故事 ...

  9. 201521123086《JAVA程序设计》第二周学习总结

    一.本章学习总结 学会在Java程序中使用函数,使程序层次更清晰 使用StringBuilder编写代码,减少内存空间的占用 使用BigDecimal精确计算浮点数 使用枚举类型编写函数,掌握返回值使 ...

  10. 201521123098 《Java程序设计》第1周学习总结

    1. 本章学习总结 在本章的学习中,我和当初学习C语言一样由"Hello world"入手,初步了解了: 1. *NotePad++*的文件创建和编写*Hello world.ja ...