Oracle's ora_hash
function is designed to provide a hash code for distributing items into buckets efficiently. That makes the purpose of the function with no maximum equivalent to the purpose Java's hashCode()
method.
It appears that in Oracle the maximum value argument just takes the modulus of the hash code by the maximum value + 1.
SQL> select ora_hash('hello', 49999) from dual;
ORA_HASH('HELLO',49999)
-----------------------
13346
In Java, to provide an equivalent value, you can obtain the remainder of dividing the hash code by that maximum value plus one.
int hash = str.hashCode() % (max + 1); // max could be 99999
However, the hashing algorithms for String
and Oracle are different, so the values will differ.
String s = "hello";
System.out.println(s.hashCode() % 100000);
Output:
62322
Also, the range of a Java hash code is that of a signed int
, and the range of ora_hash
is that of an unsigned integer.