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. React Native底|顶部导航使用小技巧

    导航一直是App开发中比较重要的一个组件,ReactNative提供了两种导航组件供我们使用,分别是:NavigatorIOS和Navigator,但是前者只能用于iOS平台,后者在ReactNati ...

  2. Prism for Xamarin.Forms

    一.使用环境 OS:Win 10 16273 VS:VS2017- 15.3.4 Xamarin:4.6.3.4,nuget:2.4 Android Emulator:Visual Studio fo ...

  3. 【集美大学1411_助教博客】团队作业6——展示博客(Alpha版本)

    写在前面的话 工作还真是应该抓紧做呢,以下评分是助教在出差前评的,但出差回来就忘记了大部分内容.同学们都在预期时间内完成了自己的alpha项目.由于助教的频繁出差,评分工作落下一大截,在此表示欠意,会 ...

  4. 201521123050 《Java程序设计》第6周学习总结

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

  5. 201521123020 《Java程序设计》第4周学习总结

    本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. (1)类注释的使用方法是: /** *(要注释的内容) */ (2)学习了Object类,学会了覆 ...

  6. 12个Sublime Text应用技巧和诀窍

    本文为您提供Sublime Text编辑器的12个技巧和诀窍,深入挖掘这个看似简洁的代码编辑器,背后所隐藏的实现各种高级功能的无限可能. 1) 选择 以下是一些Sublime Text选择文本的快捷键 ...

  7. 201521123077 《Java程序设计》第14周学习总结

    1. 本周学习总结 1.1以你喜欢的方式(思维导图或其他)归纳总结多数据库相关内容. 2. 书面作业 1. MySQL数据库基本操作 -参考:实验任务书-题目1 建立数据库,将自己的姓名.学号作为一条 ...

  8. HTTP请求的header头解析

    Request Headers: 下图是我访问一个URL:http://www.hzau.edu.cn的一个header,根据具体实例来分析一下各部分的功能及其作用. Accept 作用: 浏览器端可 ...

  9. Log Reservation

    本文是在阅读<SQL Server Transaction Log Management>的Chapter 2: Log Internals时发现以往对Log Grows的理解比较片面,大 ...

  10. virtualbox修改主机名

    virtualbox修改主机名 /etc/hostname /etc/hosts