13,100
社区成员
发帖
与我相关
我的任务
分享
static Map sortMap(Map map) {
List list = new LinkedList(map.entrySet());
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
int result = ((Comparable) ((Map.Entry) (o1)).getValue())
.compareTo(((Map.Entry) (o2)).getValue());
return result == 0?
((Comparable) ((Map.Entry) (o1)).getKey())
.compareTo(((Map.Entry) (o2)).getKey())
:result;
}
});
Map result = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry)it.next();
result.put(entry.getKey(), entry.getValue());
}
return result;
}
package com.xuz.csdn.worldcup.day22;
import java.util.HashMap;
import java.util.Map;
public class HelloWorldCountTest {
public static void main(String[] args) {
String hello = "helloworld!";
Map<Character, Integer> map = new HashMap<Character, Integer>();
char[] ch = hello.toCharArray();
for (char c : ch) {
Integer i = map.get(c);
if (i == null) {
map.put(c, 1);
} else {
map.put(c, i.intValue() + 1);
}
}
System.out.println(map);
}
}