gpt4 book ai didi

java - 对 HashMap 进行排序,同时保留重复项

转载 作者:行者123 更新时间:2023-12-02 14:38:43 26 4
gpt4 key购买 nike

我正在尝试以两种方式对 HashMap 进行排序。默认方式:按值的字母顺序,第二种方式:按键的数字顺序,数字较大的在顶部。我四处搜寻,但找不到有关该主题的任何内容,而我找到的内容也不起作用。如果无法对两者进行排序(我希望将具有最高键的人放在顶部,随着人们具有较低键而减少,然后按字母顺序对所有其余的进行排序(以 0 作为键的人)。

这是我迄今为止尝试过的:

private HashMap<String, Integer> userGains = new HashMap<String, Integer>();

public void sortGains(int skill, int user) {
userGains.put(users.get(user).getUsername(), users.get(user).getGainedExperience(skill));
HashMap<String, Integer> map = sortHashMap(userGains);
for (int i = 0; i < map.size(); i++) {
Application.getTrackerOutput().getOutputArea(skill).append(users.get(user).getUsername() + " gained " + map.get(users.get(user).getUsername()) + " experience in " + getSkillName(skill) + ".\n");
}
}

public LinkedHashMap<String, Integer> sortHashMap(HashMap<String, Integer> passedMap) {
List<String> mapKeys = new ArrayList<String>(passedMap.keySet());
List<Integer> mapValues = new ArrayList<Integer>(passedMap.values());
LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();

Collections.sort(mapValues);
Collections.sort(mapKeys);

Iterator<Integer> it$ = mapValues.iterator();
while (it$.hasNext()) {
Object val = it$.next();
Iterator<String> keyIt = mapKeys.iterator();
while (keyIt.hasNext()) {
Object key = keyIt.next();
String comp1 = passedMap.get(key).toString();
String comp2 = val.toString();
if (comp1.equals(comp2)) {
passedMap.remove(key);
mapKeys.remove(key);
sortedMap.put((String) key, (Integer) val);
break;
}
}
}
return sortedMap;
}

由于您无法运行,因此这是一个 SSCCE:

private HashMap<String, Integer> userGains = new HashMap<String, Integer>();

private Object[][] testUsers = { { "Test user", 15 }, { "Test", 25 }, { "Hello", 11 }, { "I'm a user", 21 }, { "No you're not!", 14 }, { "Yes I am!", 45 }, { "Oh, okay. Sorry about the confusion.", 0 }, { "It's quite alright.", 0 } };

public static void main(String[] arguments) {
new Sorting().sortGains();
}

public void sortGains() {
for (Object[] test : testUsers) {
userGains.put((String) test[0], (Integer) test[1]);
}
HashMap<String, Integer> map = sortHashMap(userGains);
for (int i = 0; i < map.size(); i++) {
System.out.println(testUsers[i][0] + " gained " + map.get(testUsers[i][0]) + " experience.");
}
}

public LinkedHashMap<String, Integer> sortHashMap(HashMap<String, Integer> passedMap) {
List<String> mapKeys = new ArrayList<String>(passedMap.keySet());
List<Integer> mapValues = new ArrayList<Integer>(passedMap.values());
LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();

Collections.sort(mapValues);
Collections.sort(mapKeys);

Iterator<Integer> it$ = mapValues.iterator();
while (it$.hasNext()) {
Object val = it$.next();
Iterator<String> keyIt = mapKeys.iterator();
while (keyIt.hasNext()) {
Object key = keyIt.next();
String comp1 = passedMap.get(key).toString();
String comp2 = val.toString();
if (comp1.equals(comp2)) {
passedMap.remove(key);
mapKeys.remove(key);
sortedMap.put((String) key, (Integer) val);
break;
}
}
}
return sortedMap;
}

程序当前的输出是:

Test user gained 15  experience.
Test gained 25 experience.
Hello gained 11 experience.
I'm a user gained 21 experience.
No you're not! gained 14 experience.
Yes I am! gained 45 experience.
Oh, okay. Sorry about the confusion. gained 0 experience.
It's quite alright. gained 0 experience.

当我需要它时:

Yes I am! gained 45  experience. // start numeric sorting here, by highest key.
Test gained 25 experience.
I'm a user gained 21 experience.
Test user gained 15 experience.
No you're not! gained 14 experience.
Hello gained 11 experience.
It's quite alright. gained 0 experience. // start alphabetical sorting here, if possible.
Oh, okay. Sorry about the confusion. gained 0 experience.

有什么见解吗?

最佳答案

根本不可能对 HashMap 进行排序。根本。根据定义,HashMap 中的键是无序的。如果您希望对 Map 的键进行排序,请使用 TreeMap 和适当的 Comparator 对象。如果您想以多种方式访问​​相同的数据,您可以创建多个具有不同 ComparatorTreeMap

关于java - 对 HashMap 进行排序,同时保留重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8855849/

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