对HashMap进行key、Value排序
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String str = sc.nextLine();
HashMap<String, Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < str.length(); i++) {
if (map.keySet().contains(str.charAt(i)+ "")) {
map.put(str.charAt(i)+"", map.get(str.charAt(i)+ "") + 1);
} else {
map.put(str.charAt(i)+"", 1);
}
}
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
List<Map.Entry<String, Integer>> list= new ArrayList<Map.Entry<String, Integer>>(entrySet);
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> x, Map.Entry<String, Integer> y) {
if (x.getValue() == y.getValue()) {
return x.getKey().compareTo(y.getKey());
} else {
return y.getValue() - x.getValue();
}
}
});
for (Map.Entry<String, Integer> item : list) {
System.out.print(item.getKey());
}
System.out.println("");
}
}
}