Guava Library Learning: Learning from obejects utility classes in guava

Source: Internet
Author: User

Link Address: http://www.xx566.com/detail/128.html

The object class in Java is a superclass of all Java classes (that is, ancestors), all objects implement methods in the object class, and in daily work, we often need to rewrite several of them, such as equals, toString, hashcode, and so on, while at work , we implement these methods are sometimes more painful, such as the Equals method to judge non-null, tostring debugging information is incomplete, etc., in guava, it provides the objects class to help us simplify the implementation of these common methods, next, we come together to learn.

First, we look at its source code, which contains a number of important methods, such as: Equal,hascode,firstnonnull,tostringhelper, etc., we introduced in turn.

Equal: The function is the same as the Object.Equals method, which is used to determine whether two objects are equal, regardless of whether the object is empty, and avoids throwing the nullpointerexception when the object is null.

Hascode: The function and the Object.hascode method is the same, used to produce hascode value of the object, generally in the work, overriding the Equals method to overwrite the Hascode method, when the two objects are identical, both equals and hascode of the object need to be true.

Firstnonnull: Pass in two parameters, return the one that is not empty in two arguments, and return the first argument if neither is empty.

Tostringhelper: Used to return a Tostringhelper object.

Tostringhelper: Is the static inner class of objects, which is the dynamic addition of output information for debugging of the ToString method, where the Add (String name, Object value) method is used for adding a set of key-value outputs; AddValue ( Object value) is used to add an unnamed value output, but guava recommends using the Add method to specify a name;omitnullvalues to configure Tostringhelper for value. Ignores the Null property at ToString.

Next, let's take a look at the following code, which is simple to apply the above method, the code is as follows:

package guava; import com.google.common.base.objects;import org.junit.test; /**  * objects:object method Tool Class  * User: Realfighter * Date: 2014/8/20 *  time: 9:42 */public class objectstest {     //Printing Methods      private static void print (object obj)  {         system.out.println (string.valueof (obj));    }       @Test     public void testobjects ()  {         //equal method: Used to determine whether two objects are equal, avoid null pointers          Objects.equal (null, 123);//false        objects.equal (123,  123);//true        objects.equal (123, null);//false         objeCts.equal (null, null);//true         // Hashcode method: Gets the hashcode value generated after hashing the incoming field         //determines that two objects are exactly the same, except that equals is true, Hashcode also needs the same          //firstnonnull: passing in two parameters, returning the one that is not empty in two arguments, If neither is empty, the first argument is returned.         print (Objects.firstnonnull (null, "You are null"));// This is null        print (Objects.firstNonNull ("I am  not null "," You are null "));//i am not null         print (Objects.firstnonnull (null,null));//nullpointerexception          //tostringhelper method: For easy and enjoyable writing ToString method          Print (Objects.tostringhelper (this)                  .add ("NAMe ", " Realfighter ")                  .add ("Age",  null)         // Note omitnullvalues after output:objectstest{name=realfighter, age=null}                 .omitnullvalues ()                  .tostring ());  //objectstest{name=realfighter}     } }


Guava Library Learning: Learning from obejects utility classes in guava

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.