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
gene.koopmangene.koopman 

Set<object> .removeAll(set<element>) method, what is the criteria for matching?

I'm building a list of users, and optionally, querying for an alternative list of users, and then removing the users from list #2, from list #1. I'm using Set<User>, so I should be able to use removeAll(setOfUsers) to accomplish this, but it doesn't seem to work. I know both lists contain at least 1 common user, but after this method executes nothing is changed. What is the criteria for matching? Can I influence it somehow? I would prefer not to have to loop through both sets of returns multiple times to find any matches.

 

Also, before anyone asks: No, I cannot simply include the optional filtering criteria in the initial query. I would love to do this, as it would be simpler, but the complexity of both queries precludes this option.

 

Here's the code:

 

        //iterate over the matches and load the users                
        foundSet = new Set<User>();
        for(sObject s : Database.query(qry))
                foundSet.add(new User(ID        = ((User_Info__c)s).User__c, 
                                        FirstName = ((User_Info__c)s).User__r.FirstName, 
                                        LastName  = ((User_Info__c)s).User__r.LastName,
                                        City      = ((User_Info__c)s).User__r.City,
                                        State     = ((User_Info__c)s).User__r.State));
        
        //exclude those users with existing events
        if(foundSet.size() > 0 && includeConflictingUsers.toLowerCase() == 'false')
            foundSet.removeAll(buildUsersWithConflictList(new List<User>(foundSet)));

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Ritesh AswaneyRitesh Aswaney

I'm guessing Set de-duplication will take place based on all the attributes of the User elements - if all attributes of two Users have identical attributes, then that would probably constitute a match.

 

You could perhaps try a map. 

Map<Id, User> userIdMap = new Map<Id, User>([Select Id, ....]);

 

Have your buildUsersWithConflictList return a map or a list of Ids and eliminiate the Users by Id.

 

userIdmap.remove(key);

 

 

All Answers

Ritesh AswaneyRitesh Aswaney

I'm guessing Set de-duplication will take place based on all the attributes of the User elements - if all attributes of two Users have identical attributes, then that would probably constitute a match.

 

You could perhaps try a map. 

Map<Id, User> userIdMap = new Map<Id, User>([Select Id, ....]);

 

Have your buildUsersWithConflictList return a map or a list of Ids and eliminiate the Users by Id.

 

userIdmap.remove(key);

 

 

This was selected as the best answer
gene.koopmangene.koopman

This seems like a simple and effective(therefore brilliant) solution. I'll give it a try in the morning and let you know.

Thanks,

Gene

gene.koopmangene.koopman

Thanks again for the suggestion, it worked perfectly.

 

That being said, this is something SF should fix. We should be able to specify the comparison like you do in .NET.

 

Thanks again Ritesh.