I m试图以两种方式打上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) + ".
");
}
}
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;
}
既然你不能这样说,那就是一个常设专家委员会:
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.
任何见解?