我正在做一些霍夫曼编码,我希望能够打印频率表。
然而,我尝试了下面的代码,得到了一个‘不兼容的类型:条目不能被转换为(Map.Entry Entry :freq.entrySet())的条目’错误。有没有办法修复它,我可能能够显示频率表,它将更多地以表或类似数组的形式,而不是地图形式?
public static void buildHuffmanTree(String text)
{ // count frequency each character and store it in a map
Map<Character, Integer> freq = new HashMap<>();
for (int i = 0 ; i < text.length(); i++)
{
if (!freq.containsKey(text.charAt(i)))
{
freq.put(text.charAt(i), 0);
}
freq.put(text.charAt(i), freq.get(text.charAt(i)) + 1);
}
// Create a priority queue to store nodes of Huffman tree
// Notice that highest priority item has lowest frequency
PriorityQueue<Node> pq = new PriorityQueue<>((l, r) -> l.freq -
r.freq);
for (Map.Entry<Character, String> entry : freq.entrySet())
{
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
转载请注明出处:http://www.souyuntu.com/article/20230526/1954305.html