我有一个数组
val bins = Array(0,100, 250, 500, 1000, 2000, 3000)
这是我的代码:
private var cumulativeDelay:Map[String ,Double] = linkIds.zip(freeFlowDelay).groupBy(_._1).mapValues(l => l.map(_._2).sum)
private var cumulativeCapacity:Map[String , Double] = linkIds.zip(linkCapacity).groupBy(_._1).mapValues(l => l.map(_._2).sum)
cumulativeCapacity foreach {
case(linkId , capacity) => {
val rangeToValue = bins.zip(bins.tail)
.collectFirst { case (left, right) if capacity >= left && capacity <= right =>
Map(s"$left-$right" -> cumulativeDelay.get(linkId))
}
.getOrElse(Map.empty[String, Double])
}
}
所以rangeToValue
的值就像Map(1000-2000-> Some(625))一样,但是我要rangeToValue:Map [String,Double] =(1000-2000-> 625)
答案 0 :(得分:1)
您应该尝试这样的操作,但不适用于超出范围的值:
val bins = Array(0, 100, 250, 500, 1000, 2000, 3000)
val effectiveValue = 625
val rangeToValue = bins.zip(bins.tail)
.collectFirst { case (left, right) if effectiveValue >= left && effectiveValue <= right =>
Map(s"$left-$right" -> effectiveValue)
}
.getOrElse(Map.empty[String, Int])
rangeToValue("500-1000")