Scala:可变HashMap不会在for循环内更新 [英] Scala: mutable HashMap does not update inside for loop

查看:780
本文介绍了Scala:可变HashMap不会在for循环内更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个全局定义的 var permutedTables = HashMap [List [Int],List [String,String]]

  print(permutedTables):
Map( List(1,2,3,4) - > List(),
List(2,4,5,6) - > List()等等...)

当我想要更新for循环中的HashMap的值(空列表)时(第二个方法内),会出现问题。换句话说,我想在List()中为每个键添加元组(String,String)。

  for(pi_k <  -  permutedTables.keySet){
var mask = emptyMask;
mask = pi_k.foldLeft(mask)((s,i)=> s.updated(i,'1'))
val maskB = Integer.parseInt(mask,2)

val permutedFP =(intFP& maskB).toBinaryString

//尝试1:
// permutedTables(pi_k):+(url,permutedFP)
//尝试2:
// permutedTables.update(pi_k,permutedTables(pi_k)::: List((url,permutedFP)))

}

这些值不会更新。我仍然有空列表作为值。我不明白我的代码有什么问题。编辑1:当我调用 print(permutedTables)时, 在两次尝试(在循环内)之后,值似乎更新了,但是当我在循环之外调用它时,列表是空的



编辑2:我的代码中的第二次尝试似乎现在工作(!)。但为什么先不行?

解决方案


我的代码中的第二次尝试似乎现在工作!)。但是为什么首先不能工作?


因为你在第一种情况下做的是从获得一个列表permutedTables ,添加一个元素并丢弃结果而不存储它。它会工作,如果你突变的值,但 List 是不可变的。使用 List ,您需要

  permutedTables + = pi_k  - > permutedTables(pi_k):+(url,permutedFP)

或者,如您所见, update



您可以使用eg ArrayBuffer ListBuffer 代替您的值类型(请注意,您需要:+ = 而不是:+ 来改变它们),并在最后转换为你想要的类型。这将比追加到列表的末尾更有效率,除非列表非常小!

最后,请注意,您通常需要 var 或可变类型,不能同时出现。


I have a var permutedTables = HashMap[List[Int], List[String, String]] defined globally. I first populate the Hashmap with the keys in a method, which works.

print(permutedTables) :
Map(List(1,2,3,4) -> List(),
List(2,4,5,6) -> List(), etc...)

The problem occurs when I want to update the values (empty lists) of the HashMap inside a for loop (inside a second method). In other words, I want to add tuples (String, String) in the List() for each key.

for(pi_k <- permutedTables.keySet){
      var mask = emptyMask;
      mask = pi_k.foldLeft(mask)((s, i) => s.updated(i, '1'))
      val maskB = Integer.parseInt(mask,2)

      val permutedFP = (intFP & maskB).toBinaryString

     // attempt 1 :
     // permutedTables(pi_k) :+ (url, permutedFP)
     // attempt 2 : 
     // permutedTables.update(pi_k, permutedTables(pi_k):::List((url, permutedFP)))

    }

The values do not update. I still have empty lists as values. I don't understand what is wrong with my code.

EDIT 1: When I call print(permutedTables) after any of the two attempts (inside the loop), the value seem updated, but when I call it outside of the loop, the Lists are empty

EDIT 2: The second attempt in my code seems to work now(!). But why does first not work ?

解决方案

The second attempt in my code seems to work now(!). But why does first not work ?

Because what you do in the first case is get a list from permutedTables, add an element and throw away the result without storing it back. It would work if you mutated the value, but a List is immutable. With List, you need

permutedTables += pi_k -> permutedTables(pi_k) :+ (url, permutedFP)

Or, as you saw, update.

You can use e.g. ArrayBuffer or ListBuffer as your value type instead (note that you need :+= instead of :+ to mutate them), and convert to your desired type at the end. This is going to be rather more efficient than appending to the end of the list, unless the lists are quite small!

Finally, note that you generally want either var or a mutable type, not both at the same time.

这篇关于Scala:可变HashMap不会在for循环内更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆