带有现有密钥的NoSuchElementException

时间:2014-07-13 19:59:53

标签: scala hashmap nosuchelementexception

建筑类:

class Building(val name: String, val skill: String, @volatile var workHours: Int) {

  var workers = new HashMap[Artisan, Int]()

  def doWork(worker: Artisan): Boolean = {
    ...
    workers.get(worker) match {
      case Some(i: Int) =>
        worker.cash += i
        true
      case None => false
    }
    ...
  }

工匠班:

class Artisan(val skill: String, city: City) extends Player(skill, city) {
  var assignment = new Building("Empty", "", 0)

  def doWork() {
    if ( !assignment.doWork(this) )
    ...
  }

  def canEqual(other: Any): Boolean = other.isInstanceOf[Artisan]

  override def equals(other: Any): Boolean = other match {
    case that: Artisan =>
      (that canEqual this) &&
        assignment == that.assignment &&
        skill == that.skill
    case _ => false
  }

  override def hashCode(): Int = {
    val state = Seq(assignment, skill)
    state.map(_.hashCode()).foldLeft(0)((a, b) => 31 * a + b)
  }
}

测试用例:

@Test
def testIncome() {
  val building = new Building("Bakery", "Builder", 10)
  building.setSallery(100, player)  // add player to building hashmap
  player.assignment = building
  player.doWork()
  assertEquals("Should earn 100", player.cash, 100)
}

输出:

Negative test case

我看不出这段代码实际上是如何提供此错误的。如果元素不存在,我的代码应该返回false。通过调试程序,Artisan对象被添加到我的hashmap中,所以我不明白它为什么会失败。

1 个答案:

答案 0 :(得分:1)

在Scala中不鼓励使用return。您的错误消息看起来好像整个函数都被评估,即使有一个return语句。如果像你一样,return有几种选择:

1)使用Option

var workers = new HashMap[Artisan, Int]()

  def doWork(worker: Artisan): Boolean = {
    val maybeIncome = workers.get(worker)
    maybeIncome match {
        case None => false
        case Some(income) => {
          val result = <do something with income that returns Boolean>
          result
         }
       }
    }

2)使用for-comprehensions

var workers = new HashMap[Artisan, Int]()

  def doWork(worker: Artisan): Boolean = {
    val result = for { income <- workers.get(worker) } yield {
      <do something with income that returns boolean>
    }
    result getOrElse false
  }

更新:我看到你的Artisan类中有可变元素。使用这样的类作为键可能会产生不可预测的结果(我想知道为什么它甚至可能)。我宁愿定义两个地图:Map [A​​rtisanID,Artisan]和Map [A​​rtisanID,income],两者都有不可变的键。 (ArtisanID可以是任何能够唯一识别您的工匠并且永不改变的东西。)