Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

275
Views
Not able to print the value of a key of a Map in Kotlin

I'm fairly new to Kotlin. I want to print the count of a character in a string. For this, I'm using Kotlin's groupingBy() function and applying eachCount() to it.

My code:

val str = "0100101101010"
val countMap : Map<Char, Int> = str.groupingBy { it }.eachCount()
println(countMap["1"])

But I'm getting this error in the console: Type inference failed. The value of the type parameter K should be mentioned in input types (argument types, receiver type or expected type). Try to specify it explicitly.

Can someone explain to me what I'm doing wrong here?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

"1" is a string literal, but your map has characters as keys. You should use the character literal '1':

println(countMap['1'])

The reason for that confusing error message is because it is trying to call this get overload, which is generic. It tries to infer the generic type arguments, and fails.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!