First groupBy key and then pick latest Date.
arr
.groupBy(_._1)
.map { case (k, v) => k -> v.maxBy(_._2)._2 }
use mapValues
to make it shorter
arr.groupBy(_._1).mapValues(_.maxBy(_._2)._2)
As date (string) is formatted properly max date is the latest date. You need not convert date into time in millis to decide the max date.
Scala 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)
date conversion is not needed but if you wish to convert it then go ahead.
date conversion:
//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();
}
Solution:
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 }
Scala 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)