I'm trying to compare two objects and both have the exact same values on each field.
System.out.println(basicTypes1);
[id=2013756475,
boolean2={true},
byte2={32},
short2={1,2},
int2={10,11},
float2={0.1,0.2},
double2={0.01,0.02},
number2={10,11},
char2={c,t},
string2={7c2cfc4e-5805-45ad-8687-4f2412a92e1d,d91b9609-39fb-4fe9-9f36-db33608970d1},
date2={Tue Jun 17 11:48:56 BST 2014,Tue Jun 17 11:48:56 BST 2014},
obj2={443f38f4-d5b4-41fb-aeeb-d086f4edc62a,d855d031-ad8d-4fce-b5e1-d959034a0b7c}]
System.out.println(basicTypes2);
[id=2013756475,
boolean2={true},
byte2={32},
short2={1,2},
int2={10,11},
float2={0.1,0.2},
double2={0.01,0.02},
number2={10,11},
char2={c,t},
string2={7c2cfc4e-5805-45ad-8687-4f2412a92e1d,d91b9609-39fb-4fe9-9f36-db33608970d1},
date2={Tue Jun 17 11:48:56 BST 2014,Tue Jun 17 11:48:56 BST 2014},
obj2={443f38f4-d5b4-41fb-aeeb-d086f4edc62a,d855d031-ad8d-4fce-b5e1-d959034a0b7c}]
Note that they both print the same result.
However, when I'm comparing the fields in each object the number2
and the string2
using .equals, the return value is false.
Here is a sample of the method where the values are being compared:
public boolean isEqual(Object basicTypes1, Object basicTypes2) {
boolean objectResult = true, fieldResult;
Class<?> leftClass = basicTypes1.getClass();
Class<?> rightClass = basicTypes2.getClass();
List<Field> fields = getAllFields(leftClass);
for (Field field : fields) {
field.setAccessible(true);
//the fields number2 and string2 enter in this if so I won't post the rest of them
if (TypeVerifier.isBasicType(field.getType())) { // handle primitives
fieldResult = leftFieldValue.equals(rightFieldValue); //comparing the above values, returns false, why?
if (!fieldResult) {
logger.error(getFieldError(leftClass, field, leftFieldValue, rightFieldValue));
}
}
objectResult = objectResult && fieldResult;
}
return obejctResult;
}
Is there any other way I should compare the Number[] number2
and the String[] string2
?
All the other fields return true in the same equals method (as expected).
number2={10,11} and string2={7c2cfc4e-5805-45ad-8687-4f2412a92e1d,d91b9609-39fb-4fe9-9f36-db33608970d1}
Thanks