Apex List Contains Code

Apex List Contains

The Idea, Contains/IndexOf Method for Lists, has been delivered in the Spring ’18 release. There are two new List methods: contains(listElement), and indexOf(listElement). The methods take a List element as an argument and return whether the List contains the element or the index in the List, respectively. This post looks at some interesting aspects of the methods.

Prior State

Prior to these two methods, one common way to determine whether a List contained an element was to convert the List to a Set, either by iteration or using the Set<T>(listToCopy) constructor, and then invoking the Set contains method. Another common way was to just iterate over the List until the element was found. This led to a lot of duplicated code in the worst case, or reusable methods in utility classes in the best case.

Primitives

The methods generally work as expected for primitives, which is that if the listElement argument equals an element on the List, that element is found. There are two primitive types worth exploring more.

String

When the List contains a String the comparison is case sensitive. Even though two Strings are ==, they are not equals. That is because the == operator is case insensitive and the equals method on the String class is case sensitive and different cased Strings have different hashCodes. This is consistent with the behavior of Maps and Sets that have Strings as keys or elements.

Decimal

When the List contains a Decimal the scale is considered in determining whether or not the List contains the element. Two Decimals are == if they are the same, without scale considered. Also, although there is no hashCode method on Decimal, if you cast to Object you can call hashCode and see that even though the Decimals are ==, because they have different scales they do have different hashCodes.

SObjects

A List contains an SObject when the SObject on the List has the same field values as the listElement argument. The SObjects may refer to the same record in the Database, but if the fields are different they are not the same List element.

However, care must be taken because some fields like Id and RecordTypeId are implicitly returned in SOQL queries, so two queries that appear different actually return records that are ==.

Custom Types

Custom Types work with the contains and indexOf methods as long as they are programmed to do so. The equals and hashCode methods should be overridden for the Custom Types to work. There are standard recipes for hashCode and equals that you can find online and books like Effective Java have good examples with explanations.

More

This post has scratched the surface about how the new contains and indexOf methods on List. You can mostly reason about how these work, if you understand underlying Apex language constructs (operators, types, casting, etc.). Consult the Apex Developer Developer Guide for much more detailed definitions, explanations, and examples.

 

 

2 thoughts on “Apex List Contains

Leave a Reply

Your email address will not be published. Required fields are marked *