
import java.util.*; public class Main{ public static void main(String [] args) { Scanner sc=new Scanner(System.in); while(sc.hasNext()) { char[] words=sc.nextLine().toCharArray(); Map<String,Integer> map=new HashMap<>(); for(char c:words) { String temp=String.valueOf(c); if((c>=‘a‘&&c<=‘z‘)||(c>=‘A‘&&c<=‘Z‘)||(c>=‘0‘&&c<=‘9‘)||(c==‘ ‘)){ if(map.keySet().contains(temp)) { map.put(temp,map.get(temp)+1); }else{ map.put(temp,1); } } } List<Map.Entry<String,Integer>> list=new ArrayList<>(map.entrySet()); Collections.sort(list, new Comparator<Map.Entry<String,Integer>>(){ @Override public int compare(Map.Entry<String,Integer> o1, Map.Entry<String,Integer> o2) { if(o1.getValue()<o2.getValue()) { return 1; }else{ if(o1.getValue()==o2.getValue()) { return o1.getKey().compareTo(o2.getKey()); } else { return -1; } } } }); for(Map.Entry<String,Integer> entry:list) { System.out.print(entry.getKey()); } System.out.println(); } } }
原文:https://www.cnblogs.com/lemonzhang/p/13113708.html