I have the following HashMap with properties keys and values:
private HashMap<String, Object> prop_values;
I need to check if one instance of it is equal to another one. In the past, i just did this:
if (prop_values_1.equals(prop_values_2)){
// do something
}
And this worked until i got Object[] as a value. So, my previous expression always returned false on such HashMap with any Object[] value.
So, i have to implement this method:
private boolean isPropValuesEquals(HashMap<String, Object> pv1, HashMap<String, Object> pv2){
boolean isEquals = true;
if (pv1 == null || pv2 == null){
return false;
}
if (!pv1.keySet().equals(pv2.keySet())){
return false;
}
for (String key : pv1.keySet()){
Object cur_pv1 = pv1.get(key);
Object cur_pv2 = pv2.get(key);
if (cur_pv1 instanceof Object[]){
if (cur_pv2 instanceof Object[]){
isEquals = Arrays.equals((Object[])cur_pv1, (Object[])cur_pv2);
} else {
return false;
}
} else {
isEquals = isEquals && cur_pv1.equals(cur_pv2);
}
if (!isEquals){
return false;
}
}
return isEquals;
}
It works, but it seems to be some kind of hack, and i'm not sure this is the best way to achieve what I need.
So, here's two questions:
why Object[].equals() is not the same as Arrays.equals()? It seems to be painful.
is there some better way to compare
HashMap<String, Object>, if values can be anObject[]?
Objectto parametrized type likeList<Object>.