📜  Guava-对象类

📅  最后修改于: 2020-11-16 06:52:13             🧑  作者: Mango


Objects类提供适用于所有对象的帮助程序功能,例如equals,hashCode等。

类声明

以下是com.google.common.base.Objects类的声明-

@GwtCompatible
public final class Objects
   extends Object

类方法

Sr.No Method & Description
1

static booleanequal(Object a, Object b)

Determines whether two possibly-null objects are equal.

2

static TfirstNonNull(T first, T second)

Deprecated. Use MoreObjects.firstNonNull(T, T) instead. This method is scheduled for removal in June 2016.

3

static int hashCode(Object… objects)

Generates a hash code for multiple values.

4

static Objects.ToStringHelpertoStringHelper(Class clazz)

Deprecated. Use MoreObjects.toStringHelper(Class) instead. This method is scheduled for removal in June 2016

5

static Objects.ToStringHelpertoStringHelper(Object self)

Deprecated. Use MoreObjects.toStringHelper(Object) instead. This method is scheduled for removal in June 2016.

6

static Objects.ToStringHelpertoStringHelper(String className)

Deprecated. Use MoreObjects.toStringHelper(String) instead. This method is scheduled for removal in June 2016.

继承的方法

此类从以下类继承方法-

  • java.lang.Object

对象类的例子

使用您选择的任何编辑器在C:/> Guava中创建以下Java程序

GuavaTester.java

import com.google.common.base.Objects;

public class GuavaTester {
   public static void main(String args[]) {
      Student s1 = new Student("Mahesh", "Parashar", 1, "VI");    
      Student s2 = new Student("Suresh", null, 3, null);    
      
      System.out.println(s1.equals(s2));
      System.out.println(s1.hashCode());    
      System.out.println(
         Objects.toStringHelper(s1)
         .add("Name",s1.getFirstName()+" " + s1.getLastName())
         .add("Class", s1.getClassName())
         .add("Roll No", s1.getRollNo())
         .toString());
   }
}

class Student {
   private String firstName;
   private String lastName;
   private int rollNo;
   private String className;

   public Student(String firstName, String lastName, int rollNo, String className) {
      this.firstName = firstName;
      this.lastName = lastName;
      this.rollNo = rollNo;
      this.className = className;        
   }

   @Override
   public boolean equals(Object object) {
      if(!(object instanceof Student) || object == null) {
         return false;
      }
      Student student = (Student)object;
      // no need to handle null here        
      // Objects.equal("test", "test") == true
      // Objects.equal("test", null) == false
      // Objects.equal(null, "test") == false
      // Objects.equal(null, null) == true        
      return Objects.equal(firstName, student.firstName)  // first name can be null
         && Objects.equal(lastName, student.lastName)     // last name can be null
         && Objects.equal(rollNo, student.rollNo)    
         && Objects.equal(className, student.className);  // class name can be null
   }

   @Override
   public int hashCode() {
      //no need to compute hashCode by self
      return Objects.hashCode(className,rollNo);
   }
   
   public String getFirstName() {
      return firstName;
   }
   
   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }
   
   public String getLastName() {
      return lastName;
   }
   
   public void setLastName(String lastName) {
      this.lastName = lastName;
   }
   
   public int getRollNo() {
      return rollNo;
   }
   
   public void setRollNo(int rollNo) {
      this.rollNo = rollNo;
   }
   
   public String getClassName() {
      return className;
   }
   
   public void setClassName(String className) {
      this.className = className;
   }
}

验证结果

使用javac编译器编译类,如下所示:

C:\Guava>javac GuavaTester.java

现在运行GuavaTester以查看结果。

C:\Guava>java GuavaTester

查看结果。

false
85871
Student{Name=Mahesh Parashar, Class=VI, Roll No=1}