1

I have to create a Binary Search Tree that takes a WordCount Object as the key and the number of times that word is added to the BST as the value. In my code I have the class:

public class WordCountMap<WordCount, V> {
    private TreeNode root;
    private WordCount wordItem;

   /**
    * This is the node class
    */
   private class TreeNode {
        private WordCount item;
        private V count;
        private TreeNode left;
        private TreeNode right;

        TreeNode(WordCount item, V count, TreeNode left, TreeNode right) {
            this.left = left;
            this.right = right;
            this.item = item;
        }
    }

    public WordCountMap() {
        //Create a new WordCountMap
    }

    /**
     * Adds 1 to the existing count for a word, or adds word to the WordCountMap
     * with a count of 1 if it was not already present.
     */ 
     public void incrementCount(String word) {
          wordItem = new WordCount(word);
          if (root == null) {
              root = new TreeNode(wordItem, wordItem.getCount(), null, null);
          }
          //more code below
    }
}

When I try to compile the code I get the error:

WordCount extends Object declared in class WordCountMap

I tried @SuppressWarnings("rawtypes") but that still resulted in the same error.

2
  • 1
    We can't do new WordCount(...) when WordCount is a type variable. For more targeted answers: What are you trying to do? Why do you want WordCount as a type parameter? How do you instantiate a new WordCountMap? Commented May 22, 2015 at 19:15
  • I think you need to provide the second parameter. WordCountMap<WordCount, SOMETHING> Commented May 22, 2015 at 19:18

1 Answer 1

4

It looks like you aren't using the generics properly.

Imagine you replaced all instances of WordCount with T (it's the same program either way) . In incrementCount(), you have the line wordItem = new T(word); but that doesn't make sense because you don't know if T has a constructor with a String argument.

Since it looks like you always want the key to be of type WordCount, you may want to declare the class as follows.

public class WordCountMap<V> {}

But do you want the type of count to be any object? Can the type of count be String, Stack, or InputStream? You can be on the safer side and declare the class as . . .

public class WordCountMap<V extends Number> {}

But even then, why do you want the count to be generic? Are you planning on having this class be extended by another class? Is there any reason the count type can't just be int or long? I would get rid of all generics and just use some primitive number type for the count

public class WordCountMap { 
    ... 
    private class TreeNode { 
        ... 
        private int count;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yea it would be better to get rid of generic types. For bonus points we could've implemented WordCountMap as Dictionary, but at this late hour I would rather just not use generic types. Thanks anyways.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.