Scala Concatenate Map-一个带有选项,另一个没有选项

发布于 2025-01-18 16:09:00 字数 1330 浏览 2 评论 0原文

我有以下输入值

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]),
    )
  )

我的要求是:

  1. 有 2 种类型的费率。一种是固定利率,另一种是有时限利率

  2. 如果日期大于今天(例如),我需要应用时间限制利率

  3. 我正在尝试使用该方法来获得如下所示的单个综合地图

    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:

  1. There are 2 types of rates. One is fixed rate and other is time bound rate

  2. I need to apply the time bound rate if the date is greater than today (for example)

  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风不识月 2025-01-25 16:09:00

(我认为,我误解了您最初要做的事情,所以我用第一个答案删除了它,以此为此替换它):

您实际上几乎拥有它,唯一的问题是第二张映射中的值是options。只是“解开”它们:

contractRate ++ timeBoundContractRatesList.mapValues { 
  _.find(date.after(_._1)).map(_._2)
}.collect { case(k, Some(v)) => k -> v }

摘要的主要区别是使用收集而不是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:

contractRate ++ timeBoundContractRatesList.mapValues { 
  _.find(date.after(_._1)).map(_._2)
}.collect { case(k, Some(v)) => k -> v }

The main difference from your snippet here is to use collect instead of filter: it lets you not only remove the empty values, but also transform the non-empty ones to get rid of the Option around them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文