简体   繁体   English

哈夫曼树的HashMap

[英]HashMap for Huffman Tree

I have an assignment dealing with Huffman trees. 我有一个处理霍夫曼树的任务。 My professor mentioned that we should use a HashMap but didn't explain anything about them. 我的教授提到我们应该使用HashMap,但没有解释它们。 I've looked things up about them but I am running into problems with them in my code. 我已经了解了他们,但我在我的代码中遇到了问题。 These are the error messages that I am getting. 这些是我得到的错误消息。

n00832607.java:542: error: incompatible types
for(Map.Entry e : charCounts.entrySet())
                                    ^
required: Entry
found:    Object
n00832607.java:610: error: incompatible types
for(Map.Entry entry : codeTable.entrySet())
                                       ^
required: Entry
found:    Object

So I can tell that it wants it to return an Entry but I am not exactly sure how to approach this. 所以我可以说它希望它返回一个条目,但我不确定如何处理它。 Am I supposed to be casting it as something? 我应该把它当作某种东西吗?

This is my code. 这是我的代码。

//Beging Huffman Class
class Huffman{

Tree tree;

TreeMap codeTable;


protected HashMap calculateFrequency(String message)
{

int messageLength = message.length();
char ch;
int count;
HashMap charCounts = new HashMap();

for(int i = 0; i< messageLength; i++)
{

ch = message.charAt(i); 

if(charCounts.containsKey(ch))
{
count = (int) charCounts.get(ch);
++count;
charCounts.put(ch, count);   
}

else
{
charCounts.put(ch, 1);
}
}
//end for
return charCounts;
}
//end calculateFrequency

protected void createHuffmanTree(String message)
{
HashMap charCounts = calculateFrequency(message);
PriorityQueue trees = new PriorityQueue();
Tree temp;

for(Map.Entry e : charCounts.entrySet())
  {

  temp = new Tree((char)e.getKey(), (int)e.getValue());
  trees.add(temp);

  }


while(trees.size() > 1)
  {
  temp = new Tree((Tree)trees.remove(), (Tree)trees.remove());
  trees.add(temp);   
  }
tree = (Tree)trees.remove();      

}
//end createHuffmanTree

//Begin displayCodeTable
public void displayCodeTable()
{

System.out.println("Character Code");

for(Map.Entry entry : codeTable.entrySet())
  {
  char key = (char)entry.getKey();
  System.out.println(key + "" + entry.getValue());
  }
}
//End displayCodeTable 

It's just that the fancy new Java syntax (available as of Java 5 or 6, i forget) is not very casting friendly. 只是花哨的新Java语法(从Java 5或6开始,我忘了)不是非常友好的。

In the old days, you'd have to do this: 在过去,你必须这样做:

    TreeMap map = new TreeMap();
    Iterator itr = map.entrySet().iterator();
    while (itr.hasNext())
    {
        E thing = (E)itr.next();            
    }

You can get around this by typing your TreeMap with <,>, and using the keySet() instead of the entrySet(). 您可以通过使用<,>键入TreeMap并使用keySet()而不是entrySet()来解决此问题。
That is: 那是:

    TreeMap map = new TreeMap<String, String>();  (or whatever types you're mapping)

If you want a quick fix, use this: 如果您想快速修复,请使用:

    for (Object o : map.entrySet()) {
       Map.Entry me = (Map.Entry)o;  
    }

Use generics to specify the types of key and value used in the collections. 使用泛型指定集合中使用的键和值的类型。 For instance 例如

HashMap < Character, Integer > HashMap <Character,Integer>

is a hashmap mapping chars to ints. 是一个将字符映射到整数的哈希映射。 You cannot use the primitive types (int, char, long, short, byte) in generics, so use the class wrappers (Integer, Character, Long, Short, Byte) instead. 您不能在泛型中使用基本类型(int,char,long,short,byte),因此请使用类包装器(Integer,Character,Long,Short,Byte)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM