You need not convert to Boolean type
product.get("inStock").map(_.forall(_ == "true")).getOrElse(false)
Beware below line can throw exception if inStock
key is not present in the map.
product("inStock").forall(_ == "true")
Scala REPL
scala> val product = Map("ids" -> List("1" ,"2", "3"), "inStock" -> List("true", "false", "true", "true"))
product: Map[String, List[String]] = Map("ids" -> List("1", "2", "3"), "inStock" -> List("true", "false", "true", "true"))
scala> import scala.util._
import scala.util._
scala> product.get("inStock").map(_.forall(_ == "true")).getOrElse(false)
res7: Boolean = false
scala> product.get("foo").map(_.forall(_ == "true")).getOrElse(false)
res8: Boolean = false
Lurking Danger with NoSuchElementException
scala> product("foo").forall(_ == "true")
java.util.NoSuchElementException: key not found: foo
scala.collection.MapLike$class.default(MapLike.scala:228)
scala.collection.AbstractMap.default(Map.scala:59)
scala.collection.MapLike$class.apply(MapLike.scala:141)
scala.collection.AbstractMap.apply(Map.scala:59)
$sess.cmd9$.<init>(cmd9.sc:1)
$sess.cmd9$.<clinit>(cmd9.sc:-1)
Fool proof way
scala> product.get("foo").map(_.forall(_ == "true")).getOrElse(false)
res7: Boolean = false
More typesafe
product.get("inStock")
.toSeq.flatten.map(elem => Try(elem.toBoolean))
.collect { case Success(value) => value}
.reduce(_ && _)
for comprehension
Use the fact that Option is an iterable.
(for {
list <- product.get("inStock").iterator
elem <- list.iterator
result <- Try(elem.toBoolean).toOption.iterator
} yield result).reduce(_ && _)