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 astatic
methodboolean nullSafeEquals(Object o1, Object o2)
that determines if two objects are equal. If the two objects are equal it returnstrue
, if one of them isnull
it returnsfalse
. Then it checks if the two obejcts are instances of any kind of array, eg.Object[]
,boolean[]
etc. Then it usesequals()
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 overridesequals(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 ofA
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 classNullSafeEquals
that extendsObjectUtils
,
as described in the code snippet below.
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | 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.