5

I have used hashCode() method from String API and it is generating value as 99162322 for the below code:

String str = "hello";           
System.out.println(str.hashCode());

Is there any Java API which generate a hash value with only 5 digits (63346) like Oracle SQL as below?

select ORA_HASH('hello','99999') from dual  --63346
durron597
  • 31,968
  • 17
  • 99
  • 158
Sudarsana Kasireddy
  • 922
  • 1
  • 8
  • 24

3 Answers3

5

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.

Community
  • 1
  • 1
rgettman
  • 176,041
  • 30
  • 275
  • 357
1

Not really sure why you need this, but you could MOD the result, for example...

String h = "blah";

int hash = h.hashCode() % 100000; // Would give numbers from 0-99999
int hash = h.hashCode() % 10000; // Would give numbers from 0-9999

I suppose you need it to return exactly the same number as the Oracle function thought right?

BretC
  • 4,141
  • 13
  • 22
  • Thanks for response Bret. yes similar to Oracle, but I need Unique hash code. for ex "str1***".hashcode%10000 should not same as as "str***".hashcode%10000. with MOD i may get same results with different String right? – Sudarsana Kasireddy Apr 15 '15 at 17:53
  • @SudarsanaKasireddy - in this context, there's not really any such thing as a "unique" hash code - especially if you've only got 5 digits to represent every possible string that you could ever create. As stated below, the "hashCode" in Java is basically used to organise data into a series of "buckets". The "equals" method can be used to tell if two instances with the same hash code are the same "thing" or not. I hope this clarifies things. – BretC Apr 15 '15 at 19:07
  • Tks Bret...I will try different way to generate unique values with 5 digits – Sudarsana Kasireddy Apr 15 '15 at 20:35
  • @SudarsanaKasireddy Unless your strings are less than 6 digits long and contain only numbers, this will be impossible – BretC Apr 15 '15 at 23:05
1

The most straightforward way to do this would simply be to use modulus, i.e.:

int hash = str.hashCode() % 100000; // results 0-99999

However, whenever you create a hash function, you need to be aware of a high possibility of collisions, which can affect performance.

Do you need your hash function to output exactly the same hash for a string as the Oracle Hash? Note that ORA_HASH can return any max, by specifying the max_bucket argument. See linked documentation

Further reading: What is a good Hash Function?

Community
  • 1
  • 1
durron597
  • 31,968
  • 17
  • 99
  • 158