PriorityQueue + HashMap

定义比较方式两种:override compareTo() / lambda表达式

遍历map两种方法:forEach() + lambda表达式 / Map.Entry<>

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.PriorityQueue;


public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNext()) {
            String s = in.nextLine();
            Map<Character, Integer> map = new HashMap<Character, Integer>();
            for(int i = 0; i < s.length(); i++) {
                char cur = s.charAt(i);
               map.put(cur, map.getOrDefault(cur, 0) + 1);
            }
            PriorityQueue<Point> pq = new PriorityQueue<>();
            /*
            PriorityQueue<Point> pq = new PriorityQueue<>((x, y) -> -128*(x.number - y.number) + (int)(x.c - y.c));
            for (Map.Entry<Character, Integer> entry : map.entrySet()) {
                Point p = new Point(entry.getKey(), entry.getValue());
                pq.offer(p);
            }
            */
            map.forEach((k, v) -> pq.offer(new Point(k, v)));
            StringBuilder res = new StringBuilder();
            while (!pq.isEmpty()) {
                res.append(pq.poll().c);
        }
        System.out.println(res.toString());
        }
    }
}
class Point implements Comparable<Point>{
    char c;
    int number;
    Point(char c, int number){
        this.c = c;
        this.number = number;
    }
    @Override
    public int compareTo(Point p2){
        if (p2.number != this.number){
            return p2.number - this.number;
        }else {
            return (int)(this.c - p2.c);
        }
    }
}