1

I have a list of Users, each with a list of their Groups. Something like this:

John -->[GroupA]

Peter --> [GroupB, GroupC]

Bob --> [GroupC]

Tom --> [GroupA,GroupB]

Jack --> []

Then I have my own list of groups:

Me -->[GroupA, GroupB, GroupC].

So what I want is to put the users in a "bucket" that matches their groups with mine. If the user has no groups, their will be added to a Generic group, in the list. So the result has to be something like:

Contacts:

{ GroupA:[John, Tom], GroupB:[Peter, Tom], GroupC:[Peter, Bob], Generic:[Jack]}

So I test something like this, but, It generates duplicated values on the LinkedHashMap, and I don't know how to solve it.

    private void createVoidCollection() {
            groupsCollections = new LinkedHashMap<String, List<RosterEntry>>();
            ArrayList<RosterEntry> contacts = new ArrayList<RosterEntry>();
            for (String group : groupList) {
                groupsCollections.put(group, contacts);

            }
        }

    private void createContactListCollection() {
            createVoidCollection();

            boolean bAdded = false;

            //Returns a Collection of USers/contacts
            Collection<RosterEntry> contacts = mRoster.getEntries();

            for (RosterEntry buddy : contacts) {
                //We get the groups that pertains every contact/user
                Collection<RosterGroup> buddyGroups = buddy.getGroups();
                List<RosterEntry> contactsAux = new ArrayList<RosterEntry>();

                for (RosterGroup group : buddyGroups) {     
                    //For all the groups of the user, we check if is in some of my groups. If not they will be added to a generic group.        
                    if (groupList.contains(group.getName())) {
                        contactsAux = groupsCollections.get(group.getName());
                        contactsAux.add(buddy);
                        groupsCollections.put(group.getName(), contactsAux);
                        bAdded = true;
                    }
                }
                if (!bAdded) {

                    //The generic group is checked if exist or not, to be created if neeeded.
                    if (groupsCollections.containsKey(mBuddGroup)) {
                        contactsAux = groupsCollections.get(mBuddGroup);
                        contactsAux.add(buddy);
                        groupsCollections.put(mBuddGroup, contactsAux);
                    } else {
                        contactsAux.add(buddy);
                        groupsCollections.put(mBuddGroup, contactsAux);
                        groupList.add(mBuddGroup);
                    }
                }
                bAdded = false;

            }

        }

Question:

How can I solve it? Is there any way to do it better?

My groups list:

DEV-andorraCASS2007
D40-E30-Kosmos

The Contacts and Group list of each one is:

User:
    pruebaopenfire
        Groups: 
            D40-E30-Kosmos
User:
    Diana P
        Groups: 
            DEV-andorraCASS2007
            D40-E30-Kosmos
User:
    Fabio C
        Groups: 
            D40-E30-Kosmos
User:
    Alejandro 
        Groups: 
            DEV-andorraCASS2007
User:
    Jordi C
        Groups: 
            DEV-andorraCASS2007
            D40-E30-Kosmos
User:
    Mikel S
        Groups: 
            D40-E30-Kosmos
User:
    AAAAA
        Groups: 
User:
    Rubén R
        Groups: 
            DEV-andorraCASS2007
            D40-E30-Kosmos
User:
    Diego M
        Groups: 
            D40-E30-Kosmos
User:
    jfkgl
        Groups: 
User:
    Luis T
        Groups: 
            DEV-andorraCASS2007
            D40-E30-Kosmos
User:
    Melissa Y
        Groups: 
            D40-E30-Kosmos
User:
    Prova Prova
        Groups: 
            D40-E30-Kosmos

The result is:

02-16 13:22:13.436: D/TESTINGGROUPS(6056): 
{
    DEV-andorraCASS2007=[Diana P: [email protected] [DEV-andorraCASS2007, D40-E30-Kosmos], Diana P: [email protected] [DEV-andorraCASS2007, D40-E30-Kosmos], Alejandro Q: [email protected] [DEV-andorraCASS2007], Mikel S: [email protected] [D40-E30-Kosmos], Rubén R: [email protected] [DEV-andorraCASS2007, D40-E30-Kosmos], Rubén R: [email protected] [DEV-andorraCASS2007, D40-E30-Kosmos], Diego M: [email protected] [D40-E30-Kosmos], Jordi C: [email protected] [DEV-andorraCASS2007, D40-E30-Kosmos], Jordi C: [email protected] [DEV-andorraCASS2007, D40-E30-Kosmos], Luis T: [email protected] [DEV-andorraCASS2007, D40-E30-Kosmos], Luis T: [email protected] [DEV-andorraCASS2007, D40-E30-Kosmos], Prova Prova: [email protected] [D40-E30-Kosmos], pruebaopenfire: [email protected] [D40-E30-Kosmos], Melissa Y: [email protected] [D40-E30-Kosmos], Fabio C: [email protected] [D40-E30-Kosmos]], 

    D40-E30-Kosmos=[Diana P: [email protected] [DEV-andorraCASS2007, D40-E30-Kosmos], Diana P: [email protected] [DEV-andorraCASS2007, D40-E30-Kosmos], Alejandro Quintana: [email protected] [DEV-andorraCASS2007], Mikel Sobradillo Ecenarro: [email protected] [D40-E30-Kosmos], Rubén R: [email protected] [DEV-andorraCASS2007, D40-E30-Kosmos], Rubén R: [email protected] [DEV-andorraCASS2007, D40-E30-Kosmos], Diego M: [email protected] [D40-E30-Kosmos], Jordi C: [email protected] [DEV-andorraCASS2007, D40-E30-Kosmos], Jordi C: [email protected] [DEV-andorraCASS2007, D40-E30-Kosmos], Luis T: [email protected] [DEV-andorraCASS2007, D40-E30-Kosmos], Luis T: [email protected] [DEV-andorraCASS2007, D40-E30-Kosmos], Prova Prova: [email protected] [D40-E30-Kosmos], pruebaopenfire: [email protected] [D40-E30-Kosmos], Melissa Y: [email protected] [D40-E30-Kosmos], Fabio C: [email protected] [D40-E30-Kosmos]], 

    Otros Contactos=[jfkgl: jhgfk, AAAAA: [email protected]]
}
4
  • please show result what getting in LinkedHashMap with current logic Commented Feb 16, 2015 at 12:17
  • I added what you needed. Commented Feb 16, 2015 at 12:29
  • already have an answer try it Commented Feb 16, 2015 at 12:30
  • Just seen it, thanks. I'll try it Commented Feb 16, 2015 at 12:30

1 Answer 1

2

Heres a solution using sets, which will automatically avoid duplicates:

    Map<String, List<String>> userToGroup = new HashMap<String, List<String>>();
    userToGroup.put("John", Arrays.asList("GroupA"));
    userToGroup.put("Peter", Arrays.asList("GroupB", "GroupC"));
    userToGroup.put("Bob", Arrays.asList("GroupC"));
    userToGroup.put("Tom", Arrays.asList("GroupA", "GroupB"));
    userToGroup.put("Jack", Collections.<String> emptyList());

    Set<String> myGroups = new HashSet<String>(Arrays.asList("GroupA", "GroupB", "GroupC"));

    Map<String, Set<String>> groupToUsers = new HashMap<String, Set<String>>();
    groupToUsers.put("Generic", new HashSet<String>());
    for (String user : userToGroup.keySet()) {
        List<String> groups = userToGroup.get(user);
        if (groups.isEmpty()) {
            groupToUsers.get("Generic").add(user);
            continue;
        }

        for (String group : groups) {
            if (!myGroups.contains(group)) {
                continue;
            }

            Set<String> userInGroup = groupToUsers.get(group);
            if (userInGroup == null) {
                userInGroup = new HashSet<String>();
                groupToUsers.put(group, userInGroup);
            }

            userInGroup.add(user);
        }
    }

    System.out.println(groupToUsers);

Output:

{GroupC=[Bob, Peter], GroupB=[Tom, Peter], GroupA=[Tom, John], Generic=[Jack]}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks in advanced!, I'm gonna try and tell something ^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.