How to generate a checksum for an java object

后端 未结 10 801
生来不讨喜
生来不讨喜 2020-12-13 01:04

I\'m looking for a solution to generate a checksum for any type of Java object, which remains the same for every execution of an application that produces the same object.

10条回答
  •  Happy的楠姐
    2020-12-13 01:30

    Do you want to be able to do this for all Java objects?

    In that case hashCode() doesn't work.

    For some classes hashCode() has a stricter definition which guarantees equality across executions. For example String has a well-defined hashCode implementation. Similarly List and Set have well-defined values, provided all objects that they contain also have well-defined values (note that the general Collection.hashCode() does not require the value to be well-defined).

    For other classes you will have to use reflection recursively with some well-defined formula to build a checksum.

提交回复
热议问题