Monday, 13 July 2015

java.util.Objects class

java.util.Objects class provides number of utility methods to work with java Objects. By using Objects class, you can do following tasks effectively.
         1. You can generate hash code for given object
         2. You can check equality of two objects
         3. You can check deep equality of two objects
         4. You can check for null condition of given object
         5. You can compare objects using specific comparator.

1. Generate hash code for given object
Objects class provides following method, to generate hash code for an object.

public static int hash(Object... values)
Generates a hash code for a sequence of input values.
import java.util.Objects;

public class Employee {
 private String firstName;
 private String lastName;
 private int age;
 private int id;

 Employee(int id, String firstName, String lastName, int age) {
  this.firstName = firstName;
  this.lastName = lastName;
  this.age = age;
  this.id = id;
 }

 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 getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 @Override
 public boolean equals(Object employee) {
  if (Objects.isNull(employee))
   return false;

  if (!(employee instanceof Employee))
   return false;

  Employee emp = (Employee) employee;

  return id == emp.id;
 }

 @Override
 public int hashCode() {
  return Objects.hash(firstName, lastName, age);
 }
}


public class EmployeeTest {
 public static void main(String args[]) {
  Employee emp1 = new Employee(1, "Hari Krishna", "Gurram", 26);
  Employee emp2 = new Employee(1, "Hari Krishna", "Gurram", 26);
  Employee emp3 = new Employee(2, "Hari Krishna", "Gurram", 27);
  Object obj = new Object();

  System.out.println("emp1 hashcode : " + emp1.hashCode());
  System.out.println("emp2 hashcode : " + emp2.hashCode());
  System.out.println("emp3 hashcode : " + emp3.hashCode());

  System.out.println("Is emp1 and emp2 equal : " + emp1.equals(emp2));
  System.out.println("Is emp1 and emp3 equal : " + emp1.equals(emp3));
  System.out.println("Is emp1 and obj equal : " + emp1.equals(obj));
  System.out.println("Is emp1 and null equal : " + emp1.equals(null));
 }
}


Output
emp1 hashcode : -1761213639
emp2 hashcode : -1761213639
emp3 hashcode : -1761213638
Is emp1 and emp2 equal : true
Is emp1 and emp3 equal : false
Is emp1 and obj equal : false
Is emp1 and null equal : false

If you want to generate hash code manually, go through following article.

2. Check equality of two objects
Objects class provides equals method, which returns true, if two objects are equal, else false.

public static boolean equals(Object a, Object b) {
         return (a == b) || (a != null && a.equals(b));
}

3. Check deep equality of two objects
it is same as equals method, but if both a and b are arrays, the equality is evaluated using Arrays.deepEquals. Go through following article, to know more about Arrays.deepEquals method.


4. Check for null condition of given object
Objects class provides following methods to check for null conditios of given object.

Method
Description
public static boolean isNull(Object obj)

Returns true if obj is null otherwise returns false.
public static boolean nonNull(Object obj)
Returns true if obj is non-null otherwise returns false.
public static <T> T requireNonNull(T obj)

If obj is null, then it throws NullPointerException.
public static <T> T requireNonNull(T obj, String message)
If obj is null, then it throws NullPointerException with given message.
public static <T> T requireNonNull(T obj, Supplier<String> messageSupplier)

If obj is null, then it throws NullPointerException with given message.

5. Compare objects using specific comparator
Objects class provides compare method, to compare objects using specific comparator.

public static <T> int compare(T a, T b, Comparator<? super T> c) {
         return (a == b) ? 0 :  c.compare(a, b);

}



No comments:

Post a Comment