简体   繁体   中英

java - generate unique md5 hash for objects

I need to generate unique hash for objects to identify objects with the same exact attributes.

After reading a bit on the subject I decided its best to use MD5 hashing rather than java hashCode (I have a large number of objects, around 200,000-300,000).

I found many examples on how to do MD5 hashing on a string, but didn't find how to do it on an object, so that it will really be unique according to the object's attributes.

Hash code of an object does not need to be unique. By the way it is impossible

Hash code must comply to the following contract:

  • It should return the same value every time it is invoked on the same object in the same state.
  • If two objects are equal according to Object::equals then the hashCode should return the same.

Most of IDEs can generate the method hashCode() but there are some tools even in JDK for generating hashcode eg. java.util.Objects.hash(Object...) .

You can read a great summary here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM