目录
HashSet Constructors
Propertys
HashSet.Comparer
HashSet.Count
Methods
HashSet.Add
HashSet.Clear
HashSet.Contains
HashSet.CopyTo
HashSet.ExceptWith
HashSet.IntersectWith
HashSet.IsProperSubsetOf
HashSet.IsProperSupersetOf
HashSet.IsSubsetOf
HashSet.IsSupersetOf
HashSet.Overlaps
HashSet.Remove
HashSet.SetEquals
HashSet.SymmetricExceptWith
HashSet.TrimExcess
HashSet.TryGetValue
HashSet.UnionWith
该类在 System.Collections.Generic 命名空间。
点击这里进入官网了解更多成员详情
HashSet<T> 类 表示值的集。
类型参数 T 表示哈希集中的元素类型。
HashSet<T> Constructors
该构造函数还有很多的重载形式 。这里列出常用的三种形式
public HashSet ();
public HashSet (System.Collections.Generic.IEnumerable<T> collection);
public HashSet (int capacity);
static void Main(string[] args)
{
// 调用默认构造函数,该实例为空并使用集类型的默认相等比较器
HashSet<int> evenNumbers = new HashSet<int>();
int[] number = { 55,55, 44, 33 ,33};
HashSet<int> evenNumbers1 = new HashSet<int>(number);
foreach(var tt in evenNumbers1)
{
WriteLine(tt); // 输出 55 44 33
}
WriteLine();
HashSet<int> evenNumber2 = new HashSet<int>(evenNumbers1); // evenNumber2 是 evenNumbers1 的副本
foreach (var tt in evenNumber2)
{
WriteLine(tt); // 输出 55 44 33
}
HashSet<int> evenNumber3 = new HashSet<int>(10); // evenNumber3 的初始容量是10
}
Propertys
HashSet<T>.Comparer
public System.Collections.Generic.IEqualityComparer<T> Comparer { get; }
HashSet<T>.Count
public int Count { get; }
static void Main(string[] args)
{
int[] number = { 55,55, 44, 33 ,33};
HashSet<int> evenNumbers1 = new HashSet<int>(number);
// 输出3, 记住 HashSet 中的元素值不能是重复的,每一个元素值必须是唯一的
WriteLine(evenNumbers1.Count);
}
Methods
HashSet<T>.Add
public bool Add (T item);
- 将名为 item 的元素添加到HashSet<T> 的尾部。
- 如果将元素添加到 HashSet <T> 对象中,则为true; 如果该元素已存在,则返回 false。
- 如果 Count已经等于 HashSet <T> 对象的当前容量,则会自动调整容量以容纳新的 item 元素。
- 如果Count小于内部数组的当前容量,则此方法为 o (1) 操作。 如果HashSet<T>对象必须调整大小,此方法将成为 O (
n
) 操作,其中n
是Count。