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 9044469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:59:51+00:00 2026-06-16T10:59:51+00:00

From java HashMap’s source code it is clear that its space is expanded twice

  • 0

From java HashMap’s source code it is clear that its space is expanded twice when the space threshold is reached.

I thought about an use case where all 6 elements are stored under same index in linked fashion. The HashMap(size 10) with threshold 7(10*.75) gets expanded when the 7th element arrives. here actually there is no need of expansion since all are saved under one index.

kindly enlighten me

        void addEntry(int hash, K key, V value, int bucketIndex)
        {
            Entry<K,V> e = table[bucketIndex];
            table[bucketIndex] = new Entry<K,V>(hash, key, value, e);
            if (size++ >= threshold)
                resize(2 * table.length);
        }

        void resize(int newCapacity)
        {
            Entry[] oldTable = table;
            int oldCapacity = oldTable.length;
            if (oldCapacity == MAXIMUM_CAPACITY) {
                threshold = Integer.MAX_VALUE;
                return;
            }

            Entry[] newTable = new Entry[newCapacity];
            transfer(newTable);
            table = newTable;
            threshold = (int)(newCapacity * loadFactor);
        }
  • 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-16T10:59:52+00:00Added an answer on June 16, 2026 at 10:59 am

    You say there’s no need to resize, since the HashMap can hold these entries.

    However a HashMap ideally should be providing constant access time (O(1)). The resizing occurs in order to try and provide this access time. By reorganising the buckets a lookup for a key should ideally reference a bucket with only one value (to avoid iterating through a list of entries).

    In the get() method you’ll find this line:

    for (Entry<K,V> e = table[indexFor(hash, table.length)];
    

    The HashMap is using the indexFor() method to identify the bucket, and then it will iterate through the buckets to find a matching key. In order to optimise this the iteration should ideally only occur once (you can’t avoid the bucket lookup)

    This points to hashcodes ideally being equally distributed across the int range (2^31-1). You can make an objects hashcode constant (e.g. 1), but then you can see the HashMap can’t do anything but dump all entries in one bucket, and performance is consequently impacted.

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

Sidebar

Related Questions

I use scala.collection.immutable.HashMap<A,B> from some Java code and would like to use the Scala-native
It has been reported that from Java JDK 1.3.1 to JDK 1.4.0 HashMap is
Possible Duplicate: Java Hashmap: How to get key from value? I know that a
I want to store values and retrieve them from a Java HashMap. This is
From Java, is it possible to get the complete commandline with all arguments that
From Java, I run a C++ app which logs its progress as it proceeds,
i get a list from my java code and I would like to create
I have a java.util.HashMap object m (a return value from a call to Java
Very often I have to use objects from the java.util.collection package, objects that conform
I am manually converting code from Java to C# and struggling with (what I

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.