Home > Back-end >  JAVA: Map.put(K, K V), doesn't Maps only have 2 values?
JAVA: Map.put(K, K V), doesn't Maps only have 2 values?

Time:12-15

So I started programming 2 months ago, this maybe obvious but I have no idea how I could even google my question.

Map<Character,Integer> characters = new HashMap<>();
    
    for (int i = 0; i < word.length(); i  ) {
        char c = word.toLowerCase().charAt(i);
        if(characters.containsKey(c)) {
            characters.put(c,characters.get(c)   1);
        } else {
            characters.put(c,1);
        }
    }

In the 4th line, I put a Character, then a Character and Integer pair into the map.

Doesn't the .put method require exactly 2 values (Key and Value)?

CodePudding user response:

You are stil using two arguments in this line.

characters.put(c,characters.get(c)   1);

The second argument is the operation characters.get(c) 1 that results in a single integer.

  • Related