1

Basically i am using the hasCode() from the Objects class to obtain a hash code of some strings.

I want that number to represent a position in the array. Basically a hash table. I havent written code for this yet.

I had in mind:

 int hashNumber = SomeString.hascode(), pos; 
 String array[] = new String[10];

 if (hashNumber > 0)
   pos = hashNumber % array.length
 if (hasNumber < 0 )
   //dont know what to do

I do know for a fact that hashCode can return a negative integer. What to do if its negative integer? i though about adding the array length

  pos = hashNumber + array.length

is this the best way?

thanks in advance

Alessandroempire
  • 1,640
  • 4
  • 31
  • 54

2 Answers2

3

If hashNumber is negative, just get the mod of -hashNumber (which will be positive):

if (hashNumber >= 0)
  pos = hashNumber % array.length
else
  pos = -hashNumber % array.length

Or for a single expression that will work for both:

pos = (hashNumber % array.length + array.length) % array.length

See this answer to a question about Java's behavior with mod and negative numbers.

Community
  • 1
  • 1
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
  • i just tested it. and its giving me a negative integer. In other words: it does not work. – Alessandroempire Jan 25 '13 at 19:07
  • Interesting... I did a quick test in Python and assumed modulo would be implemented the same elsewhere. I would expect [`-3 % 5 == 2`](https://www.google.com/search?q=-3+%25+5). – Andrew Clark Jan 25 '13 at 19:19
  • question: why do you do: %array.length outside the parenthesis? with: (hashNumber % array.length + array.length) it will give you a number between the array length. It seems redundant to me. – Alessandroempire Jan 25 '13 at 19:58
  • Consider `(9 % 10 + 10)`, that would become `9 + 10` or `19`, so we need the `% 10` on the outside to bring it back in range. It is only for negative numbers where the value inside of the parentheses will be in the right range. – Andrew Clark Jan 25 '13 at 20:03
  • o yes yes. Well i decided to make an IF, and when its a negative integer: it will calculate the position using: (hashNumber % array.length + array.length) and for what i seen its valid. – Alessandroempire Jan 25 '13 at 22:22
  • `Math.abs` works fine for this problem and is much more concise than the first part of my answer. My code is formatted the way it is just to be consistent with the OP's question. The second block of code is for actually giving a correct modulus, for example `-1 % 8` gives `-1` in Java and `7` in Python. The expression I gave will give `7` in Java instead of `1` like `Math.abs(-1) % 8`. – Andrew Clark Jan 03 '14 at 17:18
  • There is one int value X in java such that both X and -X are negative: `Integer.MIN_VALUE` (`-Integer.MIN_VALUE == Integer.MIN_VALUE`). So this answer would not work in the case that a user decided to present the program with a string that had a hash code equal to that value. – M. Justin May 15 '23 at 16:42
0

http://docs.oracle.com/javase/specs/jls/se5.0/html/expressions.html#15.17.3

the sign of the result equals the sign of the dividend.

See here for more discussion: How does java do modulus calculations with negative numbers?

To be safe, you could simply use pos = Math.abs(hashNumber) % array.length

Community
  • 1
  • 1
Tom Carchrae
  • 6,398
  • 2
  • 37
  • 36