java面试题网

普通会员

483

帖子

9

回复

187

积分

楼主
发表于 2019-08-26 15:24:39 | 查看: 5057| 回复: 0

Guava Objects类

Guava Objects类

Objects类提供适用于所有对象,如equals, hashCode等辅助函数

类声明

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

@GwtCompatible
public final class Objects
   extends Object

类方法

Guava Objects类_www.wityx.com

方法继承

这个类从以下类继承的方法:

java.lang.Object

Objects 示例

使用所选择的编辑器,创建下面的java程序比如 C:/> Guava 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}
 


文章来自www.wityx.com,转载请注明出处!原文地址http://www.wityx.com/post/1248_1_1.html


java面试题交流群:327440556

上一篇:Guava Range类 下一篇:Guava Ordering类
您需要登录后才可以回帖 登录 | 立即注册

java面试题网www.wuliaokankan.cnjava建站系统提供技术支持V2.1 网站地图 © 2016-2018