Creating an unordered map of <char, int> in java

collections, declaration, dictionary, java

Solution

Try this declaration:

Map<Character, Integer> m = new HashMap<Character, Integer>();

You can then add characters as such:

char c = //...;
if (m.containsKey(c))
    m.put(c, m.get(c) + 1);
else
    m.put(c, 1);

Problem

So I need to have a some sort of multiset of characters, where adding a duplicate character increases the cardinality by 1, and the multiplicity of characters should not drastically increase the memory that the object takes up. This will be implemented with some sort of map where characters are keys, that hold a value representing the number of that character is represented in the set. However, I'm struggling to figure out which collection would be best for this (I was looking at hashmap) and how to declare this data type. I was doing something like this ``` Map m = new HashMap(char, int); ``` But the above is an incorrect declaration, and I'm not sure how to exactly approach this.

Original source