Home > Software engineering >  Transform Array[Seq[(Int, String)]] to Seq[(Int, String)] in SCALA
Transform Array[Seq[(Int, String)]] to Seq[(Int, String)] in SCALA

Time:06-02

I'm pretty new to scala and I can't find a way to get rid of my Array[Seq[(Int, String)]] to one big Seq[(Int, String)] containing the (Int, String) of each Seq[(Int, String)].

Here is a more explicit example:

Array[Seq[(Int, String)]]:

ArrayBuffer((1,a), (1,group), (1,of))

ArrayBuffer((2,following), (2,clues))

ArrayBuffer((3,three), (3,girls))

And here is what I want my Seq[(Int, String)]] to looks like:

Seq((1,a), (1,group), (1,of), (2,following), (2,clues), (3,three), (3,girls))

CodePudding user response:

You are looking for flatten: val flat: Array[(Int, String)] = originalArray.flatten

If you want it to be a Seq rather than an array (good choice), just tuck a .toSeq at the end: originalArray.flatten.toSeq

  • Related