Though I do not know the exact way String class implements hashCode, the concept of hash code is such that it is possible for multiple object to have the same hash code. Say if I have an approach to add the position of each character to evaluate the hash code the consider the following String HashCode ------ -------- ABC 1+2+3=6 [position of A is 1, B is 2 and so on] = 6 is the hash code
Now given 6 (the hashcode) it is not possible to determine uniquely the whether the object was ABC, BD or CAB. Note that this may not be the way String calculates hash code. But even with a different approach or formula, a similar situation can arise which will prevent you from getting the exact String. So just the hash code is not enough to uniquely determine the String. BUT IN CASE STRING HAS A FORMULA IN WHICH EVERY STRING OBJECT HAS A DIFFERENT HASHCODE VALUE[the best and ideal hash-code algorithm], THEN IT SHOULD BE POSSIBLE. BUT THAT IS MORE UNLIKELY.
I am now certain that you cannot find the String from its hashcode. String calculates hash code as : s*31^(n-1) + s*31^(n-2) + ... + s[n-1]
This formula never guarantees an unique hash code to each string. For example: "ABCDEa123abc" and "ABCDFB123abc" has the same hash code but are not equal since their sequence of characters are different. To iterate, the following will happen:- 1. int hash1 ="ABCDEa123abc".hashCode(); int hash2 = "ABCDFB123abc".hashCode(); //hash1 and hash2 are equal 2.The 2 strings are unequal
So hashcode cannot uniquely identify the string. As I had mentioned earlier, if you can come up with a hash code formula so that every object has a different hash code and hash code not repeating ever, then it can be considered as the ideal hash code.
Joined: Aug 03, 2002
This formula never guarantees an unique hash code to each string
So you are saying a string can have two (or more) hashcode values?
Joined: Jun 01, 2006
No. Not at all. I said that 2 strings can have the same hash-code. The example that followed illustrated it. The 2 strings I showed have the same hash-code but not the same sequence of characters[hence not equal. equals method returns false]. Please examine the same once more.
Barry, you seem to be using "unique" a bit differently than I would. I would say each string has one hashcode, but I would never call that "unique" since other strings can have the same hashcode. I think this is how most people would understand the term here. Or maybe it's just me and Amit.
"I'm not back." - Bill Harding, Twister
Joined: Aug 03, 2002
Well, what I mean is that hashcode() is a many-one mapping. For the same string you get a unique code (does not change when you execute hashcode() a second time during the lifetime of the JVM). Two strings can map onto that same unique hashcode. Going the other way the mapping is one-many, so given a hashcode you cannot find a unique string that maps to it. Or it could be, Jim, I am different since I fell off my bike a couple of weeks ago. In other words we are thinking about the same thing, but differently. [ November 24, 2006: Message edited by: Barry Gaunt ]
We all are thinking the same thing in the same way. There is absolutely no difference in what we all are saying. There is nothing different in the understanding. There are no confusions to be debated or resolved. Absolutely none.
In the API documentation for equals() and hashCode(), it is written that if equals() returns true for two objects, say 'a' and 'b', then their hash-codes MUST be same. But, it further says that the hashcodes need not be different, if equals() returns false.