代码之家  ›  专栏  ›  技术社区  ›  mark

在PowerShell中,如何使用接受集合的构造函数创建哈希集?

  •  0
  • mark  · 技术社区  · 6 年前

    我想创建一个 HashSet 使用 the constructor accepting a collection

    但我的尝试都没有成功:

    C:\> $c = @(1,2,3,4,5)
    C:\> New-Object System.Collections.Generic.HashSet[int]
    C:\> New-Object System.Collections.Generic.HashSet[int] -ArgumentList @(,$c)
    New-Object : Cannot find an overload for "HashSet`1" and the argument count: "1".
    At line:1 char:1
    + New-Object System.Collections.Generic.HashSet[int] -ArgumentList @(,$ ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodException
        + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
    
    C:\> New-Object System.Collections.Generic.HashSet[int] -ArgumentList $c
    New-Object : Cannot find an overload for "HashSet`1" and the argument count: "5".
    At line:1 char:1
    + New-Object System.Collections.Generic.HashSet[int] -ArgumentList $c
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodException
        + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
    
    C:\> New-Object System.Collections.Generic.HashSet[int] -ArgumentList @($c)
    New-Object : Cannot find an overload for "HashSet`1" and the argument count: "5".
    At line:1 char:1
    + New-Object System.Collections.Generic.HashSet[int] -ArgumentList @($c ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodException
        + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
    
    C:\>
    

    有办法吗?

    2 回复  |  直到 6 年前
        1
  •  0
  •   Theo    6 年前

    我一直在摆弄这件事,似乎这件事很管用:

    [int[]]$c = 1,2,3,4,5
    [System.Collections.Generic.HashSet[int]]::new([System.Collections.Generic.IEnumerable[int]]$c)
    

    你甚至可以忽略 [System.Collections.Generic.IEnumerable[int]] 分头来做吧

    [int[]]$c = 1,2,3,4,5
    [System.Collections.Generic.HashSet[int]]::new($c)
    

    没有 将数组声明为 [int[]] 它不起作用,你会得到错误的

    无法转换类型为“System.Object[]”的“System.Object[]”值 键入“System.Collections.Generic.IEnumerable`1[System.Int32]”。

    类型化 [INT[] ] ,变量的类型 c$ System.Int32[] 而不是简单 System.Object[] 这正是构造函数想要的。

    希望有所帮助

        2
  •  -1
  •   Lee_Dailey    6 年前

    显然,您不能像这样将整个集合添加到哈希集中。[ 皱眉 ]您需要遍历集合并使用 .Add() 方法。如果添加集合 直接地 你得到的整个系列 集合中的项。哎哟!

    所以你需要这样的东西…

    $HashSetTest = [System.Collections.Generic.HashSet[string]]::new()
    $FileExtList = (Get-ChildItem -LiteralPath $env:TEMP -File).Extension
    
    $FileExtList.Where({$_}).ForEach({[void]$HashSetTest.Add($_)})
    
    $HashSetTest.GetType()
    '=' * 40
    $HashSetTest.Count
    '=' * 40
    $HashSetTest
    '=' * 40
    

    输出…

    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     HashSet`1                                System.Object
    ========================================
    10
    ========================================
    .csv
    .zip
    .txt
    .json
    .log
    .ini
    .tmp
    .bmp
    .js
    .ani
    ========================================