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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 7 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 '14 at 7:14
@sfdcfox Its one answer on this site I remember because it is so elegant. – Keith C Jun 15 '14 at 10:19

Your Answer


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.