如何以系统格式打印地图是Scala

时间:2017-08-18 23:02:34

标签: scala formatting

我有一张地图,我试图以系统格式打印它,但卡住了。我想要一个像下面这样的结果。我是斯卡拉的新人。

val mapTest =
    Map(RedDef -> List(Map(RedTypP -> ^.{14}XY.*$, RedTypC -> XY, ExpNoOfAt -> 19, attributes -> List(Map(AttId -> 1, AttNa -> Pro Ty, AttMaxLen -> 1, DataTy -> Map(DataTyName -> AN)), Map(AttId -> 2, AttNa -> Pro Cod, AttMaxLen -> 6, DataTy -> Map(DataTyName -> AN)), Map(AttId -> 3, AttNa -> REQ CODE, AttMaxLen -> 7, DataTy -> Map(DataTyName -> AN))), RedLen -> 117, RedDeIn -> 2, MinNoOfAt -> 19), Map(RedTypP -> ^.{14}JK.*$, RedTypC -> JK, ExpNoOfAt -> 10, attributes -> List(Map(AttId -> 1, AttNa -> Log Si Ty, AttMaxLen -> 1, DataTy -> Map(DataTyName -> A)), Map(AttId -> 2, AttNa -> Log Si Cod, AttMaxLen -> 6,PassValidation->List(00000000) DataTy -> Map(DataTyName -> AN)), RedDeIn -> 3, MinNoOfAt -> 10), Map(RedTypP -> ^.{14}FK.*$, RedTypC -> FK, ExpNoOfAt -> 33, attributes -> List(Map(AttId -> 1, AttNa -> Pro Ty, AttMaxLen -> 1, DataTy -> Map(DataTyName -> AN)), Map(AttId -> 2, AttNa -> Pro Cod, AttMaxLen -> 6, DataTy -> Map(DataTyName -> AN)), Map(AttId -> 3, AttNa -> REQ CDE, AttMaxLen -> 7, DataTy -> Map(DataTyName -> AN)), Map(AttId -> 4, AttNa -> RED Ty, AttMaxLen -> 2, DataTy -> Map(DataTyName -> AN))), RedLen -> 117, RedDeIn -> 4, MinNoOfAt -> 33)))

我想要输出:

RedTypP = ^.{14}XY.*$
RedTypC = XY
ExpNoOfAt = 19
AttId = 1
AttNa = Pro Ty
AttMaxLen =1
DataTyName = AN
AttId = 2
AttNa = Pro Cod
AttMaxLen = 6
PassValidation=00000000
DataName = AN
RedTypP = "JK.*$"
RedTypC = JK
ExpNoOfAt = 10
AttId = 1
AttNa = Log Si Ty
AttMaxLen = 1
DataTyName = AN
RedLen = 117
RedDeIn = 2
MinNoOfAt =10 

1 个答案:

答案 0 :(得分:1)

read this关于在SO上提出好问题。

之后看看这有多接近你的目标。

def map2str(m: Map[String,_]): String = {
  m.map{
    case (_:String,v:List[Map[String,_]]) => v.map(map2str).mkString
    case (_:String,v:Map[String,_])       => map2str(v)
    case (k:String,v:String)              => s"$k = $v\n"
    case x                                => s"unknown: $x\n"
  }.mkString
}