两数组并集交集差集
using System; using System.Collections.Generic; using System.Linq; namespace ServiceTest { class Program { static void Main(string[] args) { IList<Student> oneStudents = new List<Student>(); oneStudents.Add(new Student(1, false, "小新1", "one")); oneStudents.Add(new Student(2, false, "小新2", "one")); IList<Student> twoStudents = new List<Student>(); twoStudents.Add(new Student(2, false, "小新2", "two")); twoStudents.Add(new Student(2, false, "小新22", "two")); twoStudents.Add(new Student(3, false, "小新3", "two")); var bingji = oneStudents.Union(twoStudents, new StudentListEquality()).ToList();//并(全)集 var jiaoji = oneStudents.Intersect(twoStudents, new StudentListEquality()).ToList();//交集 var chaji = oneStudents.Except(twoStudents, new StudentListEquality()).ToList();//差集 var weiyi = twoStudents.Distinct(new StudentListEquality()).ToList(); Console.WriteLine(); Console.WriteLine("以下是并集的结果"); bingji.ForEach(x => { Console.WriteLine(x.StudentId.ToString() + " " + x.Sex.ToString() + " " + x.Name.ToString() + " " + x.Address.ToString()); }); Console.WriteLine(); Console.WriteLine("以下是交集的结果"); jiaoji.ForEach(x => { Console.WriteLine(x.StudentId.ToString() + " " + x.Sex.ToString() + " " + x.Name.ToString() + " " + x.Address.ToString()); }); Console.WriteLine(); Console.WriteLine("以下是差集的结果"); chaji.ForEach(x => { Console.WriteLine(x.StudentId.ToString() + " " + x.Sex.ToString() + " " + x.Name.ToString() + " " + x.Address.ToString()); }); Console.WriteLine(); Console.WriteLine("以下是唯一的结果"); weiyi.ForEach(x => { Console.WriteLine(x.StudentId.ToString() + " " + x.Sex.ToString() + " " + x.Name.ToString() + " " + x.Address.ToString()); }); Console.ReadLine(); //new SignVerifyUnifiedOperationService().Debug(); //Console.ReadLine(); } } public class Student { public Student(int studentId, bool sex, String name, String address) { this.StudentId = studentId; this.Sex = sex; this.Name = name; this.Address = address; } public int StudentId { 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.StudentId == y.StudentId; } public int GetHashCode(Student obj) { if (obj == null) { return 0; } return obj.ToString().GetHashCode(); } } }
结果
并集优先选oneStudents
交集优先选oneStudents
唯一集选择twoStudents顺序靠前的小新2,而不是小新22
如果调换顺序结果也会颠倒
IList<Student> oneStudents = new List<Student>(); oneStudents.Add(new Student(1, false, "小新1", "one")); oneStudents.Add(new Student(2, false, "小新2", "one")); IList<Student> twoStudents = new List<Student>(); twoStudents.Add(new Student(2, false, "小新22", "two"));//这里吧小新22放前面了 twoStudents.Add(new Student(2, false, "小新2", "two")); twoStudents.Add(new Student(3, false, "小新3", "two")); //交换顺序 var bingji = twoStudents.Union(oneStudents, new StudentListEquality()).ToList();//并(全)集 var jiaoji = twoStudents.Intersect(oneStudents, new StudentListEquality()).ToList();//交集 var chaji = twoStudents.Except(oneStudents, new StudentListEquality()).ToList();//差集 var weiyi = twoStudents.Distinct(new StudentListEquality()).ToList();
结果如下:
侵删