Count Items in a Generic Tally
mediumQuestion 504Write a generic class that counts how many times each item has been added. Because the class is generic, the same code can count words, numbers or characters.
Requirements:
1. Tally<T> stores its counts in a private HashMap<T, Integer>: the item is the key and the number of times it was added is the value.
2. void add(T item) raises the count of item by one. The first time an item is added its count becomes 1.
3. int countOf(T item) returns how many times item was added, and 0 for an item that was never added.
4. int distinct() returns how many different items have been added.
Do NOT write a main method.
Expected output
3 1 0 2
= newline. The output ends with a newline, so print one after the last line.
Hints4 available
Sign in to use hintsTopics
GenericsClasses
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (sysout, psvm, fori)
Your output
Run your code to see the output here...
Test cases
Run your code to see test case results.