题目如下:
Given an array of strings words and an integer k, return the k most frequent strings.
Return the answer sorted by the frequency from highest to lowest. Sort the words with the same frequency by their lexicographical order.
```java
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/62286606113611.png "=600 #left")
String[] words = {"i","love","leetcode","i","love","coding"};
int k = 2;
List<String> res = topKFrequent(words, k);
System.out.println(res);
}
public static List<String> topKFrequent(String[] words, int k) {
HashMap<String, Integer> mapping = new HashMap<>();
for(String word: words){
if(!mapping.containsKey(word)) {
mapping.put(word, 0);
}
int count = mapping.get(word) + 1;
mapping.put(word,count);
}
PriorityQueue<String> pq = new PriorityQueue<>((w1, w2) -> mapping.get(w1).equals(mapping.get(w2)) ? w2.compareTo(w1): mapping.get(w1) - mapping.get(w2));
for(String word: mapping.keySet()){
pq.add(word);
if(pq.size() > k){
pq.poll();
}
}
List<String> res = new ArrayList<>();
while(!pq.isEmpty()){
res.add(pq.poll());
}
Collections.reverse(res);
return res;
我想问这一行代码
PriorityQueue<String> pq = new PriorityQueue<>((w1, w2) -> mapping.get(w1).equals(mapping.get(w2)) ? w2.compareTo(w1): mapping.get(w1) - mapping.get(w2));
是什么意思?