Newer Version Available
Using Custom Types in Map Keys and Sets
For maps, instances of your Apex classes can be added either as keys or values. If you add them as keys, there are some special rules that your class must implement for the map to function correctly; that is, for the key to fetch the right value. Similarly, if set elements are instances of your custom class, your class must follow those same rules.
When using a custom type (your Apex class) for the map key or set elements, provide equals and hashCode methods in your class. Apex uses these two methods to determine equality and uniqueness of keys for your objects.
Adding equals and hashCode Methods to Your Class
To ensure that map keys of your custom type are compared correctly and their uniqueness can be determined consistently, provide an implementation of the following two methods in your class:
- The equals method with this
signature:
1public Boolean equals(Object obj) { 2 // Your implementation 3} - The hashCode method with this
signature:
1public Integer hashCode() { 2 // Your implementation 3}
Sample
This sample shows how to implement the equals and hashCode methods. The class that provides those methods is listed first. It also contains a constructor that takes two Integers. The second example is a code snippet that creates three objects of the class, two of which have the same values. Next, map entries are added using the pair objects as keys. The sample verifies that the map has only two entries since the entry that was added last has the same key as the first entry, and hence, overwrote it. The sample then uses the == operator, which works as expected because the class implements equals. Also, some additional map operations are performed, like checking whether the map contains certain keys, and writing all keys and values to the debug log. Finally, the sample creates a set and adds the same objects to it. It verifies that the set size is two, since only two objects out of the three are unique.
1public class PairNumbers {
2 Integer x,y;
3
4 public PairNumbers(Integer a, Integer b) {
5 x=a;
6 y=b;
7 }
8
9 public Boolean equals(Object obj) {
10 if (obj instanceof PairNumbers) {
11 PairNumbers p = (PairNumbers)obj;
12 return ((x==p.x) && (y==p.y));
13 }
14 return false;
15 }
16
17 public Integer hashCode() {
18 return (31 * x) ^ y;
19 }
20}This code snippet makes use of the PairNumbers class.
1Map<PairNumbers, String> m = new Map<PairNumbers, String>();
2PairNumbers p1 = new PairNumbers(1,2);
3PairNumbers p2 = new PairNumbers(3,4);
4// Duplicate key
5PairNumbers p3 = new PairNumbers(1,2);
6m.put(p1, 'first');
7m.put(p2, 'second');
8m.put(p3, 'third');
9
10// Map size is 2 because the entry with
11// the duplicate key overwrote the first entry.
12System.assertEquals(2, m.size());
13
14// Use the == operator
15if (p1 == p3) {
16 System.debug('p1 and p3 are equal.');
17}
18
19// Perform some other operations
20System.assertEquals(true, m.containsKey(p1));
21System.assertEquals(true, m.containsKey(p2));
22System.assertEquals(false, m.containsKey(new PairNumbers(5,6)));
23
24for(PairNumbers pn : m.keySet()) {
25 System.debug('Key: ' + pn);
26}
27
28List<String> mValues = m.values();
29System.debug('m.values: ' + mValues);
30
31// Create a set
32Set<PairNumbers> s1 = new Set<PairNumbers>();
33s1.add(p1);
34s1.add(p2);
35s1.add(p3);
36
37// Verify that we have only two elements
38// since the p3 is equal to p1.
39System.assertEquals(2, s1.size());