3

Is it possible to Compare two sets namely set A and set B while both contains set of ids.

I need to check if set A contains ids which is present in set B.

If it is present then i need to remove from set B.

Example:

set<id> SetA= new set<id>{'00524000001IFbNAAW','00524000001IFbVAAW','00524000001IFrHAAW','00524000001IFxDAAW','00524000001IFrEAAW','00524000001IFrGAAW'};

set<id> setB= new set<id>{'00524000001IFbNAAW','00524000001IFbVAAW','00524000001IFrHAAW','00524000001IFxDAAW','00524000001IFrEAAW','00524000001IFrGAAW','00524000001IFmPAAW','00524000001IFnKAAW','00524000001IFgPAAW'};

Thanks !!!

2 Answers 2

1

You can try like this way

Set<Id> setAIds = new set<Id>{'0039000001pJpRH', '0069000000K8YjG'};
Set<Id> setBIds = new set<Id>{'0039000001pJpRH'};

system.debug('====setAIds==='+setAIds);
system.debug('====setBIds==='+setBIds);

for(Id objId: setAIds){
    if(setBIds.contains(objId))
        setBIds.remove(objId);
}
system.debug('====setAIds==='+setAIds);
system.debug('====setBIds==='+setBIds);
7

You can use "containsAll" and "removeAll" method of set. list:

Boolean containsAll = satb.containsAll(setA);

This "ContainsAll" method will store true if all the elements are present in setB.

Check this article

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_set.htm

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .