I went through the Salesforce documentation for Using Custom Types in Map Keys and Sets . The example in the documentation is not clear for me.
Can someone provide use cases of using apex class as map keys and sets?
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.Sign up to join this community
One case is this sfdcfox answer to a question about how to catch duplicate records. In that case equality is defined as a record that has the same first name, last name and email address. Rather than having to compare those three fields for every entry in an existing list, the code can just see if the set of existing keys contains the new key using one line of code (which is also fast because of the use of hashing).
Another case is code doing calculations on date range objects. A date range typically holds a start date and an end date and has methods to calculate things like the intersection between two date ranges. Sets and maps are typically used to hold collections of these. Because Apex now supports the
hashCode methods, it is simple to arrange that date range instances with the same start and end dates are interchangeable as map keys or set values.