zoukankan      html  css  js  c++  java
  • 《学习笔记》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 }
    */


    返回导读目录,阅读更多随笔



    分割线,以下为博客签名:

    软件臭虫情未了
    • 编码一分钟
    • 测试十年功


    随笔如有错误或不恰当之处、为希望不误导他人,望大侠们给予批评指正。

  • 相关阅读:
    jquery的图片异步加载
    thinkphp3.1的验证码
    android的edittext设置输入限制,只能输入数字
    android,安卓get请求的提交以及我遇到的异常
    android安卓开发基础小笔记,添加按钮事件,打开新窗体,窗体传值,回传
    php正则表达式函数
    php对浮点数小数取整,php除法取整数
    php数组全排列,元素所有组合
    javascript数组全排列,数组元素所有组合
    spring 配置中相关属性的含义:
  • 原文地址:https://www.cnblogs.com/08shiyan/p/1861469.html
Copyright © 2011-2022 走看看