标题: 是大神的请过来看看,百撕不得骑姐 [打印本页] 作者: 子若 时间: 2016-4-27 00:18 标题: 是大神的请过来看看,百撕不得骑姐 程序如下:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一串字符串:");
String line = sc.nextLine();
char[] arr = line.toCharArray();
Map<Character, Integer> map = new HashMap<Character, Integer>();
for (char c :arr ) {
if (map.containsKey(c)) {
map.put(c, map.get(c)+1);
}else{
map.put(c, 1);
}
}
TreeMap<Integer, Character> tm = new TreeMap<>(new Comparator<Integer>() {
@Override
public int compare(Integer i1,Integer i2){
int num = i2 - i1;
return num == 0 ? 1 : num;
}
});
for (Character c : map.keySet()) {
tm.put(map.get(c), c);
}
System.out.println(tm);