题解 | #字符统计#
字符统计
http://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); String s=sc.nextLine(); Map<Character,Integer> map=new HashMap<>(); for(char ch:s.toCharArray()){ map.put(ch,map.getOrDefault(ch,0)+1); } List<Map.Entry> list=new ArrayList<>(map.entrySet()); Collections.sort(list,(o1,o2)->{ if(o1.getValue()!=o2.getValue()){ return (int) o2.getValue()-(int) o1.getValue(); }else{ return (char) o1.getKey() - (char) o2.getKey(); } }); for(Map.Entry entry:list){ System.out.print(entry.getKey()); } } }