https://kotlinlang.org logo
Title
o

oday

02/01/2021, 8:08 PM
why can’t I do this?
val map = mutableMapOf<Char, Int>()

map['a'] += 1
r

Russell Stewart

02/01/2021, 8:16 PM
I think because there isn't an entry for
a
in the map yet. If you're trying to add a value to it, you would want:
map['a'] = 1
Or just set it in the definition:
val map = mutableMapOf<Char, Int>('a' to 1)
o

oday

02/01/2021, 8:16 PM
I want to construct the map based on characters from another String
r

Russell Stewart

02/01/2021, 8:16 PM
However, if you're trying to add 1 to whatever value is in the map, then you'd have to set that value first:
val map = mutableMapOf<Char, Int>('a' to 1)
map['a'] += 1
You mean assign each character in the string to a key in the map?
h

Haris Khan

02/01/2021, 8:18 PM
+= is effective plusAssign
plusAssign's receiver canot be null but map["a"] is a nullable type.
💡 1
o

oday

02/01/2021, 8:18 PM
fun canConstruct(ransomNote: String, magazine: String): Boolean {
    val letters = mutableMapOf<Char, Int>()

    magazine.forEach {
        if (it != ' ') {
            letters[it] += 1
        }
    }
i want to have the characters in the magazine mapped out with 1 starting value for each character
r

Russell Stewart

02/01/2021, 8:19 PM
OK, then I think you just want to do
= 1
instead of
+= 1
.
o

oday

02/01/2021, 8:19 PM
but later I will want to decrement that value
and it definitely does exist as you saw, I made a map out of each of the characters
your example
val map = mutableMapOf<Char, Int>('a' to 1)
map['a'] += 1
doesn’t work, same error
r

Russell Stewart

02/01/2021, 8:20 PM
You made an empty map, but I don't see where you're assigning values to it.
If you want to assign the value
1
for each character, then do:
magazine.forEach {
  if (it != ' ') {
    letters[it] = 1
  }
}
o

oday

02/01/2021, 8:21 PM
I want to add 1
r

Russell Stewart

02/01/2021, 8:21 PM
OK, but you have to have a value to add it to. At first, there's nothing in there.
o

oday

02/01/2021, 8:22 PM
alright, so I run that loop, and after it i will be able to add
magazine.forEach { 
        letters[it] += 1
    }
same error here
that ran after the loop where i placed values inside
the adding function itself expects null, even if I don’t have null in there
r

Russell Stewart

02/01/2021, 8:23 PM
Yeah. If you want the loop to add 1 if the values are already there, you could try:
magazine.forEach {
  if (it != ' ') {
    if (letters.containsKey(it) {
      letters[it] += 1
    } else {
      letters[it] = 1
    }
  }
}
👍 1
o

oday

02/01/2021, 8:23 PM
CONTAINSKEY
ok that’s the thing i missed
thank you
r

Russell Stewart

02/01/2021, 8:24 PM
No problem!
o

oday

02/01/2021, 8:41 PM
map[it] = (map[it] ?: 0) + 1
r

Russell Stewart

02/01/2021, 8:42 PM
Yes, that's much better. Kind of embarrassed I didn't think of it. 🙂
o

oday

02/01/2021, 8:44 PM
I just submitted the question and got to see other answers, this was in one of them
r

Russell Stewart

02/01/2021, 8:45 PM
Yeah, this one is nice because it takes advantage of Kotlin's nullability features to make things more compact.
n

nanodeath

02/01/2021, 9:44 PM
if we're code golfing, here's something else that you can try:
val foo = "hello world"
val counts: Map<Char, Int> = foo.groupingBy { it }.eachCount()