+ Start a Discussion

Compare two lists

Hi Guys, 

We have 2 lists one is a List of type string and other of type object. My requirement is to check if all values in string list exists in object list. If no then false. 

List<string> A = new List<string>();
List<Contact> Contactlist = new List<Contact>();

Requirement: need to check if All values in the list A should exist in Contactlist if no then false. 


Nayana KNayana K
You want to compare list A with specific field value list of contact? Suppose A is having list of Names and you want to compare it with list of names of Contact?
Amit Chaudhary 8Amit Chaudhary 8
Please check below post to check all list method
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_list.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_lists.htm

NOTE :- As your your code you are trying to compare list of string with list if contact . In that which field you want to compare ?

IF both list are same then you can try below method if list
Compares this list with the specified list and returns true if both lists are equal; otherwise, returns false.


Let us know if this will help you

Amit Chaudhary
JyothsnaJyothsna (Salesforce Developers) 
Hi Prasanna,

Make them into the same type, and then use containsAll() method in the Set to compare.Please check the below doc.

Best Regards,
Ayiravalli ArasuAyiravalli Arasu
Hi Prasanna,
Pls tell which field from contact you want to check with the string list?
Hi All, 

Thanks for replies. 
Contact was an example but it is actually a custom object
List <custom_obj> ListB = new List<custom_obj>();
I only want to check if all values in the listA should exist in ListB not necessarily equal 
ListA may contain (red, blue, black)
ListB may contain (red, black, blue, yellow, green)
Requirement: need to check if ListA has all values in ListB then true else false in the above example it should be true. 
Say if ListB has (red, black) then it should be false.

Hi Jyothsna, 

By any chance can you please give me an example code?