Scala 文档表示,数组序列的tail性能是Linear 的,而head性能是Constant。由于包含数组元素的整个块都被放入缓存中,因此我不希望看到数组的头部和尾部之间有任何差异。如果有人解释为什么 Scala 中数组的尾部性能是线性的,我很感激。
var surfaceMap = Map[Surface, Array[Event]]()
我在 Scala 中有一张地图,其中Surface
类表示由平面组成的几何表面。在Surface类中我定义equals
方法如下:
final override def equals(other: Any): Boolean = other match {
case that: Surface => (planes.deep == that.planes.deep)
case _ => false
}
final override def hashCode: Int = planes.##
Run Code Online (Sandbox Code Playgroud)
等式检查构成曲面的所有平面是否具有相同的坐标。我有一个包含 8 个元素的 surfaceMap,当我想将 an 添加Event
到地图中已有的表面时,当我检查键是否存在时,surfaceMap.contains(newSurface)
它返回 false,但当我使用surfaceMap.exists(_._1 == newSurface)
or时surfaceMap.keySet.exists(_ == newSurface)
,它需要更长的时间并返回 true。我认为.contains()
和.keySet.exists()
做同样的工作,但似乎它们是不同的,但我不明白其中的区别。任何帮助表示赞赏。