You need to sign in to do that
Don't have an account?
Anshuma Yadav
How to compare two map values?
I need to compare the values of these maps.
Map<String, Object> fieldsToValueNew = lw.newValues.getPopulatedFieldsAsMap();
for (String fieldName : fieldsToValueNew.keySet()){
System.debug('field name is ' + fieldName + ', new value is ' +
fieldsToValueNew.get(fieldName));
}
Map<String, Object> fieldsToValueOld = lw.oldValues.getPopulatedFieldsAsMap();
for (String fieldName : fieldsToValueOld.keySet()){
System.debug('field name is ' + fieldName + ', old value is ' +
fieldsToValueOld.get(fieldName));
}
Map<String, Object> fieldsToValueNew = lw.newValues.getPopulatedFieldsAsMap();
for (String fieldName : fieldsToValueNew.keySet()){
System.debug('field name is ' + fieldName + ', new value is ' +
fieldsToValueNew.get(fieldName));
}
Map<String, Object> fieldsToValueOld = lw.oldValues.getPopulatedFieldsAsMap();
for (String fieldName : fieldsToValueOld.keySet()){
System.debug('field name is ' + fieldName + ', old value is ' +
fieldsToValueOld.get(fieldName));
}
The correct way to compare maps for value-equality is to:
- Check that the maps are the same size(!)
- Get the set of keys from one map
- For each key from that set you retrieved, check that the value retrieved from each map for that key is the same (if the key is absent from one map, that's a total failure of equality)
Reference :- https://stackoverflow.com/questions/2674021/how-to-compare-two-maps-by-their-valuesKindly mark it as the best answer if it works for you.
Thanks & Regards,
Priya Ranjan
Could you please share another example?