I understand that this seems to be already discussed and the answer is yes, String.hashCode can generate equal vales for different strings, but quite unlikely (Can Java's hashCode produce same value for different strings?). However it does happen in my application.
The following code will produce the same hashcode: -347019262 (jave 1.7.25)
String string1 = "/m/06qw_"; String string2="/m/0859_"; System.out.println(string1+","+string1.hashCode()); System.out.println(string2+","+string2.hashCode()); I do need hashcode in this case, and I want to use it to generate a unique primary key for a string. it seems that I am not doing it right. Any suggestions please?
Many thanks!
System.identityHashCode(). I think that it is supposed to be unique among the JVM (But I am not sure about that so I don't post it as an answer).Object's.hashCode()on any instanceObject.hashCode()saysclass Object does return distinct integers for distinct objectsso callingSystem.identityHashCode()would work in a single JVM.