将元素添加到LinkedHashMap内的ArrayList [Android]

时间:2015-02-16 12:12:34

标签: java android arraylist linkedhashmap

我有一个User列表,每个列表都包含Group个列表。像这样:

John -->[GroupA]

Peter --> [GroupB, GroupC]

Bob --> [GroupC]

Tom --> [GroupA,GroupB]

Jack --> []

然后我有自己的小组列表:

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

所以我想要的是将用户放在"桶中#34;这与我的团体相匹配。如果用户没有组,则会将其添加到列表中的“通用”组中。所以结果必须是:

联系人:

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

所以我测试这样的东西,但是,它在LinkedHashMap上生成重复的值,我不知道如何解决它。

    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;

            }

        }

问题:

我该如何解决?有没有办法做得更好?

我的群组列表:

DEV-andorraCASS2007
D40-E30-Kosmos

每个的联系人和组列表是:

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

结果是:

02-16 13:22:13.436: D/TESTINGGROUPS(6056): 
{
    DEV-andorraCASS2007=[Diana P: dianapa@mail.es [DEV-andorraCASS2007, D40-E30-Kosmos], Diana P: dianapa@mail.es [DEV-andorraCASS2007, D40-E30-Kosmos], Alejandro Q: alejandroq@mail.es [DEV-andorraCASS2007], Mikel S: mikels@mail.es [D40-E30-Kosmos], Rubén R: rubenrc@mail.es [DEV-andorraCASS2007, D40-E30-Kosmos], Rubén R: rubenrc@mail.es [DEV-andorraCASS2007, D40-E30-Kosmos], Diego M: diegomm@mail.es [D40-E30-Kosmos], Jordi C: jordics@mail.es [DEV-andorraCASS2007, D40-E30-Kosmos], Jordi C: jordics@mail.es [DEV-andorraCASS2007, D40-E30-Kosmos], Luis T: luisg@mail.es [DEV-andorraCASS2007, D40-E30-Kosmos], Luis T: luisg@mail.es [DEV-andorraCASS2007, D40-E30-Kosmos], Prova Prova: prova@mail.es [D40-E30-Kosmos], pruebaopenfire: pruebaopenfire@mail.es [D40-E30-Kosmos], Melissa Y: melissak@mail.es [D40-E30-Kosmos], Fabio C: fabioc@mail.es [D40-E30-Kosmos]], 

    D40-E30-Kosmos=[Diana P: dianapa@mail.es [DEV-andorraCASS2007, D40-E30-Kosmos], Diana P: dianapa@mail.es [DEV-andorraCASS2007, D40-E30-Kosmos], Alejandro Quintana: alejandroq@mail.es [DEV-andorraCASS2007], Mikel Sobradillo Ecenarro: mikels@mail.es [D40-E30-Kosmos], Rubén R: rubenrc@mail.es [DEV-andorraCASS2007, D40-E30-Kosmos], Rubén R: rubenrc@mail.es [DEV-andorraCASS2007, D40-E30-Kosmos], Diego M: diegomm@mail.es [D40-E30-Kosmos], Jordi C: jordics@mail.es [DEV-andorraCASS2007, D40-E30-Kosmos], Jordi C: jordics@mail.es [DEV-andorraCASS2007, D40-E30-Kosmos], Luis T: luisg@mail.es [DEV-andorraCASS2007, D40-E30-Kosmos], Luis T: luisg@mail.es [DEV-andorraCASS2007, D40-E30-Kosmos], Prova Prova: prova@mail.es [D40-E30-Kosmos], pruebaopenfire: pruebaopenfire@mail.es [D40-E30-Kosmos], Melissa Y: melissak@mail.es [D40-E30-Kosmos], Fabio C: fabioc@mail.es [D40-E30-Kosmos]], 

    Otros Contactos=[jfkgl: jhgfk, AAAAA: aaa@aaa.es]
}

1 个答案:

答案 0 :(得分:2)

使用集合的解决方案,这将自动避免重复:

    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);

输出:

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