Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

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 equals and 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.

share|improve this answer
    
Thanks for the honorable mention! –  sfdcfox Jun 15 at 7:14
1  
@sfdcfox Its one answer on this site I remember because it is so elegant. –  Keith C Jun 15 at 10:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.