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. Spring Ioc DI 原理

    IOC(DI):其实这个Spring架构核心的概念没有这么复杂,更不像有些书上描述的那样晦涩.Java程序员都知道:java程序中的每个业务逻辑至少需要两个或以上的对象来协作完成,通常,每个对象在使用 ...

  2. 【2017集美大学1412软工实践_助教博客】团队作业10——项目复审与事后分析(Beta版本)

    写在前面的话 转眼轰轰烈烈本学期的软工实践就结束了,这个过程中想必在熬夜敲代码,激烈讨论中留下诸多回忆的同时,也收获了不少.恭喜所有团队完成了本阶段冲刺,此外,由于大家的贡献分给的都很平均,将个人贡献 ...

  3. GUI(JTabel表格)

    /** * */ package com.niit.javagui; import java.util.Vector; import javax.swing.ImageIcon; import jav ...

  4. 扫雷游戏制作过程(C#描述):第五节、菜单操作(续)

    前言 这里给出教程原文地址. 该项目已经放在github上托管. 发布版已经分享到百度网盘 菜单操作(续) 接着节前一章节的内容,我们继续完善菜单栏的功能. 我们首先,先完善Rank的选项,我们希望我 ...

  5. 201521123009 《Java程序设计》第6周学习总结

    1. 本周学习总结 2. 书面作业 Q1:clone方法 1.1 Object对象中的clone方法是被protected修饰,在自定义的类中覆盖clone方法时需要注意什么? 用protected修 ...

  6. 控制结构(1) 分枝/叶子(branch/leaf)

    // 下一篇:卫语句(guard clause) 典型代码: function doSomething1(){ // ... } function doSomething2(){ // ... } f ...

  7. 参加IMWebConf 2017 前端开发者大会是什么体验?

    周六作为特邀讲师之一参加了IMWebConf 2017 前端开发者大会的主题演讲,主题为<WebAssembly:面向未来的web开发技术>.本次大会质量非常高,来自国内外的技术专家带了很 ...

  8. 分页复用代码【Page类、JSP显示页面】

    前言 为了复用,记载一些以前写过的工具类.方法 page类 import java.util.List; /** * Created by ozc on 2017/3/1. */ public cla ...

  9. ops-web运维平台-create.jsp-mootools下拉框-复选框

    create.jsp页面的,body部分 <body onload="Page.init('${pageError}','${pageMessage}',${isSubmit},tru ...

  10. 基于FPGA的腐蚀膨胀算法实现

    本篇文章我要写的是基于的腐蚀膨胀算法实现,腐蚀膨胀是形态学图像处理的基础,,腐蚀在二值图像的基础上做"收缩"或"细化"操作,膨胀在二值图像的基础上做" ...