class Solution {    public List<String> topKFrequent(String[] words, int k) {        List<String> ans = new ArrayList<>();        // 单词频率统计        Map<String, Integer> map = new HashMap<>();        for (String word : words) {            if (!map.containsKey(word)) {                map.put(word, 1);            } else {                map.put(word, map.get(word) + 1);            }        }        // 出现次数相同时按照字母顺序进行排序,出现次数不同时按照频率进行排序;        PriorityQueue<String> minHeap = new PriorityQueue<>(            (o1, o2) -> (map.get(o1) == map.get(o2) ? o2.compareTo(o1) : map.get(o1) - map.get(o2))        );         for (String word : map.keySet()) {            minHeap.offer(word);            if (minHeap.size() > k)                minHeap.poll();        }        for (int i = 0; i < k; i++) {            ans.add(minHeap.remove());        }        Collections.reverse(ans);        return ans;    }}