Scala : How to find types of values inside a scala nested collection -


consider following variables in scala :

val nestedcollection_1 = array(   "key_1" -> map("key_11" -> "value_11"),   "key_2" -> map("key_22" -> "value_22"))  val nestedcollection_2 = map(   "key_3"-> ["key_33","value_33"],   "key_4"-> ["key_44"->"value_44"]) 

following questions :

1) want read values of variables nestedcollection_1, nestedcollection_2 , ensure value of variables of format

array[map[string, map[string, string]] 

and

map[string, array[string]]] 

2) possible detailed type of variable in scala? i.e. nestedcolelction_1.some_method should return array[map[string, map[string, string]] type of values

i not sure exacltly mean. compiler can ensure type of variable if annotate type:

val nestedcollection_2: map[string, list[string]] = map(   "key_3"-> list("key_33", "value_33"),   "key_4"-> list("key_44", "value_44")) 

you can see type of variable in scala repl when define it, or using alt + = in intellij idea.

scala> val nestedcollection_2 = map(      |   "key_3"-> list("key_33", "value_33"),      |   "key_4"-> list("key_44", "value_44")) nestedcollection_2: scala.collection.immutable.map[string,list[string]] = map(key_3 -> list(key_33, value_33), key_4 -> list(key_44, value_44)) 

edit

i think question now. here how can type string:

import scala.reflect.runtime.universe._ def typeasstring[a: typetag](elem: a) = {   typeof[a].tostring } 

test:

scala> typeasstring(nestedcollection_2) res0: string = map[string,scala.list[string]] scala> typeasstring(nestedcollection_1) res1: string = scala.array[(string, scala.collection.immutable.map[string,string])] 

Comments