循环后不保留Scala - HashMap元素

时间:2018-04-20 17:04:44

标签: scala apache-spark

我有一个名为countSumMeanVariance2的RDD,类型结构如下:

(" Key",(4,123,30.75,2.06))

我想跟踪最后两个值:_._ 2._3和_._ 2._4将它们更新为HashMap变量(因为我将运行for循环并每次更改这些值。)

我目前正在做的更新HashMaps的方法如下:

val meanHashMap = scala.collection.mutable.Map[String,Float]();

for (c <- countSumMeanVariance2) {
    meanHashMap.update(c._1, c._2._3);
    println("c._1: " + c._1);
    println("c._2._3: " + c._2._3);
    println("meanHashMap(c._1): " + meanHashMap(c._1));
    println("meanHashMap size: " + meanHashMap.size);
}

println("meanHashMap size after for: " + meanHashMap.size);

我发现很奇怪的是在循环之后,HashMap不会持久化值。循环中的println可以告诉更新的hashmap的大小,但是循环后的println显示大小被设置回零。

...

c._1: "GEORGIA"
c._2._3: 53.6605
meanHashMap(c._1): 53.6605
meanHashMap size: 46

c._1: "VIRGINIA"
c._2._3: 55.478752
meanHashMap(c._1): 55.478752
meanHashMap size: 47
c._1: "ILLINOIS"
c._2._3: 154.02426
meanHashMap(c._1): 154.02426
meanHashMap size: 48

meanHashMap size after for: 0

任何人都知道为什么?你能帮我解释一下并建议更新hashmap值的方法吗?

0 个答案:

没有答案