I have a map I want to loop through and remove all duplicate items. Is there a method that can be used like my pseudo code has?

public Map<String, User[]> allPeople {
    Map<String, User[]> teams = new Map<String, User[]>();
    ....
    return teams;
}

public plcontroller(){
    for(User[] loopOverAllPeople : allPeople.values()){
    system.debug('*current person: ' + loopOverAllPeople);
    ****pseudo code****
        if(allPeople.get(loopOverAllPeople.Name()) > 1) {
            loopOverAllPeople.Name.remove();
        }
    }
}

This is what the system debug looks like:

Administration=(User:{Name=Joe Smith, Title=CEO, Department=Administration, UserRoleId=1111, Leadership__c=true, Id=123}, User:{Name=Ally Scott, Title=Receptionist, Department=Administration, ManagerId=987xxxasf, UserRoleId=222, Leadership__c=false, Id=543543}, User:{Name=Billy Bob, Title=Receptionist, Department=Administration, ManagerId=0054354AS, UserRoleId=333, Leadership__c=false, Id=G}, User:{Name=Joe Smith, Title=CEO, Department=Administration, UserRoleId=1111, Leadership__c=true, Id=123}, User:{Name=Julia Emez, Title=Receptionist, Department=Administration, Extension=789, Phone=896-899-8745, ManagerId=7894651312, UserRoleId=444, Leadership__c=false, Id=675444566}),

Analytics=(User:{Name=Tommy Boy, Title=Director of Analytics, Department=Analytics, UserRoleId=77888, Leadership__c=true, Id=6543654}, User:{Name=Amy Smith, Title=Data Scientist, Department=Analytics, Extension=55, Phone=508-998-8855, ManagerId=45534654654, UserRoleId=22222, Leadership__c=false, Id=545345421}, User:{Name=Tommy Boy, Title=Director of Analytics, Department=Analytics, UserRoleId=77888, Leadership__c=true, Id=6543654}),

etc...

share|improve this question
    
what dont you create Set<> at the first place? – Santanu Boral 11 hours ago
    
@SantanuBoral because I need to map a department name to a list of users – Olivia 11 hours ago
    
so finally you want to create list of unique users? – Santanu Boral 11 hours ago
    
So you have a collection of users and you want to remove any with duplicate names? You could do that when building the first map using Map<string, Map<string, User>> where the inner string is the user name. – Daniel Ballinger 11 hours ago
    
@SantanuBoral yes, I want the allPeople User[] list to be editable/ unique. – Olivia 11 hours ago

So, this snippet should do it. (I am being more verbose than I need to, for clarity's sake)

List<User> usersWithDupes = <your list here>

Set<User> usersUnique = new Set<User>();
usersUnique.addAll(usersWithDupes);

List<User> usersUniqueList = new List<User>();
usersUniqueList.addAll(usersUnique);
share|improve this answer
    
I thought the same Sebastian.. but how can u validate uniqueness in set will be based on name ? y dont the uniqueness be on id or some other key ? – pmvsdt 11 hours ago
    
According to this: developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…, you don't need the ID. – Sebastian Kessel 11 hours ago
    
lets say List<User> has 3 columns name, company name, role... how can u make sure when u add the list to set, only users with unique names will be in the set.. y not role or nay other field ? – pmvsdt 11 hours ago
1  
It's narrow, but based on what the OP wants, I think it's appropriate. I wouldn't use it unless very specific conditions were met... and I think the OP's code indicates that they do. If you disagree, Adrian, it would be helpful for me if you justify your comments. I'd love to hear your rationale, it's not unheard of for me to overlook a detail. – Sebastian Kessel 10 hours ago
1  
@SebastianKessel it worked, my duplicates are removed but as are the departments. I am going to try and save it back into the map so I can connect the department to the list of users. – Olivia 10 hours ago

So you have a collection of users and you want to remove any with duplicate names? You could do that when building the first map using Map<string, Map<string, User>> where the inner string is the user name.

E.g.

public Map<String, User[]> allPeople {
    Map<String, Map<string, User>> departmentMap = new Map<String, Map<string, User>>();

    for(User u : [Select Id, Name, Department from User]) {
        Map<string, User> userNamesMap = null;
        if(departmentMap.containsKey(u.Department)) { 
            userNamesMap = departmentMap.get(u.Department);
        } else {
            userNamesMap = new Map<string, User>();
            departmentMap.put(u.Department, userNamesMap);
        } 
        // You might want to decide how to handle Name conflicts here 
        userNamesMap.put(u.Name, u);
    } 

    return departmentMap;
}

The outer mapped will be keyed by the Users department. The inner map will be keyed by the user name. As such, you can only have one User per department with each name. It would be possible to have the same User Name in different departments. If that is a problem then you would need to maintain a SET of processed user names that you check before adding the User to the map.

share|improve this answer
2  
You could also use complex keys here. – Adrian Larson 9 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.