我有一个像这样的2元组的Scala数组:
(("A", "2015-11-01"), ("B", "2016-11-11"), ("A", "2017-11-01"), ("B", "2013-11-11"))
我想创建一个映射,其中键映射到最新日期。因此,在上面的示例中,结果应为:
Map ("A" -> "2017-11-01", "B" -> "2016-11-11")
我知道如何迭代地执行此操作-但是执行此操作的Scala方式(功能性方式)是什么?
最佳答案
首先按密钥分组,然后选择最新的日期。
arr
.groupBy(_._1)
.map { case (k, v) => k -> v.maxBy(_._2)._2 }
使用
mapValues
使其更短arr.groupBy(_._1).mapValues(_.maxBy(_._2)._2)
由于日期(字符串)的格式正确,因此最大日期是最新日期。您无需将日期转换成以毫秒为单位的时间来确定最大日期。
斯卡拉REPL
scala> val arr = Array(("A", "2015-11-01"), ("B", "2016-11-11"), ("A", "2017-11-01"), ("B", "2013-11-11"))
arr: Array[(String, String)] = Array((A,2015-11-01), (B,2016-11-11), (A,2017-11-01), (B,2013-11-11))
scala> :paste
// Entering paste mode (ctrl-D to finish)
arr
.groupBy(_._1)
.map { case (k, v) => k -> v.maxBy(_._2)._2 }
// Exiting paste mode, now interpreting.
res0: scala.collection.immutable.Map[String,String] = Map(A -> 2017-11-01, B -> 2016-11-11)
日期转换不是必需的,但是如果您要转换日期,请继续。
日期转换:
//ensure correct date format is given to this method if not it will throw match error at runtime.
def convertStringDateToMillis(str: String): Long = {
val regex = "(\\d{4})-(\\d{2})-(\\d{2})".r.unanchored
val regex(year, month, day) = str
val calendar = Calendar.getInstance()
calendar.clear()
calendar.set(Calendar.MONTH, month.toInt)
calendar.set(Calendar.YEAR, year.toInt)
calendar.set(Calendar.DAY_OF_MONTH, month.toInt)
calendar.getTimeInMillis();
}
解:
val arr = Array(("A", "2015-11-01"), ("B", "2016-11-11"), ("A", "2017-11-01"), ("B", "2013-11-11"))
arr.groupBy(_._1).map { case (k, v) => k -> v.maxBy(convertStringDateToMillis(_._2))._2 }
斯卡拉REPL
scala> def convertStringDateToMillis(str: String): Long = {
| val regex = "(\\d{4})-(\\d{2})-(\\d{2})".r.unanchored
| val regex(year, month, day) = str
| val calendar = Calendar.getInstance()
| calendar.clear()
| calendar.set(Calendar.MONTH, month.toInt)
| calendar.set(Calendar.YEAR, year.toInt)
| calendar.set(Calendar.DAY_OF_MONTH, month.toInt)
| calendar.getTimeInMillis();
| }
convertStringDateToMillis: (str: String)Long
scala> val arr = Array(("A", "2015-11-01"), ("B", "2016-11-11"), ("A", "2017-11-01"), ("B", "2013-11-11"))
arr: Array[(String, String)] = Array((A,2015-11-01), (B,2016-11-11), (A,2017-11-01), (B,2013-11-11))
scala> arr.groupBy(_._1).map { case (k, v) => k -> v.maxBy(x => convertStringDateToMillis(x._2))._2 }
res3: scala.collection.immutable.Map[String,String] = Map(A -> 2017-11-01, B -> 2016-11-11)