斯卡拉从地图数组得到数组 [英] Scala get Array from array in Map
问题描述
我在新斯卡拉,有地图:(这样也好,因为我获得巨大的地图像这样从蒙戈)
i new at scala, there is the Map: (it so, cos i get huge Map like this from mongo)
val test = Map(
"temp" -> Array( 1,
Array(
1, "kisa"
)
)
)
我怎样才能获得KISA?
How i can get "kisa" ?
推荐答案
所以......让我解释,
So... let me explain,
val test = Map(
"temp" -> Array( 1,
Array(
1, "kisa"
)
)
)
现在测试
是 scala.collection.immutable.Map [字符串,数组[任何]
这意味着地图
从字符串
到数组[任何]
。
所以...获得与主要关联值 - TEMP,您
So... to get value associated with key - "temp", you do
val tempVal = test( "temp" )
现在, tempVal
的类型为数组[任何]
键,其值为阵列(1,阵列(1,KISA))
Now, tempVal
is of type Array[ Any ]
and its value is Array( 1, Array( 1, kisa ) )
我们要的是第二个(指数= 1)进入该数组中
What we want is the second ( index = 1 ) entry in this array.
val secondEntry = tempVal( 1 )
现在... secondEntry
的类型为任何
键,其值为阵列(1 ,克萨)
...我们无法获得克萨
现在......首先,我们必须从施放此的任何
到数组[任何]
。
Now... secondEntry
has type Any
and its value is Array( 1, Kisa )
... we can not obtain Kisa
now... first we have to cast this from Any
to Array[ Any ]
.
val secondEntryArray = secondEntry.asInstanceOf[ Array[ Any ] ]
现在... secondEntryArray
的类型为数组[任何]
键,其值为阵列(1,KISA)
Now... secondEntryArray
is of type Array[ Any ]
and its value is Array( 1, kisa )
现在,我们必须从这个数组第二个条目,
Now, we have to get second entry from this array,
val kisaVal = secondEntryArray( 1 )
现在... kisaVal
的类型为任何
键,其值为 KISA
。你知道,这是一个字符串
你可能想把它当作一个字符串
...所以让类型强制转换为一个字符串,
Now... kisaVal
has type Any
and its value is kisa
. You know that it is a String
and you probably want it as a String
... so lets type-cast it to a String,
val kisaString = kisaVal.asInstanceOf[ String ]
和你做。
要做到这一点只是在同一行......
To do it simply in one line....
val kisa = test( "temp" )( 1 ).asInstanceOf[ Array[ Any ] ]( 1 ).asInstanceOf[ String ]
这篇关于斯卡拉从地图数组得到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!