1

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.

1 Answer 1

1
    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(" ")))
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your support, how to convert the same in to data frame instead of printing the output using println. I have to use .show() function to display the output.
New question, new topic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.