Edit:
This is now a function you can use. It was implemented in the Spring 18 release.
I don't know of an official reason "why," but there's an idea you can vote on. I'll be sure to ask about it when I get a chance...
As I posted in that idea a few years ago, there's a quick one-liner you can use that's (relatively) faster than a straight loop, modified to be polymorphic:
Boolean listContains(Object[] source, Object target) {
return (new Set<Object>(source)).contains(target);
}
Example uses in executeAnonymous:
Account a = new Account(Name='Test');
Account[] aList = new Account[] { a };
System.assert(listContains(aList, a));
Integer[] values = new Integer[] { 1, 1, 2, 3, 5 };
System.assert(listContains(values, 3));
System.assert(!listContains(values, 7));
Answer from sfdcfox on Stack Exchangeapex - List.contains() method in Salesforce - Salesforce Stack Exchange
apex - List class methods dynamically - Salesforce Stack Exchange
apex method for counting
How to use an Apex-Defined Data Type to pass an HTTP response to a Flow?
Videos
Edit:
This is now a function you can use. It was implemented in the Spring 18 release.
I don't know of an official reason "why," but there's an idea you can vote on. I'll be sure to ask about it when I get a chance...
As I posted in that idea a few years ago, there's a quick one-liner you can use that's (relatively) faster than a straight loop, modified to be polymorphic:
Boolean listContains(Object[] source, Object target) {
return (new Set<Object>(source)).contains(target);
}
Example uses in executeAnonymous:
Account a = new Account(Name='Test');
Account[] aList = new Account[] { a };
System.assert(listContains(aList, a));
Integer[] values = new Integer[] { 1, 1, 2, 3, 5 };
System.assert(listContains(values, 3));
System.assert(!listContains(values, 7));
You can now use List.contains() with Spring 18