C#HashSet教程展示了如何在C#中使用HashSet集合。
哈希集
HashSet
表示一组值。它不包含重复元素。该集合对数学集合抽象进行建模。
HashSet
不提供元素排序。如果我们需要保持顺序,我们可以使用SortedSet
集合。
C#HashSet计数元素
Count
属性返回HashSet
中元素的数量。
var brands = new HashSet<string>(); brands.Add("Wilson"); brands.Add("Nike"); brands.Add("Volvo"); brands.Add("IBM"); brands.Add("IBM"); int nOfElements = brands.Count; Console.WriteLine($"The set contains {nOfElements} elements"); Console.WriteLine(string.Join(", ", brands));
我们有一套品牌。
var brands = new HashSet<string>();
创建了一个新的HashSet
。
brands.Add("Wilson");
使用Add
方法将元素添加到集合中。
int nOfElements = brands.Count;
我们使用Count
属性获取元素的数量。
$ dotnet run The set contains 4 elements Wilson, Nike, Volvo, IBM
C#HashSet删除元素
在下一个示例中,我们从HashSet
中删除元素。
var brands = new HashSet<string> { "Wilson", "Nike", "Volvo", "Kia", "Lenovo" }; Console.WriteLine(string.Join(", ", brands)); brands.Remove("Kia"); brands.Remove("Lenovo"); Console.WriteLine(string.Join(", ", brands)); brands.Clear(); if (brands.Count == 0) { Console.WriteLine("The brands set is empty"); } var words = new HashSet<string> { "sky", "blue", "cup", "cold", "cloud", "pen", "bank" }; words.RemoveWhere(word => word.Length == 3); Console.WriteLine(string.Join(", ", words));
使用Remove
、Clear
和RemoveWhere
删除元素。
brands.Remove("Kia");
使用Remove
删除单个元素。
brands.Clear();
使用Clear
删除所有元素。
words.RemoveWhere(word => word.Length == 3);
使用RemoveWhere
,我们删除满足给定条件的元素。
$ dotnet run Wilson, Nike, Volvo, Kia, Lenovo Wilson, Nike, Volvo The brands set is empty blue, cold, cloud, bank
C#HashSet循环
我们可以使用foreach和while关键字遍历HashSet
。
var words = new HashSet<string> { "sky", "blue", "cup", "cold", "cloud", "pen", "bank" }; foreach (var word in words) { Console.WriteLine(word); } Console.WriteLine("----------------------"); var it = words.GetEnumerator(); while (it.MoveNext()) { Console.WriteLine(it.Current); }
在示例中,我们使用foreach和while遍历单词的HashSet
。
$ dotnet run sky blue cup cold cloud pen bank ---------------------- sky blue cup cold cloud pen bank
C#HashSet包含
Contains
方法确定HashSet
是否包含指定的元素。
var words = new HashSet<string> { "sky", "blue", "cup", "cold", "cloud", "pen", "bank" }; Console.WriteLine(words.Contains("sky")); Console.WriteLine(words.Contains("water")); Console.WriteLine("-----------------"); var users = new HashSet<User> { new User("John Doe", "gardener"), new User("Roger Roe", "driver"), new User("Lucy Smith", "teacher") }; var u1 = new User("John Doe", "gardener"); var u2 = new User("Jane Doe", "student"); Console.WriteLine(users.Contains(u1)); Console.WriteLine(users.Contains(u2)); record User(string name, string occupation);
我们有两个HashSet
集合。
var words = new HashSet<string> { "sky", "blue", "cup", "cold", "cloud", "pen", "bank" }; Console.WriteLine(words.Contains("sky")); Console.WriteLine(words.Contains("water"));
我们检查单词的HashSet
是否包含这些指定的双词。
var u1 = new User("John Doe", "gardener"); var u2 = new User("Jane Doe", "student"); Console.WriteLine(users.Contains(u1)); Console.WriteLine(users.Contains(u2));
C#记录包含Equals
和GetHashCode
方法的默认实现;因此,Contains
方法的工作方式与我们预期的一样。
$ dotnet run True False ----------------- True False
如果是类,我们需要实现Equals
和GetHashCode
方法。
var users = new HashSet<User> { new User("John Doe", "gardener"), new User("Roger Roe", "driver"), new User("Lucy Smith", "teacher") }; var u1 = new User("John Doe", "gardener"); var u2 = new User("Jane Doe", "student"); Console.WriteLine(users.Contains(u1)); Console.WriteLine(users.Contains(u2)); class User { string Name { get; set; } string Occupation { get; set; } public User(string name, string occupation) => (Name, Occupation) = (name, occupation); public override bool Equals(object obj) { return obj is User user && Name == user.Name; } public override int GetHashCode() { return HashCode.Combine(Name); } }
在这个例子中,我们有一个用户对象的HashSet
。由于该对象是使用class
关键字定义的,因此我们添加了Equals
和GetHashCode
方法的实现。
$ dotnet run True False
C#哈希集
HashSet
包含一组特定于集合的方法。
var vals1 = new HashSet<int> { 1, 2, 3, 4, 5 }; var vals2 = new HashSet<int> { 6, 7, 8, 9, 10 }; vals1.UnionWith(vals2); Console.WriteLine(string.Join(", ", vals1)); var vals3 = new HashSet<int> { 1, 2, 3, 4, 5 }; var vals4 = new HashSet<int> { 3, 4, 5, 6, 7 }; vals3.IntersectWith(vals4); Console.WriteLine(string.Join(", ", vals3));
我们使用UnionWith
和IntersectWith
方法。
vals1.UnionWith(vals2);
UnionWith
方法修改HashSet
对象以包含来自两个集合的元素。
vals3.IntersectWith(vals4);
IntersectWith
方法修改HashSet
对象以包含存在于两个对象中的元素。
$ dotnet run 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 3, 4, 5
C#排序集
SortedSet
表示一组未排序的对象。
var vals = new SortedSet<int> { 5, 2, 1, 4, 3, 7, 6 }; Console.WriteLine(string.Join(", ", vals)); var words = new SortedSet<string> { "sky", "blue", "cup", "cold", "cloud", "pen", "bank" }; Console.WriteLine(string.Join(", ", words)); var users = new SortedSet<User> { new User("Robin", "bookseller"), new User("John", "gardener"), new User("John", "writer"), new User("Janet", "teacher"), new User("Andrew", "driver"), new User("Lucy", "accountant") }; Console.WriteLine(string.Join("\n", users)); record User(string Name, string Occupation) : IComparable<User> { public int CompareTo(User other) { return this.Occupation.CompareTo(other.Occupation); } }
我们有整数、字符串和用户的SortedSet
。
$ dotnet run 1, 2, 3, 4, 5, 6, 7 bank, blue, cloud, cold, cup, pen, sky User { Name = Lucy, Occupation = accountant } User { Name = Robin, Occupation = bookseller } User { Name = Andrew, Occupation = driver } User { Name = John, Occupation = gardener } User { Name = Janet, Occupation = teacher } User { Name = John, Occupation = writer }
在本文中,我们使用了C#HashSet。
列出所有C#教程。