Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8969523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:36:14+00:00 2026-06-15T17:36:14+00:00

I was wondering how hashtable find the correct index when it increase it’s capacity.

  • 0

I was wondering how hashtable find the correct index when it increase it’s capacity. For example let’s assume I have a hashtable with default capacity 10. Now we have to add (key,value) pair
[14,”hello 1″]

The index that we will get for above key ’14’ using below index mechanism is ‘4’. So hashtable going to save this (key,value) pair inside the index 4.

int index = key.GetHashCode() % 10

Now we keep on adding items into the hashtable and it reaches to the load factor. So it’s time to resize. And let’s assume hastable resize to 20.

Now I’m going to search my old key ’14’ into this hashtable. And as per the index mechanism now I will get the index for this key as 14. So I will start searching into the hashtable from index 14 but ideally it is in index 4.

So my question is how hashtable track the existing key index when it resize? Or does hashtable rehash all existing keys when it resize?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:36:15+00:00Added an answer on June 15, 2026 at 5:36 pm

    I’ve looked through the Shared Source CLI implementation for .Net and it looks like the entries are rehashed upon expansion. However, it is not necessary to recompute the HashCode with .GetHashCode().

    If you look through the implementation you’ll see the expand() method in which the following steps occur:

    1. A temporary bucket array is created and sized to the smallest prime greater than double its current size.
    2. The new array is populated by rehashing from the old bucket array.

    .

    for (nb = 0; nb < oldhashsize; nb++)
    {
        bucket oldb = buckets[nb];
        if ((oldb.key != null) && (oldb.key != buckets))
        {
            putEntry(newBuckets, oldb.key, oldb.val, oldb.hash_coll & 0x7FFFFFFF);
        }
    }
    
    
    
    private void putEntry (bucket[] newBuckets, Object key, Object nvalue, int hashcode)
    {
        BCLDebug.Assert(hashcode >= 0, "hashcode >= 0");  // make sure collision bit (sign bit) wasn't set.
    
        uint seed = (uint) hashcode;
        uint incr = (uint)(1 + (((seed >> 5) + 1) % ((uint)newBuckets.Length - 1)));
    
        do 
        {
            int bucketNumber = (int) (seed % (uint)newBuckets.Length);
    
            if ((newBuckets[bucketNumber].key == null) || (newBuckets[bucketNumber].key == buckets)) 
            {
                newBuckets[bucketNumber].val = nvalue;
                newBuckets[bucketNumber].key = key;
                newBuckets[bucketNumber].hash_coll |= hashcode;
                return;
            }
            newBuckets[bucketNumber].hash_coll |= unchecked((int)0x80000000);
            seed += incr;
            } while (true);
        }
    }
    

    The new array has been built and will be used in subsequent operations.

    Also, from MSDN regarding Hashtable.Add():

    If Count is less than the capacity of the Hashtable, this method is an O(1) operation. If the capacity needs to be increased to accommodate the new element, this method becomes an O(n) operation, where n is Count.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering if the default implementation of Java's Hashtable#hashCode() is broken when the Hashtable
Wondering if someone could help me. I have next to no knowledge with Ajax,
I was wondering how Java orders items in the Map ( HashMap or Hashtable
I was wondering if you knew of a robust implementation of a hashtable in
So, let's say I have this code (VB.Net): Sub Main() dim xxx as string
If I have many queues and each has an unique ID, would a Hashtable
I want to find all entry pairs with maximum value from a Hashtable, my
I am wondering why does Hashtable avoid using negative hashcode ? int hash =
I'm wondering is there a way to overwrite Hashtable (or Dictionary) class so it
A particular class has a Hashtable containing 1..N elements. I'm wondering if there are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.