Home > Blogs > Pre-allocating a HashSet
In the section about HashSet, I provided this example of creating a HashSet that contains 50 million items:
static void Main(string[] args)
{
int maxSize = 50000000;
Console.WriteLine("Max size = {0:N0}", maxSize);
// Initialize a List<long> to hold maxSize items
var l = new List<long>(maxSize);
// now add items to the HashSet
for (long i = 0; i < maxSize; i++)
{
if ((i % 1000000) == 0)
{
Console.Write("\r{0:N0}", i);
}
l.Add(i);
}
Console.WriteLine();
// Construct a HashSet from that list
var h = new HashSet<long>(l);
Console.WriteLine("{0:N0} items in the HashSet", h.Count);
After I submitted my column, I experimented a little more and discovered that if I remove all the items from the HashSet, I'm left with an empty collection that has a capacity of 50 million items! Removing each item (or calling Clear()) takes the items out of the collection, but doesn't reduce the capacity. You can use this technique to create a HashSet that contains up to 89,478,457 items--the same as the maximum Dictionary size.
Take advantage of special member promotions, everyday discounts, quick access to saved content, and more! Join Today.
Inside the Android OS: Building, Customizing, Managing and Operating Android System Services