Is there any way to check if a list contains a certain element? I looked at the List functions and did not see any contain() function like Java or C# , so I was wondering how other people are handling this.
I really need to use a List I cant use a Map like in this example here
What I have now is really bad..
for (String s : allContacts)
{
for(String ic:insertedContacts)
{
if (s != ic )
{
errorContacts.add(s);
break;
}
break;
}
}
mySet. addAll(myList);. You can now use the Set. contains() method to check the set for the element you're looking for.
1) contains(listElement) Returns true if the list contains the specified element. 2) indexOf(listElement) Returns the index of the first occurrence of the specified element in this list. If this list does not contain the element, returns -1.
NOT contains( 'string' ) ? The contains method returns a boolean, so you can use boolean operators on the result. You may need to check for null conditions on object / Field__c as well. You may also want to use containsIgnoreCase instead of "contains" if you want a case-insensitive check.
A Set might be what you're looking for.
Set<String> mySet = new Set<String>();
Set.addAll()
method to add all of the List elements to the set. mySet.addAll(myList);
.Set.contains()
method to check the Set for the element you're looking for.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With