The Most Awaited Feature “List Contains” is Now Available – Spring’18

Yes, you heard it right! Now no more replicating List to Set. You can directly use contains over List.

Salesforce Spring ’18 Release

contains(listElement):

Same as a set, it will return true if a list contains an element.

List<String> listStrings = new List<String>();
listStrings.add(“One”);
listStrings.add(“Two”);
listStrings.add(“Three”);
listStrings.add(“Four”);
System.debug('Check Contains Four-->'+ listStrings.contains('Four'));

Output:

Check Contains Four-->True

indexOf(listElement):

You can consider as an extra feature. It returns the position of the element and if the element is no there it returns -1.

List<String> listStrings = new List<String>();
listStrings.add(“One”);
listStrings.add(“Two”);
listStrings.add(“Three”);
listStrings.add(“Four”);
System.debug(‘Index of Ten–>’+ listStrings.indexOf(‘Ten’));

Output:

Index of Ten–>-1

Keep Smiling!!!!

Happy Coding 🙂

Reference: Apex: New and Changed Classes

Related Articles

Responses