1

So I know I can convert a string to a hashcode simply by doing .hashCode(), but is there a way to convert (or use some other function if there is one out there) that will instead of returning an integer return a double between 0 and 1? I was thinking of just dividing the number by the maximum possible integer but wasn't sure if there was a better way.

*Edit (more information about why i'm trying to do this): i'm doing a mathematical operation, and i'm trying to group different objects to perform the same mathematical operation in their group but have a different parameter into the function. each member has a list of characteristics that "group" them... so i was thinking to put the characteristics into a string and then hashcode it and find their group value from that

6
  • Why do you need this kind of hashcode? Commented Mar 27, 2012 at 13:11
  • hashCode() is a method defined on Object, the parent class for all Java Classes and returns an int. This cannot be changed. Explain your intent and we might be able to provide a solution... Commented Mar 27, 2012 at 13:12
  • 1
    You can always write your own method that returns a double in that bounds (not named hashCode(), though), but what is it that you want to achieve? Commented Mar 27, 2012 at 13:12
  • @PetroSemeniuk i'm doing a mathematical operation, and i'm trying to group different objects to perform the same mathematical operation in their group but have a different parameter into the function. each member has a list of characteristics that "group" them... so i was thinking to put the characteristics into a string and then hashcode it and find their group value from that Commented Mar 27, 2012 at 13:14
  • 1
    @K2xL: If two objects have same hashcode it DOES NOT mean they are equal. Moreover, it DOES NOT even mean they are similar in any way. You should probably find some other way to group your objects. Commented Mar 27, 2012 at 13:20

3 Answers 3

8

You couldn't just divide by Integer.MAX_VALUE, as that wouldn't deal with negative numbers. You could use:

private static double INTEGER_RANGE = 1L << 32;
...
// First need to put it in the range [0, INTEGER_RANGE)
double doubleHash = ((long) text.hashCode() - Integer.MIN_VALUE) / INTEGER_RANGE;

That should be okay, as far as I'm aware... but I'm not going to make any claims about the distribution. There may well be a fairly simple way of using the 32 bits to make a unique double (per unique hash code) in the right range, but if you don't care too much about that, this will be simpler.

Sign up to request clarification or add additional context in comments.

3 Comments

Fun fact: 0x1.0p32 is how you specify a double constant in hexadecimal.
@LouisWasserman: Is that one of the Java 7 enhancements, or has it been there for a while?
1

Dividing it should be ok, but you might loose some "precision" due to rounding problems, etc, that doubles might have.

In general a hash is used to identify something trying to assure it'll be unique, loosing precision might have problems in that.

You could write your own String.hashCodeDouble() returning the desired number, perhaps using a common hash algorithm (let's say, MD5) and adapting it to your required response range.

Example: do the MD5 of the String to get a hash, then simply put a 0. in front of it...

Remember that the .hashCode() is used in lots of functions in Java, you can't simply overwrite it.

Comments

1

This smells bad but might do what you want:

Integer iHash = "123".hashCode();
String sHash = "0."+iHash;
Double dHash = Double.valueOf(sHash);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.