i'm getting behavior don't understand java hashcode (using lombok). i've got abstract object storable things i'm storing in various datastores.
public abstract class storable implements serializable { ... } @data @equalsandhashcode(of="url", callsuper=false) @slf4j @tostring(of="url") public final class foo extends storable { private url url; public foo(@nonnull url url, ...) { super(); this.url = url; ... } ... } when new multiple foos new foo(new url("http:///www.foo.com")) , iterate on them , check each foo.hashcode() same value. if terminate program , start run, foos in new run have different hashcode value though identical data standpoint. discrepancy causing me grief because i'm trying use hashcode identify unique objects run run. perhaps more oddly, given url i'm using testing i'm seeing 1 of same 4 integers every time.
am missing either java's default gethashcode() implementation or lombok's @equalsandhashcode implementation? or there url cause have different hashcode value? in advance help!
if using java 7 using alternate murmur hashcode implementation not guaranteed produce same hashcode across jvm instances (or same jvm run multiple times)
article discusses change hashcode in java 7
relevant section:
a couple more words alternative hash code:
it isn’t exposed publicly through string class. can access using (unofficial) sun.misc.hashing.stringhash32 method
unlike original hash code, hash32 2 strings containing same characters running in different jvms (on same machine or on different machines) isn’t guaranteed same (in fact won’t be, since “hashing_seed” value included in calculation initialized on jvm startup using current time)
the purpose of alternative hash code give better performance hashmap , related classes string keys , thwart hash-collision denial of service attacks
its usage isn’t enabled default. need set “jdk.map.althashing.threshold” property enable it. if set value x, hashmap , related classes capacity @ least x use alternative hashing algorithm.
a word of caution if want enable alternative hashing: prior java 7u40 (ie. versions between java 7u6 , java 7u39) had performance issue meant hashmap creation while alternative hashing enabled slower needed be. if want enable alternative hashing, ensure have latest java 7 runtime.
this added in java 7u6 has been removed in java 8.
here's internal implementation of java 7's murmur hash function on grep code.
here's link java 7's hashmap implementation uses new hash code calculation if key in map string http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7u40-b43/java/util/hashmap.java#hashmap.hash%28java.lang.object%29
Comments
Post a Comment