6

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?

1 Answer 1

7

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.

1
  • 1
    @sfdcfox Its one answer on this site I remember because it is so elegant.
    – Keith C
    Commented Jun 15, 2014 at 10:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .