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]]
}
LinkedHashMapwith current logic