小编use*_*292的帖子

Scala List.contains(x)返回false,但exists(_.== x)返回true

我在Scala中使用一些简单的数据结构和集合,我注意到我认为是奇怪的行为.这是对象:

class State (protected val trackmap: Map[Int, List[String]]) {

  override def clone : State = {
    new State(Map() ++ trackmap)
  }

  override def toString = { "State: " + trackmap.toString }

  def equals (other: State) : Boolean = {
    //println("Comparing " + trackmap + " to " + other.trackmap)
    trackmap == other.trackmap  

  }

  def == (other: State) : Boolean = {
    this equals other
  }
}
Run Code Online (Sandbox Code Playgroud)

我的相关测试:

  test("state equality") {
    val state = new State( Map(1 -> List("engine"), 2 -> …
Run Code Online (Sandbox Code Playgroud)

collections scala contains list exists

4
推荐指数
1
解决办法
6559
查看次数

标签 统计

collections ×1

contains ×1

exists ×1

list ×1

scala ×1