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

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

 

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 }
*/

 

posted @ 2010-10-26 14:04  把爱延续  阅读(939)  评论(0编辑  收藏  举报