我有一个字符串数组列表,它们作为键放入映射中。该映射的值是字符串出现的频率。
我想读取 20 个最常见的字符串,因此我使用实现最大堆结构的 PriorityQueue。
尽管其元素的参数类型似乎是正确的,但我无法添加到优先级队列。
static Map<String, Integer> mapGuy = new HashMap<>();
private static PriorityQueue<Map.Entry<String, Integer>> pq = new PriorityQueue<>();
public static List<String> head() {
if (map.size() == 0){
List<String> res = new ArrayList<>();
return res;
}
for (Map.Entry<String, Integer> entry : map.entrySet()) {
pq.offer(entry);
}
List<String> frequentGrams = new ArrayList<>();
int counter = 0;
/*
* the counter this determines that you only add 20 items to frequentGrams
*
* the while loop stops incrementing if there are …Run Code Online (Sandbox Code Playgroud)