简体   繁体   中英

How to iterate through a Map[B,Int] in Scala

I have a Map[B,Int] that I am trying to iterate over: So I do something like:

map foreach { case (k,v) => println(k + " " + v)}

However I keep getting the error that there is a type mismatch: Found: Unit Expected: B

I have read several times on different tutorials that traversing a map is always the same regardless of what type is point to another.

I am not sure how to fix this.

You need to override the toString method, otherwise print won't work. This is done by default for Int or String so that's why it complains only for B class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM