gpt4 book ai didi

java - 将元素添加到 LinkedHashMap 中的 ArrayList [Android]

转载 作者:行者123 更新时间:2023-11-29 20:53:11 25 4
gpt4 key购买 nike

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

John -->[GroupA]

Peter --> [GroupB, GroupC]

Bob --> [GroupC]

Tom --> [GroupA,GroupB]

Jack --> []

然后我有自己的组列表:

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

所以我想要的是将用户放入一个“桶”中,使他们的组与我的相匹配。如果用户没有组,他们将被添加到列表中的通用组。所以结果必须是这样的:

联系人:

{ 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]
}

最佳答案

这是一个使用集合的解决方案,它会自动避免重复:

    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]}

关于java - 将元素添加到 LinkedHashMap 中的 ArrayList [Android],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28541182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com