2017-08-18 70 views
1

我有一張地圖,並試圖以系統格式打印它,但卡住了。我想要一個像下面這樣的結果。我在斯卡拉非常新。如何以系統格式打印地圖是斯卡拉

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

read this約在這裏問上這麼好的問題。

之後,看看你接近你的目標有多接近。

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 
} 
+0

jwvh感謝您的重播基本上我不想在printmap方法中使用println語句。每當我想訪問println(printmap(mapTest))等printmap方法時,只有它打印結果。 – user1363308

+0

@ user1363308;微不足道的變化。看一看。 – jwvh

+0

jwvh,我得到這個錯誤java.lang.ClassCastException:java.lang.String不能轉換爲scala.collection.immutable.Map – user1363308