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
本文深入探讨了使用Scala进行Map操作的细节,包括如何利用flatMap、sort、zipWithIndex等函数处理数据,以及如何通过更新Map来维护数据结构。具体展示了在RDD[Array[Item]]上进行map操作时,如何动态更新HashMap中的数据。
726

被折叠的 条评论
为什么被折叠?



