[Guava official documentation] 5. Guava's Object public method (Common Object Utilities E

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.