function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nitish 73Nitish 73 

Comparing 2 lists

Hi Everyone, 

I have 2 lists of type String and I want to compare both of them and see if the elements inside them match and return something. 
Here is the code snippet. 
 
List<Required_Documents__c> Docslist = [SELECT ID, Decision_Matrix__c, Required_Docs__c FROM Required_Documents__c WHERE Decision_Matrix__c IN: DMList];
    
    
    List<String> dmside = new List<String>();
    for(Required_Documents__c req : Docslist){
        dmside.add(req.Required_Docs__c);
    }

//output of dmside  ----- Health Certificate; Medical History Certificate ; Treatment Certificate

 List<Applicant_Attachments__c> Lineattachments = [SELECT ID, Document_Types__c FROM Applicant_Attachments__c WHERE AC__c=:LineList[0].Id];
    List<String> lineside = new List<String>();
    for(Applicant_Attachments__c a: Lineattachments){
        lineside.add(a.Document_Types__c);
    }

//output of lineside ----- Health Certificate; Treatment Certificate; Medical History Certificate

The order doesnt matter, all I need to check is 'Do the elements in lineside list match with elements in dmside' . How can I compare them and achieve this. 
Any help is appreciated. 

Thanks
 
Best Answer chosen by Nitish 73
Mahesh DMahesh D
Hi Nitish,
You can use containsAll() method in the Set to compare, doc,
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_set.htm

Regards,
Mahesh

All Answers

Mahesh DMahesh D
Hi Nitish,
You can use containsAll() method in the Set to compare, doc,
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_set.htm

Regards,
Mahesh
This was selected as the best answer
Naval Sharma4Naval Sharma4
Hi Nitish,

If you think that your list doesn't care about unique valuse then you can go with Set. Multiple occurrance of a value like 'Health Certificate' will be treates as a single element in Set.

You can try below code.

 
List<Required_Documents__c> Docslist = [SELECT ID, Decision_Matrix__c, Required_Docs__c FROM Required_Documents__c WHERE Decision_Matrix__c IN: DMList];
    
    
    Set<String> dmside = new Set<String>();
    for(Required_Documents__c req : Docslist){
        dmside.add(req.Required_Docs__c);
    }

//output of dmside  ----- Health Certificate; Medical History Certificate ; Treatment Certificate

 List<Applicant_Attachments__c> Lineattachments = [SELECT ID, Document_Types__c FROM Applicant_Attachments__c WHERE AC__c=:LineList[0].Id];
    Set<String> lineside = new Set<String>();
    for(Applicant_Attachments__c a: Lineattachments){
        lineside.add(a.Document_Types__c);
    }

//output of lineside ----- Health Certificate; Treatment Certificate; Medical History Certificate

// Return result if both sets have the same values
return lineside.containsAll(dmside);   // Returns true/false

I hope this will work for you.

Thanks & Regards,
Naval
 
Nitish 73Nitish 73
Thanks Mahes and Naval. 

It worked. Much appreciated :)