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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:35:32+00:00 2026-05-15T12:35:32+00:00

Although I understand very well what HashCode is and what a Hash Table does,

  • 0

Although I understand very well what HashCode is and what a Hash Table does, I have to admit I don´t know how to use it (Beyond a common dictionary). I wanted to implement my own Hash Table so first I want to know the very basic about Hash:

  • I know I can get the hash code with getHashCode()/hashCode() in Java and Scala. How is this number determined. (Just out of curiosity)
  • If I know the HashCode of an object, how can I access it? That is, how can I call that memory bucket?
  • Can I change/set a variables HashCode?

Now, I have a very big (about 10^9) list of Int. I am going to access some of them (from none to all) and I need to do it in the fastest way possible. Is a hash table THE BEST way to do it?

PS: I don’t want to discuss it, I just want to know if the HashTable is know to be the most efficient. If there exist other good methods maybe you can point me to them.

Thanks,

  • 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-05-15T12:35:33+00:00Added an answer on May 15, 2026 at 12:35 pm

    The hash code is just a number that is guaranteed to be the same for every type of object “the same” as the original object.

    This means that returning “0” for every hash code call would be valid, but self-defeating. The point is there can (and in most cases will) be duplicates.

    If you know the hash code of an object, you cannot necessarily access it. per my example above, if all objects returned “0”, you still couldn’t ask which object has hash code 0. However, you could ask for ALL objects with hash code 0 and look through them (this is what a hashtable does, it reduces the amount of iterating by getting just the ones with the same hash code, then looks through those).

    If you were to set (Change) a HashCode, it would not be a hash code because the value given for an object with a given “State” cannot change.

    As for the “Best Way” to do it, the fewer unique objects that return the same hash code, the better your hash tables will perform. If you have a long list of “int”, you can just use that int value as your hash code and you will have that rare perfect hash–where each object maps to exactly one hash code.

    Note that hashtable isn’t really appropriate for this situation of storing ints. It’s better for situations where you are trying to store complex objects that are not so easy to uniquely identify or compare using other mechanisms.

    The problem with your “List of Int” is that if you have the number 5 and you want to look it up in your table, you are just going to find a number 5 there.

    Now, if you want to see if the number 5 exists in your table or not, that’s a different matter.

    For a set of numbers with few holes you could make a simple boolean array. If a[5] exists (is true), than a is in the list. If your set of numbers is very sparse (1, 5, 10002930304) then that wouldn’t be a very good solution since you’d store “False” in spots 2, 3, 4 and then a whole bunch of them before the last number, but it is a direct lookup, a single step that never takes any longer no matter how many numbers you add–O(1).

    You could make this type of storage MUCH denser by making it a binary lookup into a byte array, but unless you’re pretty good with bit-manipulation, skip it. It would involve stuff that looks like this:

    public boolean doesNumberExist(int number) {
        return bytes[number / 8] & ( 1 << number % 8);
    }
    

    and this still runs out of memory if your highest number is really big.

    So, for a large sparse list I’d use a sorted integer array instead of a lightly populated boolean array. Once it’s sorted as an array you just do a binary search; start in the middle of the sorted array, If the number you want is higher, then divide the top half of the list in the center and check that number, repeat.

    The sorted int array takes a few more steps but not too many more and it doesn’t waste any memory for non-existent numbers.

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

Sidebar

Ask A Question

Stats

  • Questions 442k
  • Answers 442k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use zero-width assertions: my @array = split /(?<=\d)x(?=\d)/, $string; This… May 15, 2026 at 5:50 pm
  • Editorial Team
    Editorial Team added an answer Just to close this question, I had an answer from… May 15, 2026 at 5:50 pm
  • Editorial Team
    Editorial Team added an answer You have to add the back button in the root… May 15, 2026 at 5:50 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.