Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<not computed> on collection output in Scala

When I tried to println MapView I have been got a message MapView(<not computed>)

val phonebook = Map("Jim" -> 555, "Daniel" -> 789)
println(phonebook)           // Map(Jim -> 555, Daniel -> 789)

val filteredMap = phonebook.filterKeys(_.startsWith("J"))
println(filteredMap("Jim"))  // 555
println(filteredMap)         // MapView(<not computed>)

If I defined filteredMap as following

val filteredMap = phonebook.view.filterKeys(_.startsWith("J"))

then nothing have been changed. Scala version is 2.13.2

Why mapview cannot be printed and what is the best way to fix it?

like image 314
Loom Avatar asked Sep 11 '25 12:09

Loom


1 Answers

Add .toMap to convert your mapView into a static map. As suggested by @ Luis Miguel Mejía Suárez

val filteredMap = phonebook.view.filterKeys(_.startsWith("J")).toMap
like image 108
Raman Mishra Avatar answered Sep 14 '25 03:09

Raman Mishra