题解 | #字符统计#
字符统计
http://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String str = sc.next(); char[] array = str.toCharArray(); Map<Character,Integer> map = new HashMap<>(); for(char c : array){ if(map.containsKey(c)){ map.put(c,map.get(c)+1); }else{ map.put(c,1); } } List<Map.Entry<Character,Integer>> list = new ArrayList<>(map.entrySet());//改变封装 //将map 转换成list 排序 Collections.sort(list,new Comparator<Map.Entry<Character,Integer>>(){ @Override public int compare(Map.Entry<Character,Integer> o1,Map.Entry<Character,Integer> o2){ if(o1.getValue() < o2.getValue()){ return 1;//返回正数说明 o1 o2 要交换 }else if(o1.getValue() == o2.getValue()){ if(o1.getKey() < o2.getKey()){ return -1; }else{ return 1; } }else{ return -1;//返回负数说明 o1 o2 不要交换 } } }); for(Map.Entry entry : list){//注意这里要遍历list System.out.print(entry.getKey()); } System.out.println(); } } }