0
我有嵌套圖如下:斯卡拉如何扁平化嵌套地圖[字符串,任何]
val x: Map[String, Any] =
Map("a" -> "apple", "b" -> "ball", "c" -> Map("x" -> "cat", "y" -> 12))
,我想將其轉換成:
Map("a" -> "apple", "b" -> "ball", "x" -> "cat", "y" -> 12)
但是,如果我嘗試調用壓扁到X然後我得到例外。
x.flatten
Error:(40, 14) No implicit view available from (String, Any) => scala.collection.GenTraversableOnce[B].
println(mx.flatten)
Error:(40, 14) not enough arguments for method flatten: (implicit asTraversable: ((String, Any)) => scala.collection.GenTraversableOnce[B])scala.collection.immutable.Iterable[B].
Unspecified value parameter asTraversable.
println(x.flatten)
那麼,如何提供隱式視圖以平坦上述地圖?
看看這裏:http://alvinalexander.com/scala/how-to-combine-map-flatten-flatmap-scala-cookbook我認爲,扁平化是錯誤的做法。如果 – sascha10000
'x'已經在外地圖上的關鍵? – mfirry
爲什麼你擺在首位的是數據結構? – Reactormonk