howdy y'all,
i vaguely recall seeing a post in this subreddit some time back that said the ArrayList had been deprecated. now i can't find it. [blush] the only things that show up on a net search are a couple of posts aimed at C# from several years ago.
so, am i misremembering things again?
take care,
lee
-ps
found the reason ... but i don't really understand it. lookee ...
ArrayList vs List<> in C# - Stack Overflow
-
http://stackoverflow.com/questions/2309694/arraylist-vs-list-in-c-sharp
lee-
I've come across two ways of creating a new System.Collections.ArrayList object:
$arrA = New-Object System.Collections.ArrayList $arrB = [System.Collections.ArrayList]@()
I have two questions:
-
for arrB, if I'm reading this right, @() is creating an empty array and then it's being cast into [System.Collections.ArrayList]?
-
Working with the created object is the same for me either way, but are there any differences I may be missing?
I made a quick test and casting was faster, but the difference is only noticeable when creating new arrays reaches the hundreds of thousands to millions (tested on an old A10-7870k). Doesn't really matter when I'm creating one array, but I thought it was mildly interesting.
$max = 100000
$ticksStart = (Get-Date).Ticks
for ($i = 0; $i -lt $max; $i++)
{
$arrA = New-Object System.Collections.ArrayList
}
$ticksEnd = (Get-Date).Ticks
Write-Host 'arrA (ticks) : ' ($ticksEnd - $ticksStart)
$ticksStart = (Get-Date).Ticks
for ($i = 0; $i -lt $max; $i++)
{
$arrB = [System.Collections.ArrayList]@()
}
$ticksEnd = (Get-Date).Ticks
Write-Host 'arrB (ticks) : ' ($ticksEnd - $ticksStart)
arrA (ticks) : 64310000
arrB (ticks) : 3170000I'm fully aware that ArrayList is not recommended for new development. That being said, do you still use it when you are writing PowerShell? I wish I could add a Poll to solicit the responses more easily, but Poll option is greyed out.
I've been roasted for using it before, but it's just so much more familiar and convenient to instantiate, for me.
[System.Collections.ArrayList]$list = @()
Slim and sexy, concise, looks like other PS syntax I'm used to.
[System.Collections.Generic.List[object]]::new()
Newfangled, unwieldy, uses C# constructor, ugly. Requires me to think about what types I'll have in my list. Smug.
(Obviously I'm feeling a little silly here / tongue in cheek, but I really do feel like I just don't want to bother getting used to the new way.)
EDIT: Some of the advice is helpful, but mostly what I was hoping to find out was whether I'm alone or whether you all use ArrayList. It's kind of like, I know I'm supposed to drink 8 glasses of water today, but I have a suspicion that few people actually do. So that's why I'm asking.
FINAL EDIT: After seeing that most people don't use ArrayLists anymore, I think I'm going to learn Lists and do things properly. Turns out I'm in the minority, at least on this sub. Thanks for the discussion everyone.
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
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)
I am trying to understand when to us what.
I use generic.list a peer uses ArrayList.
Just curious.
Hi.
I writing scripts that involve multiple API and due to the sequential nature of the single-threaded approach, some take a lot of time to execute.
For a long time I used ArrayList and "+=" approach to create list of arrays but it seems that it's deprecated and not really a best practice. See LINK1 and LINK2.
So, until now, I used approach from LINK1: declare empty ArrayList, looped through the results of my API call with foreach and then added entries with +=.
Of course, it doesn't work with Foreach-Object -Parallel, because ArrayList isn't thread safe.
I tried using ConcurrentBag but I can't access my items like I did in the past.
I.e., if I declare:
$devices = [System.Collections.Concurrent.ConcurrentBag[PSObject]]::new()
I'd like to be able to:
-
$devices[0] - select the first device from the list
-
$devices | Where-Object {$_.SerialNumber -eq "ABC1234567890"}
Doing any of these returns all the results.
Is there any other structure, similar to ArrayList or am I doing something wrong here?