Home > Blockchain >  How to convert Option[List[String]] to List[String] in Scala?
How to convert Option[List[String]] to List[String] in Scala?

Time:06-16

I am having Option of List of String like, Some(List("abc","def","ghi")) and I need to convert to List("abc","def","ghi") in Scala. I Mean Option[List[String]] to List[String].

CodePudding user response:

You should check the documentation for Option. You will find everything you'll need there.

Here are 2 ideas:

  val optlist = Some(List("abc", "def", "ghi"))
  val list = if (optlist.isDefined) optlist.get else Nil
  val list2 = optlist.getOrElse(Nil)
  println(list)
  println(list2)

Output:

List(abc, def, ghi)
List(abc, def, ghi)
  • Related