Guava - Useful Classes
- Guava - Optional Class
- Guava - Preconditions Class
- Guava - Ordering Class
- Guava - Objects Class
- Guava - Range Class
- Guava - Throwables Class
- Guava - LoadingCache Interface
Guava - Collection Utilities
- Guava - Collections Utilities
- Guava - MultiSet Interface
- Guava - MultiMap Interface
- Guava - BiMap Interface
- Guava - Table Interface
Guava - String Utilities
- Guava - String Utilities
- Guava - Joiner class
- Guava - Splitter
- Guava - CharMatcher
- Guava - CaseFormat
Guava - Primitive Utilities
- Guava - Primitive Utilities
- Guava - Bytes
- Guava - Shorts
- Guava - Ints
- Guava - Longs
- Guava - Floats
- Guava - Doubles
- Guava - Chars
- Guava - Booleans
Guava - Math Utilities
Guava - Useful Resources
Guava - Objects Class
Objects class provides helper functions applicable to all objects such as equals, hashCode, etc.
Class Declaration
Following is the declaration for com.google.common.base.Objects class −
@GwtCompatible public final class Objects extends Object
| Sr.No | Method & Description |
|---|---|
| 1 |
static boolean equal(Object a, Object b) Determines whether two possibly-null objects are equal. |
| 2 |
static <T> T firstNonNull(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.ToStringHelper toStringHelper(Class<?> clazz) Deprecated. Use MoreObjects.toStringHelper(Class) instead. This method is scheduled for removal in June 2016 |
| 5 |
static Objects.ToStringHelper toStringHelper(Object self) Deprecated. Use MoreObjects.toStringHelper(Object) instead. This method is scheduled for removal in June 2016. |
| 6 |
static Objects.ToStringHelper toStringHelper(String className) Deprecated. Use MoreObjects.toStringHelper(String) instead. This method is scheduled for removal in June 2016. |
Methods Inherited
This class inherits methods from the following class −
- java.lang.Object
Example - Usage of Objects equals() method
GuavaTester.java
package com.tutorialspoint;
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));
}
}
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;
}
}
Output
Run the GuavaTester and verify the output −
false
Example - Usage of Objects hashCode() method
GuavaTester.java
package com.tutorialspoint;
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.hashCode());
}
}
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;
}
}
Output
Run the GuavaTester and verify the output −
85871