【问题标题】:Scala: Which is better - distinct then union or union then distinct?Scala:哪个更好 - distinct then union 或 union then distinct?
【发布时间】:2016-01-09 03:51:18
【问题描述】:

如果我有两个列表:

  • listA - 100 万个字符串,
  • listB - 100 万个字符串

并且我想将它们合并到第三个 listC 中,该 listC 仅具有上述两个列表中的唯一值,以下哪种方法更好:

  1. 在联合到 listC 或之前,对 listA 和 listB 进行 distinct(),
  2. 生成联合的 listC,然后在 listC 上应用 distinct

同样的逻辑也适用于数组吗?

【问题讨论】:

  • 附注更多,但方法 1 不保证 listC 仅包含不同的值,因为 listA 和 listB 之间的重复项将作为合并两个列表的一部分保留。您还需要使用选项 1 对结果 listC 执行 distinct 以保证结果列表中的不同值。

标签: arrays list scala distinct union


【解决方案1】:

让我们看看distinct的实现:

def distinct: Repr = {
  val b = newBuilder
  val seen = mutable.HashSet[A]()
  for (x <- this) {
    if (!seen(x)) {
      b += x
      seen += x
    }
  }
  b.result()
}

出于性能原因,它使用可变结构。

因此,如果性能是一个问题,您可以以相同的方式实现您的独特联合:

def distinctUnion[E](listA: Seq[E], listB: Seq[E]): Seq[E] = {
  val b = Seq.newBuilder[E]
  val seen = mutable.HashSet[E]()
  for (x <- listA) {
    if (!seen(x)) {
      b += x
      seen += x
    }
  }
  for (x <- listB) {
    if (!seen(x)) {
      b += x
      seen += x
    }
  }
  b.result()
}

【讨论】:

    【解决方案2】:

    您需要先进行联合,但您希望以一种懒惰的方式执行此操作,以避免在内存中创建联合集合。像这样的东西应该可以工作:

    val listC = (listA.view union listB).distinct.toList
    

    从计算上讲,这可能最终会做一些与 Jean 发布的非常相似的事情,但它更好一点,因为它更好地利用了 Scala 集合库。

    【讨论】:

      猜你喜欢
      • 2015-05-19
      • 2011-10-21
      • 1970-01-01
      • 2019-12-09
      • 2018-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多