如何测试Map [String,List [Int]]中列表中的所有元素是否都存在负数?

时间:2014-12-29 13:33:37

标签: scala

如何检查所有列表元素是否为负数 - 如果任何单个值为正值,则返回false其他true

scala> val checkNegative = Map(
     |   "A" -> List(-1205678557, -1206583677, -1208669605, -1205679913),
     |   "B" -> List(-396902501, -397202715, -396902501, -396902501, -396902501),
     |   "C" -> List(-397502289, -397502289, -397502289, -397502289, -397502289),
     |   "D" -> List(-33902725, -33902725, -412803077, -33902725),
     |   "E" -> List(-458008664, -30433317),
     |   "F" -> List(300244, 300244, 300244, -396901292, 300244)
     | )
checkNegative: scala.collection.immutable.Map[String,List[Int]] = Map(E -> List(-458008664, -30433317), F -> List(300244, 300244, 300244, -396901292, 300244), A -> List(-1205678557, -1206583677, -1208669605, -1205679913), B -> List(-396902501, -397202715, -396902501, -396902501, -396902501), C -> List(-397502289, -397502289, -397502289, -397502289, -397502289), D -> List(-33902725, -33902725, -412803077, -33902725))

// How to get the value of `output`?
val output = Map(A -> true, B -> true, C -> true, D -> true, E -> true, F -> false)

2 个答案:

答案 0 :(得分:14)

val output = checkNegative.mapValues(_.forall(_ < 0))

答案 1 :(得分:2)

val output = for((key, value) <- checkNegative) yield (key, !value.exists(_ > 0))