function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anshuma YadavAnshuma 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));
            }
PriyaPriya (Salesforce Developers) 
Hi Anshuma,

The correct way to compare maps for value-equality is to:
  1. Check that the maps are the same size(!)
  2. Get the set of keys from one map
  3. 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-values
 

Kindly mark it as the best answer if it works for you.

 

Thanks & Regards,

Priya Ranjan



 
Anshuma YadavAnshuma Yadav
Thanks Priya !!

Could you please share another example?