Scala使用toSet.toList vs distinct

时间:2014-05-08 20:49:48

标签: list scala collections set distinct

如果我想获取列表中的唯一元素,我可以distinct或致电toSet.toList。哪个更有效,为什么?还有其他有效的方法吗?我的理解是distinct也会保持秩序,而toSet.toList会赢。

scala> val mylist = List(1,2,3,3,4,4,4,5,6,6,6,6,7)
mylist: List[Int] = List(1, 2, 3, 3, 4, 4, 4, 5, 6, 6, 6, 6, 7)

scala> mylist.distinct
res11: List[Int] = List(1, 2, 3, 4, 5, 6, 7)

scala> mylist.toSet.toList
res12: List[Int] = List(5, 1, 6, 2, 7, 3, 4)

1 个答案:

答案 0 :(得分:7)

直接从找到here的源代码中获取:

/** Builds a new $coll from this $coll without any duplicate elements.
* $willNotTerminateInf
*
* @return A new $coll which contains the first occurrence of every element of this $coll.
*/
  def distinct: Repr = {
    val b = newBuilder
    val seen = mutable.HashSet[A]()
    for (x <- this) {
      if (!seen(x)) {
        b += x
        seen += x
      }
    }
    b.result
  }

所以看来,如果订单保存很重要,请使用distinct,否则它们的价格相对较高。