如何在Scala Split()之后保留元素的顺序

时间:2017-06-09 14:40:40

标签: apache scala

我有一个这样的字符串:

myString = "Name:varchar nullable  ,Age:varchar nullable,BirthTimestamp:varchar nullable,Time:varchar nullable,ZipCode:varchar nullable,Address:varchar nullable"

我正在尝试像这样解析这个字符串 - >

val tmp = myString.split(",").map(x=>x.split(":")).map(x=>
  x(0)->x(1).split(" ")).toMap
tmp.foreach(x=>println(x._1+" "+x._2.foreach(y=>print(y))))

打印为

varcharnullableName ()
varcharnullableAddress ()  --> Why Address is printed here instead of Map? 
varcharnullableBirthTimestamp ()
varcharnullableAge ()
varcharnullableZipCode ()
varcharnullableTime ()

如何保留这些元素的顺序,类似于输入字符串?

此致

1 个答案:

答案 0 :(得分:1)

因为Map是无序的,只需删除.toMap就可以了:

val tmp = myString.split(",").map(x=>x.split(":")).map(x=>
  x(0)->x(1).split(" "))