class

Null safe equals method

In this example we shall show you how to use a null safe equals method to compare objects. To use a null safe equals method to compare objects we have performed the following steps:

  • We have created an abstract class, ObjectUtils that has a static method boolean nullSafeEquals(Object o1, Object o2) that determines if two objects are equal. If the two objects are equal it returns true, if one of them is null it returns false. Then it checks if the two obejcts are instances of any kind of array, eg. Object[], boolean[] etc. Then it uses equals() API method of Arrays to determine if the two arrays are equal.
  • We have also created a class A, that has an int and a String field, and overrides equals(Object o) API method of Object. In this method an instance of the class A is compared to an object. If the given object is also instance of A and its fields are equal to the object’s fields, then true is returned.
  • We create two new instances of A, with different parameters and call the nullSafeEquals(Object o1, Object o2) method, in class NullSafeEquals that extends ObjectUtils,

as described in the code snippet below.

package com.javacodegeeks.snippets.core;

import java.util.Arrays;

abstract class ObjectUtils {

    private static final int INITIAL_HASH = 7;
    private static final int MULTIPLIER = 31;
    private static final String EMPTY_STRING = "";
    private static final String NULL_STRING = "null";
    private static final String ARRAY_START = "{";
    private static final String ARRAY_END = "}";
    private static final String EMPTY_ARRAY = ARRAY_START + ARRAY_END;
    private static final String ARRAY_ELEMENT_SEPARATOR = ", ";

    /**
     * Determine if the given objects are equal, returning true if both are null
     * or false if only one is null. Compares arrays with Arrays.equals,
     * performing an equality check based on the array elements rather than the
     * array reference.
     */
    public static boolean nullSafeEquals(Object o1, Object o2) {

  if (o1 == o2) {


return true;

  }

  if (o1 == null || o2 == null) {


return false;

  }

  if (o1.equals(o2)) {


return true;

  }

  if (o1 instanceof Object[] && o2 instanceof Object[]) {


return Arrays.equals((Object[]) o1, (Object[]) o2);

  }

  if (o1 instanceof boolean[] && o2 instanceof boolean[]) {


return Arrays.equals((boolean[]) o1, (boolean[]) o2);

  }

  if (o1 instanceof byte[] && o2 instanceof byte[]) {


return Arrays.equals((byte[]) o1, (byte[]) o2);

  }

  if (o1 instanceof char[] && o2 instanceof char[]) {


return Arrays.equals((char[]) o1, (char[]) o2);

  }

  if (o1 instanceof double[] && o2 instanceof double[]) {


return Arrays.equals((double[]) o1, (double[]) o2);

  }

  if (o1 instanceof float[] && o2 instanceof float[]) {


return Arrays.equals((float[]) o1, (float[]) o2);

  }

  if (o1 instanceof int[] && o2 instanceof int[]) {


return Arrays.equals((int[]) o1, (int[]) o2);

  }

  if (o1 instanceof long[] && o2 instanceof long[]) {


return Arrays.equals((long[]) o1, (long[]) o2);

  }

  if (o1 instanceof short[] && o2 instanceof short[]) {


return Arrays.equals((short[]) o1, (short[]) o2);

  }


  return false;
    }
}

class A {

    public int x;
    public String str;

    public A(int x, String str) {

  this.x = x;

  this.str = str;
    }

    @Override
    public boolean equals(Object o) {

  

  if (o == null) {


return false;

  } else if (o instanceof A) {


A obj = (A) o;



if (this.x == obj.x && this.str.equals(obj.str)) {


    return true;


}

  }

  

  return false;
    }
}

public class NullSafeEquals extends ObjectUtils {

    public static void main(String[] args) {


  A a1 = new A(10, "Nikos");

  A a2 = new A(10, "Dimitrhs");

  

  System.out.println(nullSafeEquals(a1,a2));



  //System.out.println(nullSafeEquals(2,2));

  //System.out.println(nullSafeEquals(1,"string"));

  //System.out.println(nullSafeEquals(true,true));


    }
}

Output:

false

  
This was an example of how to use a null safe equals method to compare objects in Java.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button