Scala-如何过滤嵌套的集合结构?

时间:2019-07-23 16:33:11

标签: scala collections

这是我的案例类:

case class Metric (
id: Long,
name: String,
features: List[Feature]
)

case class Feature (
featureId: Long,
name: String,
value: String,
processTime: Timestamp
)

每个指标都有一个List[Feature]。我想过滤每个指标,以便其List[Feature]仅包含每个featureId的最新功能。

我尝试了以下操作,但是它会返回List[immutable.Iterable[Feature]],其中正确过滤了功能。但是我需要它来重新启动List[Metric],其中包含过滤的功能列表。

val f1 = Feature(1, "f1", "v1", Timestamp.valueOf("2019-07-01 00:00:00"))
val f2 = Feature(1, "f2", "v2", Timestamp.valueOf("2019-07-05 00:00:00"))
val f3 = Feature(2, "f3", "v3", Timestamp.valueOf("2019-03-07 00:00:00"))
val f4 = Feature(2, "f4", "v4", Timestamp.valueOf("2019-03-10 00:00:00"))

val metric1 = Metric(1, "m1", List(f1, f2, f3, f4))
val metric2 = Metric(1, "m1", List(f3, f4))

val metricsList = List(metric1, metric2)

val newMetrics = metricsList.map(m => m.features.groupBy(_.featureId)
  .map { case (featureId, metricsList) => metricsList.reduce {
    (m1: Feature, m2: Feature) => if (m1.processTime.after(m2.processTime)) m1 else m2
  }
  })

UPD::预期输出为List(metric1, metric2),其中

val metric1 = Metric(1, "m1", List(f2,f4))val metric2 = Metric(1, "m1", List(f4))

1 个答案:

答案 0 :(得分:2)

您可以在copy类上使用案例类Metric方法来执行此操作。这将创建具有过滤功能的Metric的新实例。请注意,您也可以使用maxBy,因此不需要使用reduce。为此,您需要提供一个Ordering隐式排序时间戳。下面的代码应该可以满足您的需求:

implicit def ordered: Ordering[Timestamp] = new Ordering[Timestamp] {
  def compare(x: Timestamp, y: Timestamp): Int = x compareTo y
}

val newMetrics = metricsList.map(m => {
  val features = m.features.groupBy(_.featureId).mapValues(_.maxBy(_.processTime)).values
  m.copy(features = features.toList)
})