Just a general question (and I’m sort of new to java) but what would be a good collection that I could add objects to, and keep track of how many of each I’ve added? For example, if I added the alphabet a character at a time, it would have 26 different characters, and an associated value of 1 for each. Likewise, adding ‘z’ 10 times would have z with an associated 10. Suggestions? The name “hashtable” had sounded promising, but I don’t think I want to use that…
Share
There’s no need to use a special data structure as simply using a HashMap should work well. When adding a char, myChar, you call get(myChar), and if null, create a new item for the map for that Character with an Integer value of 1. If the Map returns an Integer, simply add one to it, and then put it back into the Map.