lak
lak

Reputation: 143

Scala: How to print a list of map using scala

I am writing below code,

val maplist=List(Map("id" -> "1", "Name" -> "divya"), 
         Map("id" -> "2", "Name" -> "gaya")
        )

val header=maplist.flatMap(_.keys).distinct
val data=maplist.flatMap(_.values)
println(header)
println(data)

I am getting the below output,

List(id, Name)
List(1, divya, 2, gaya)

however I am expecting output as below,

id Name

1 Divya

2 gaya

here in this case I am having only 2 header but in my map it may contain more than 2 headers how to display all in rows. Please help me.

Upvotes: 1

Views: 1672

Answers (1)

Franck Cussac
Franck Cussac

Reputation: 319

    val maplist=List(Map("id" -> "1", "Name" -> "divya"),
        Map("id" -> "2", "Name" -> "gaya")
    )

    val header=maplist.flatMap(_.keys).distinct
    val data=maplist.map(_.values)
    println(header.mkString(" "))
    data.foreach(x => println(x.mkString(" ")))

Upvotes: 1

Related Questions