Guava Learning--objects

Source: Internet
Author: User

Reprint: https://my.oschina.net/realfighter/blog/349821

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, the ToString debugging information is incomplete, etc., in guava, it provides the objects class to help us simplify the implementation of these common methods.

Several important methods, such as: Equal,hascode,firstnonnull,tostringhelper and so on.

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.

Import com.google.common.base.Objects;
Import Org.junit.Test;

public class Objectstest {

Printing method
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
When you determine that two objects are exactly the same, Hashcode needs the same, except that equals is 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.
Print (Objects.firstnonnull (null, "You were null"));//this is null
Print (Objects.firstnonnull ("I am NOT NULL", "is 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)
Comment omitnullvalues after output: Objectstest{name=realfighter, Age=null}
. Omitnullvalues ()
. toString ()); Objectstest{name=realfighter}
}

}

Guava Learning--objects

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.