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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:44:10+00:00 2026-06-02T22:44:10+00:00

I have a class having two methods: getY() and getX() (it’s a sort of

  • 0

I have a class having two methods: getY() and getX() (it’s a sort of map).
Y’s and X’s domain is [-1000.0, + 1000.0], and they’re doubles.
I have written a hashCode() method:

@Override
public int hashCode() 
{
    int hash=(int) (getX()/EPSILON);
    hash+= (int) ( (1000.0/EPSILON)*getY() );
    return hash;
}

Where EPSILON is the maximum error tolerated.
But the problem is that values are too high and I get an overflow if X=1000.0 and Y=1000.0 .
How to write a hashCode() method that handles the overflow and is still able to return two hash codes for two different objects in every case?

  • 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-02T22:44:12+00:00Added an answer on June 2, 2026 at 10:44 pm

    This is how you should do it for doubles (see here):

    long t = Double.doubleToLongBits(d);
    result = prime * result + (int) (t ^ (t >>> 32));
    

    For prime use a smallish prime number, a standard value is 37.

    Initialize result with another prime, don’t start from zero. Standard for that is 17.

    This is from Effective Java by Josh Bloch.

    As far as your immediate questions:

    You handle overflow by ignoring it. Overflow is welcome in hash code calculation.

    Of course you cannot guarantee distinct hash for every possible value. The goal is good dispersion, not uniqueness.

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

Sidebar

Related Questions

If I have a class called myclass does having static methods within the class
I am having a protocol in which I have two methods as follows. @protocol
I have these two methods for thread-exclusive access to a CMyBuffer object: Header: class
I am having problem with overriding Form Action URL I have a class to
Im having rather an annoying problem. I have a class called Person and a
I have a form in spring mvc 2 having command class Class1 I want
I have a div having a css class MyClass. Inside this div, I can
Im having a problem with python.. I have a binary tree node type: class
I have class that represents users. Users are divided into two groups with different
I'm trying to provide a functionality of having two Methods one called StartTask(action mymethod)

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.