《学习笔记》System.Collections.Generic 命名空间 HashSet<T>

本文详细介绍了.NET中HashSet<T>类的各种操作方法,包括添加、删除元素,集合间的并集、交集、差集运算等,并通过具体示例展示了如何使用这些方法来高效地处理数据集。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

挺方便的一个类,提供了对数据集的批量操作,尤其是合并与删除 对于解决一些特殊问题还是挺有帮助的。 

 

Methods

 

 

Add

CopyTo

 

Clear

 

 

ExceptWith

Removes all elements in the specified collection from the current HashSet < T > object

示例见下:

IntersectWith

Modifies the current HashSet < T > object to contain only elements that are present in that object and in the specified collection.

ExceptWith 用法相似。

IsProperSubsetOf

Determines whether a HashSet < T > object is a proper subset of the specified collection.

 

IsProperSupersetOf

Determines whether a HashSet < T > object is a proper superset of the specified collection

 

Remove

Removes the specified element from a HashSet < T > object.

 

RemoveWhere

Removes all elements that match the conditions defined by the specified predicate from a HashSet < T > collection.

 

UnionWith

Modifies the current HashSet < T > object to contain all elements that are present in both itself and in the specified collection.

示例见下:

TrimExcess

Sets the capacity of a HashSet < T > object to the actual number of elements it contains, rounded up to a nearby, implementation-specific value.

 

 

 

 

 

 

 

ExceptWith
// HashSet Example


static void Main()
{
HashSet
<int> lowNumbers = new HashSet<int>();
HashSet
<int> highNumbers = new HashSet<int>();

for (int i = 0; i < 6; i++)
{
lowNumbers.Add(i);
}

for (int i = 3; i < 10; i++)
{
highNumbers.Add(i);
}

Console.Write(
"lowNumbers contains {0} elements: ", lowNumbers.Count);
DisplaySet(lowNumbers);

Console.Write(
"highNumbers contains {0} elements: ", highNumbers.Count);
DisplaySet(highNumbers);

Console.WriteLine(
"highNumbers ExceptWith lowNumbers...");
highNumbers.ExceptWith(lowNumbers);

Console.Write(
"highNumbers contains {0} elements: ", highNumbers.Count);
DisplaySet(highNumbers);



}
/* This example provides output similar to the following:
* lowNumbers contains 6 elements: { 0 1 2 3 4 5 }
* highNumbers contains 7 elements: { 3 4 5 6 7 8 9 }
* highNumbers ExceptWith lowNumbers...
* highNumbers contains 4 elements: { 6 7 8 9 }
*/

 

 

UnionWith
using System;
using System.Collections.Generic;

class Program
{
static void Main()
{
HashSet
<int> evenNumbers = new HashSet<int>();
HashSet
<int> oddNumbers = new HashSet<int>();

for (int i = 0; i < 5; i++)
{
// Populate numbers with just even numbers.
evenNumbers.Add(i * 2);

// Populate oddNumbers with just odd numbers.
oddNumbers.Add((i * 2) + 1);
}

Console.Write(
"evenNumbers contains {0} elements: ", evenNumbers.Count);
DisplaySet(evenNumbers);

Console.Write(
"oddNumbers contains {0} elements: ", oddNumbers.Count);
DisplaySet(oddNumbers);

// Create a new HashSet populated with even numbers.
HashSet<int> numbers = new HashSet<int>(evenNumbers);
Console.WriteLine(
"numbers UnionWith oddNumbers...");
numbers.UnionWith(oddNumbers);

Console.Write(
"numbers contains {0} elements: ", numbers.Count);
DisplaySet(numbers);

}

private static void DisplaySet(HashSet<int> set)
{
Console.Write(
"{");
foreach (int i in set)
{
Console.Write(
" {0}", i);
}
Console.WriteLine(
" }");
}
}
/* This example produces output similar to the following:
* evenNumbers contains 5 elements: { 0 2 4 6 8 }
* oddNumbers contains 5 elements: { 1 3 5 7 9 }
* numbers UnionWith oddNumbers...
* numbers contains 10 elements: { 0 2 4 6 8 1 3 5 7 9 }
*/

 

转载于:https://www.cnblogs.com/08shiyan/archive/2010/10/26/1861469.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值