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

  • Home
  • SEARCH
  • 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 8613177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:48:30+00:00 2026-06-12T04:48:30+00:00

For an assignment I have to write the code for a generic Hash Table.

  • 0

For an assignment I have to write the code for a generic Hash Table. In an example Put method, there are two lines:

int hash = key.hashCode(); // get the hashcode of the key
int index = compress(hash); // compress it to an index

I was of the understanding that the hashCode method used the key to return an index, and you would place the key/value pair in the array at that index. But here we “compress” the hash code to get the index. What does this method do? How does it “compress” the hash code? Is it necessary and/or preferred?

  • 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-12T04:48:32+00:00Added an answer on June 12, 2026 at 4:48 am

    The hash code can be any integer between -231 and and 231-1. That’s ~4 billion different possible hash codes. If you have, say, 40 hash table buckets, you need to “compress” those 4 billion integers down to the range 0-39.

    A common way to do this is with the modulus operator %. a % b returns the remainder after dividing a by b. For example, 7 % 3 == 1.

    int compress(int hash) {
        return hash % numBuckets;
    }
    

    Note: This isn’t true in all languages, but in Java the sign of the result equals the sign of the dividend, meaning the result of our calculation above will always be non-negative. In C and C++ this is not the case (the sign is implementation defined), and so one would need to take special care to handle negative hash values correctly.

    See integer modulo operators in various programming languages on Wikipedia for a breakdown of how each programming language handles modulus’s sign.

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

Sidebar

Related Questions

As an assignment in operating systems we have to write our own code for
I have to write this code for a homework assignment but I don't even
For a school assignment I have to write x86 assembly code, except I can't
I have an assignment that wants me to write an ternary search algorithm and
I have an assignment about fftw and I was trying to write a small
I have the following assignment: Write a complete 8086 program to perform the calculator
I have been asked to write a program using python for an assignment. I
Here is my issue. I have an assignment where I have to write a
I have assignment when I need to write simple time server and a client
This is the assignment: Write a method that sorts the elements of a matrix

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.