+ Start a Discussion
chaitanya salesforcecrmchaitanya salesforcecrm 

Removing a List of values from set in apex?

I have two lists(mainGroupLinkerIPGlist and toCopy) i am trying to remove all the values of mainGroupLinkerIPGlist from toCopy by creating a set and adding both the lists to the IPGset. below is the apex code.
 
List<Intralinks_Group_and_Contact_Links__c> mainGroupLinkerIPGlist = [SELECT Id,Intralinks_Portal_Group__c FROM Intralinks_Group_and_Contact_Links__c where Contact__c = :mainGroupLinker.Contact__c];
        //system.debug('mainGroupLinkerIPGlist =' + mainGroupLinkerIPGlist.size());

    //query all the Groups the copyFrom contact has
    if(copyFrom.Contact__c != null) {
       List<Intralinks_Group_and_Contact_Links__c> toCopy = [SELECT Id,Intralinks_Portal_Group__c FROM Intralinks_Group_and_Contact_Links__c where Contact__c = :copyFrom.Contact__c];

         system.debug('toCopy =' + toCopy.size());

         set<Intralinks_Group_and_Contact_Links__c> IPGset = New set<Intralinks_Group_and_Contact_Links__c>(toCopy);

         boolean result = IPGset.removeall(mainGroupLinkerIPGlist);
         System.assertEquals(true, result);
         system.debug('mainGroupLinkerIPGlist Count=' + mainGroupLinkerIPGlist.size());
         system.debug('IPGset count=' + IPGset.size());

when i am trying to remove all the mainGroupLinkerIPGlist values from set it is not removing and i am getting below message. System.AssertException: Assertion Failed: Expected: true, Actual: false
Best Answer chosen by chaitanya salesforcecrm
Tavva Sai KrishnaTavva Sai Krishna
Hi Chaitanya,

try below code. 
list<Integer> toCopy  = new list<Integer>{1,2,3,4,5};
list<Integer> mainGroupLinkerIPGlist   = new list<Integer>{1,2};
for(Integer i: mainGroupLinkerIPGlist  ){

  for(integer k = 0; k<toCopy .size()-1; k++){
             if(i == toCopy [k] ){
                 toCopy .remove(k);
}
}

}

Let me know if you have any queiries. Also mark it as best answer if it solves your questions.

Thanks and Regards,
Sai Krishna Tavva.

All Answers

Tavva Sai KrishnaTavva Sai Krishna
Hi chaitanya,

I have modified your code such that to achieve the same logic. try with below:
//query all the Groups the copyFrom contact has
    if(copyFrom.Contact__c != null) {
       List<Intralinks_Group_and_Contact_Links__c> toCopy = [SELECT Id,Intralinks_Portal_Group__c FROM Intralinks_Group_and_Contact_Links__c where Contact__c = :copyFrom.Contact__c OR Contact__c = :mainGroupLinker.Contact__c];

         system.debug('toCopy =' + toCopy.size());

Let me know if you face any issues.

Thanks and Regards,
Sai Krishna Tavva.
mritzimritzi
The Salesforce documentation says that the function call "removeAll" return true ony if the original set changed as a result of the call.

It means that in case mainGroupLinkerIPGlist & toCopy lists have no common elements, then this function call will return false.

As a workaround, you can store size of IPGset before and after the function call and compare sizes to ascertain that values have been removed from IPGset.

Mark this as Best Answer, if this helps solve your problem.
chaitanya salesforcecrmchaitanya salesforcecrm
HI Tavva Sai Krishna,

Your query is adding the mainGroupLinkerIPGlist  values again to toCopy List. i want to remove the mainGroupLinkerIPGlist  values from toCopy List. for ex : if mainGroupLinkerIPGlist  has {1,2} values and toCopy List has {1,2,3,4,5}. i want to remove {1,2} and keep only {3,4,5} in toCopy List.
Tavva Sai KrishnaTavva Sai Krishna
Hi Chaitanya,

try below code. 
list<Integer> toCopy  = new list<Integer>{1,2,3,4,5};
list<Integer> mainGroupLinkerIPGlist   = new list<Integer>{1,2};
for(Integer i: mainGroupLinkerIPGlist  ){

  for(integer k = 0; k<toCopy .size()-1; k++){
             if(i == toCopy [k] ){
                 toCopy .remove(k);
}
}

}

Let me know if you have any queiries. Also mark it as best answer if it solves your questions.

Thanks and Regards,
Sai Krishna Tavva.
This was selected as the best answer
Tavva Sai KrishnaTavva Sai Krishna
Hi chaitanya,

If you the answer the for this , please mark it as a best answer as it may helpful to others.

Thanks and Regards,
Sai Krishna Tavva.