List Except 操作,IEqualityComparer 使用
1.此介面用於對集合 的自定義相等比較演算法的實現。包含2個方法:
Equals(T,T)
: 確定指定的物件是否相等。 T 為要比較的物件型別;GetHashCode(T)
:返回指定物件的雜湊程式碼。如果兩個物件的Equal
比較結果相等,則他們hashCode返回的物件也必須返回同一個值;
2.官方建議繼承 EqualityComparer<T>
而不是實現 IEqualityComparer<T>
3.
Union()
這個方法將會Union(並集)兩個序列(集合)連線成一個新列表(集合)
public static IEnumerable<TSource> Union<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second)
public static IEnumerable<TSource> Union<TSource>(this IEnumerable<TSource> first,IEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
Intersect ()
它將產生兩個序列的交集.
方法定義是:
public static IEnumerable<TSource> Intersect<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second)
public static IEnumerable<TSource> Intersect<TSource>(this IEnumerable<TSource> first, Enumerable<TSource> second,IEqualityComparer<TSource> comparer)
Except ()
它是從一個集合中刪除存在另一個集合中的項.兩個序列產生的集合差. 英文意思是:除此之外
方法定義是:
public static IEnumerable<TSource> Except<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second)
public static IEnumerable<TSource> Except<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
例項程式碼分別如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Test
{
class Program
{
static void Main(string[] args)
{
IList<Student> oneStudents = new List<Student>();
oneStudents.Add(new Student(1, false, "小新1", "徐匯"));
oneStudents.Add(new Student(2, false, "小新2", "閔行"));
oneStudents.Add(new Student(3, false, "小新3", "嘉定"));
oneStudents.Add(new Student(4, false, "小新4", "閘北"));
IList<Student> twoStudents = new List<Student>();
twoStudents.Add(new Student(5, false, "小新5", "貴州"));
twoStudents.Add(new Student(6, false, "小新6", "湖北"));
twoStudents.Add(new Student(7, false, "小新7", "山東"));
twoStudents.Add(new Student(8, false, "小新8", "西藏"));
IList<Student> threeStudents = new List<Student>();
threeStudents.Add(new Student(1, false, "小新1", "徐匯"));
threeStudents.Add(new Student(2, false, "小新2", "閔行"));
var bingji = oneStudents.Union(twoStudents, new StudentListEquality()).ToList();//並(全)集
var jiaoji = oneStudents.Intersect(threeStudents, new StudentListEquality()).ToList();//交集
var chaji = oneStudents.Except(threeStudents, 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.ReadKey();
}
}
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;
}
else
{
return obj.ToString().GetHashCode();
}
}
}
}
https://msdn.microsoft.com/zh-cn/library/bb336390(v=vs.110).aspx
執行結果如圖:
相關文章
- C++中list的使用方法及常用list操作總結C++
- python列表(list)的使用技巧及高階操作Python
- python中try..except語句如何使用?Python
- apart from,besides,except和except for有什麼區別?IDE
- RMAN命令LIST操作總結
- 如何高效能操作list
- List常用操作工具類
- Redis之list型別及操作Redis型別
- Java Map和List常見操作Java
- Python異常處理 try、except和else的使用Python
- ORACLE SQL的EXCEPT、INTERSECT用法OracleSQL
- vant list元件使用元件
- Scala 片段2:List的操作符魔法
- LeetCode:Product of Array Except SelfLeetCode
- redis list 使用和理解Redis
- 常用php操作redis命令整理(三)LIST型別PHPRedis型別
- python-try-except:pass的用法Python
- [Typescript] Accept Anything Except Null or UndefinedTypeScriptNullUndefined
- Python資料型別-str,list常見操作Python資料型別
- 介紹幾種List集合分批操作的工具
- Python 列表 list 陣列 array 常用操作集錦Python陣列
- c/c++ 標準容器 forward_list resize 操作C++Forward
- Java8 新特性 Stream流操作List集合 (二)Java
- Python 通過List 實現佇列的操作Python佇列
- C++ forward_list 中插入和刪除操作C++Forward
- PTSQLServer中exists和except用法介紹wkaSQLServer
- Python基礎:使用list & tuplePython
- 使用flutter打造炫酷的listFlutter
- Java函式泛型List引數,操作泛型元素Java函式泛型
- Python 列表 list 陣列 array 常用操作集錦薦Python陣列
- python 編輯器提示 do not use bare exceptPython
- Leetcode 238. Product of Array Except SelfLeetCode
- [LeetCode] 238. Product of Array Except SelfLeetCode
- Java學習--list,set,Map介面使用Java
- 進行List集合去重操作,分為保持原List集合元素順序和不保持原順序
- SQL Server中的集合運算: UNION, EXCEPT和INTERSECTSQLServer
- c# 敏捷2 ForEach ToDictionary ToLookup Except比較C#敏捷
- 探討 T-SQL 的 EXISTS、EXCEPT、INTERSECT 算符SQL