11

I need to convert a HashSet to an ArrayList?

$hashset = New-Object System.Collections.Generic.HashSet[int]
$hashset.Add(1)
$hashset.Add(2)
$hashset.Add(3)

$arraylist = New-Object System.Collections.ArrayList
# Now what?
6
  • did you try [array]$arraylist = $hashset . both variables after this still pipe to "get-member" with the same output. What is your goal in creating an array vs a "hashset" Commented Oct 24, 2018 at 6:29
  • @RobertCotterman I need an ArrayList. Arrays are fixed size and don't have Sort(). Commented Oct 24, 2018 at 6:35
  • 1
    Daniel, arrays in Powershell can be sorted with Sort-Object and you can add items to them (it will create a new array to do that, but from a language standpoint it doesn't really matter what the type is -- unless, of course, you totally ed the ArrayList to pass to a method that requires one. But I hope such APIs are long dead now.) Commented Oct 24, 2018 at 7:50
  • @Joey I often prefer using ArrayList or List because the way PowerShell handles arrays, especially when adding new items, is damn slow. Commented Oct 24, 2018 at 7:53
  • @Joey The lists can become quite big and I don't want to copy big lists every time I add one item. Beside that, I also need GetRange() which also isn't supported by the Array that Sort-Object returns. Commented Oct 24, 2018 at 8:02

3 Answers 3

7

One way, using CopyTo:

$array = New-Object int[] $hashset.Count
$hashset.CopyTo($array)
$arraylist = [System.Collections.ArrayList]$array

Another way (shorter, but slower for large hashsets):

$arraylist = [System.Collections.ArrayList]@($hashset)

Also, I strongly recommend to favor List over ArrayList, as it's pretty much deprecated since the introduction of generics:

$list = [System.Collections.Generic.List[int]]$hashset
Sign up to request clarification or add additional context in comments.

7 Comments

Didn't know about lists. Thanks.
Nice, List[int]++. Worth noting that if you're using PowerShell >5.0, the constructor takes any IEnumerable[int] (including HashSet[int]) directly as its argument, faster than casting it: [System.Collections.Generic.List[int]]::new($hashset)
@MathiasR.Jessen Did you actually try if it's faster? Because PowerShell's cast does use a constructor implicitly, if possible.
@MathiasR.Jessen I've already removed the @() in my answer, it's unnecessary, as you correctly stated. Another nice benefit of using List instead of ArrayList! Then you can just do the cast and it will be about the same speed is it's (most likely) using the constructor internally.
|
5

Unsure if this is what you are after but it here you go...

$hashset = New-Object System.Collections.Generic.HashSet[int]
$null = $hashset.Add(1)
$null = $hashset.Add(2)
$null = $hashset.Add(3)
# @($hashset) converts the hashset to an array which is then 
# converted to an arraylist and assigned to a variable
$ArrayList = [System.Collections.ArrayList]@($hashset)

Comments

1

You can also add every item from hashtable to array using foreach loop:

$hashset = New-Object System.Collections.Generic.HashSet[int]
$hashset.Add(1)
$hashset.Add(2)
$hashset.Add(3)

$arraylist = New-Object System.Collections.ArrayList
# Now what?
foreach ($item in $hashset){
    $arraylist.Add($item)
}

2 Comments

You should use $arraylist.Add($item) | Out-Null or [void]$arraylist.Add($item) to prevent unwanted output to the pipeline.
Thanks. That's how I do it at the moment. But it's unnecessarily verbose for my taste.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.