简体   繁体   中英

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. 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.

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().
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 >

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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