在RDD.map操作中“修改外部声明的变量”是无效的

val heightMap=mutable.HashMap[Int,mutable.ArrayBuffer[Double]](1->3.5,2->4.8)

val filteredData =transactions.map{ transaction=>
      val filtered=transaction.flatMap(itemToRank.get)
      ju.Arrays.sort(filtered)
      val tmp: Map[Int, Int] =filtered.zipWithIndex.toMap //Int型的频繁项:频繁项在freqItems中的下标。Map(Int型的频繁项,频繁项在filtered中的下标)
      tmp.foreach{case (item,height)=>
        val heightArr =heightMap.getOrElseUpdate(item,mutable.ArrayBuffer[Double]())
        heightArr+=height
        heightMap(item)=heightArr
        if(heightMap(item).length>=2)
            heightMap(item)=mutable.ArrayBuffer(heightMap(item).max)
      }
      filtered
    } //transactions: RDD[Array[Item]]

就算在上述transactions的map操作中有往heightMap添加元素的操作,最后heightMap的值依然为1->3.5,2->4.8

发布了94 篇原创文章 · 获赞 20 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/helloworld0906/article/details/103242130