Scala Concatenate Map-一个带有选项,另一个没有选项
我有以下输入值
import java.sql.Timestamp
import java.lang.{Double => JDouble}
val date = Timestamp.valueOf("2021-08-01 00:00:00")
val contractRate: Map[String, JDouble] = Map("ITABUS" -> 0.075,
"KARAT-S" -> 0.10,
"KAUTRA" -> 0.05)
val timeBoundContractRatesList: Map[String, List[(Timestamp, JDouble)]] = Map(
"ITABUS" -> List((Timestamp.valueOf("2021-07-30 23:59:59"), 0.085.asInstanceOf[JDouble]),
)
)
我的要求是:
有 2 种类型的费率。一种是固定利率,另一种是有时限利率
如果日期大于今天(例如),我需要应用时间限制利率
我正在尝试使用该方法来获得如下所示的单个综合地图
val withTimeBoundContractRate = ContractRate ++ timeBoundContractRatesList .map { case (运营商, timeRateSet) => val 过滤条目 = timeRateSet .filter { case (startDate, _) =>;日期.after(开始日期) } (运营商,filteredEntry.map(_._2).headOption) } .filter(_._2.nonEmpty)
问题在于输出。我得到以下输出
withTimeBoundContractRate: scala.collection.immutable.Map[String,java.io.Serializable] = Map(ITABUS -> Some(0.085), KARAT-S -> 0.1, KAUTRA -> 0.05)
但我正在寻找的是具有原始数据类型的地图(没有选项)
withTimeBoundContractRate: Map[String, JDouble] = Map(ITABUS -> 0.085, KARAT-S -> 0.1, KAUTRA -> 0.05)
或者是否有完全不同的方法来有效地解决这个问题?
I have the below input values
import java.sql.Timestamp
import java.lang.{Double => JDouble}
val date = Timestamp.valueOf("2021-08-01 00:00:00")
val contractRate: Map[String, JDouble] = Map("ITABUS" -> 0.075,
"KARAT-S" -> 0.10,
"KAUTRA" -> 0.05)
val timeBoundContractRatesList: Map[String, List[(Timestamp, JDouble)]] = Map(
"ITABUS" -> List((Timestamp.valueOf("2021-07-30 23:59:59"), 0.085.asInstanceOf[JDouble]),
)
)
My requirement here is:
There are 2 types of rates. One is fixed rate and other is time bound rate
I need to apply the time bound rate if the date is greater than today (for example)
I am trying with the approach to have a single consolidated Map like below
val withTimeBoundContractRate = contractRate ++ timeBoundContractRatesList .map { case (carrier, timeRateSet) => val filteredEntry = timeRateSet .filter { case (startDate, _) => date.after(startDate) } (carrier, filteredEntry.map(_._2).headOption) } .filter(_._2.nonEmpty)
The problem is with the output. I get the below output
withTimeBoundContractRate: scala.collection.immutable.Map[String,java.io.Serializable] = Map(ITABUS -> Some(0.085), KARAT-S -> 0.1, KAUTRA -> 0.05)
But what I am looking for is a Map with original datatype(without Option)
withTimeBoundContractRate: Map[String, JDouble] = Map(ITABUS -> 0.085, KARAT-S -> 0.1, KAUTRA -> 0.05)
Or is there a totally different approach to solve this efficiently?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(我认为,我误解了您最初要做的事情,所以我用第一个答案删除了它,以此为此替换它):
您实际上几乎拥有它,唯一的问题是第二张映射中的值是
options
。只是“解开”它们:摘要的主要区别是使用
收集
而不是filter
:它不仅可以删除空值,还可以转换非 - 空的人要摆脱选项
。(I think, I misunderstood what you are trying to do originally, so I deleted by first answer, to replace it with this):
You actually almost have it, the only problem is that the values in your second map are
Options
. Just "unwrap" them:The main difference from your snippet here is to use
collect
instead offilter
: it lets you not only remove the empty values, but also transform the non-empty ones to get rid of theOption
around them.