When the value is null Object. equalsThe method is very troublesome, because you must checkNull. UseThe Objects. equal method can be considered.Null (null-sensitive)EqualsCheck, without worrying aboutNullPointerException.
Objects.equal("a","a");Objects.equal(,"a");Objects.equal("a",);Objects.equal(,);
Note::Objects. equals
ObjectIt is easier to obtain hash values for all domains. Guava'sObjects. hashCode (Object ...)The method can combine the specified domain sequence to obtain a reasonable order-sensitive hash value related to the domain sequence. UseObjects. hashCode (field1, field2,..., fieldn)In place of manual hash calculation.
Note:: Newly introduced in JDK 7ObjectsClass provides the equivalentObjects. hash (Object...) method.
ToStringThe method is very valuable to debug, but it is very troublesome to write. Use Objects. toStringHelper to easily compile availableToString method. Here are two simple examples:
Objects.toStringHelper("x",1 Objects.toStringHelper("MyObject""x",1
Comparator or ComparableThe interface may be very troublesome. For example:
Person Comparable<Person> cmp =(cmp != 0=(cmp != 0
ComparisonChain.
ComparisonChainImplementation of the "Short Circuit" comparison method: it will continue to compare without any non-0 (not equal) results; if it encounters a non-0 result, it will ignore all the subsequent input.
Here
Translator: Dai cangshu