Home > Blogs > Pre-allocating a HashSet

Pre-allocating a HashSet

By  May 13, 2008

Topics: Programming, Windows Programming

In my most recent .NET Guide update, I mention that it's possible to create a large HashSet by creating a List or other collection, and then passing that collection to the HashSet constructor. However, I did not have a way to create an empty HashSet larger than about 48 million items. A little experimentation after I submitted the column revealed a solution.

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.

Become an InformIT Member

Take advantage of special member promotions, everyday discounts, quick access to saved content, and more! Join Today.

Other Things You Might Like

Xamarin Unleashed

Xamarin Unleashed