Why function getTopNPricedProducts returns more than the numbers defined by the parameter


#1

I repeated the script available at: https://gist.github.com/dgadiraju/a60322146d5aa654acb280a3400fa850

getTopNPricedProducts(productsIterable, 3).foreach(println)
returns 12 items:
743,34,Ogio Race Golf Shoes,169.99,http://images.acmesports.sports/Ogio+Race+Golf+Shoes
744,34,Ogio Race Golf Shoes,169.99,http://images.acmesports.sports/Ogio+Race+Golf+Shoes
745,34,Ogio City Spiked Golf Shoes,149.99,http://images.acmesports.sports/Ogio+City+Spiked+Golf+Shoes
746,34,Ogio City Spiked Golf Shoes,149.99,http://images.acmesports.sports/Ogio+City+Spiked+Golf+Shoes
747,34,Ogio City Spiked Golf Shoes,149.99,http://images.acmesports.sports/Ogio+City+Spiked+Golf+Shoes
754,34,TRUE linkswear Lyt Dry Golf Shoes,149.99,http://images.acmesports.sports/TRUE+linkswear+Lyt+Dry+Golf+Shoes
755,34,TRUE linkswear Lyt Dry Golf Shoes,149.99,http://images.acmesports.sports/TRUE+linkswear+Lyt+Dry+Golf+Shoes
756,34,TRUE linkswear Lyt Dry Golf Shoes,149.99,http://images.acmesports.sports/TRUE+linkswear+Lyt+Dry+Golf+Shoes
759,34,Nike Lunarwaverly Golf Shoes,139.99,http://images.acmesports.sports/Nike+Lunarwaverly+Golf+Shoes
760,34,Nike Lunarwaverly Golf Shoes,139.99,http://images.acmesports.sports/Nike+Lunarwaverly+Golf+Shoes
761,34,Nike Lunarwaverly Golf Shoes,139.99,http://images.acmesports.sports/Nike+Lunarwaverly+Golf+Shoes
762,34,Nike Lunarwaverly Golf Shoes,139.99,http://images.acmesports.sports/Nike+Lunarwaverly+Golf+Shoes

scala> getTopNPricedProducts(productsIterable, 3).size
res26: Int = 12

Can anyone explain this? presumably the parameter topN is 3 so 3 items should be shown in the result as per the function’s definition, how come 12 items are returned?

Thank you very much.


#2

I continued the lecture and tried out the next one which has the script here:

The function getTopNPricedProducts is used again there.

val top3PricedProductsPerCategory = productsGroupByCategory.flatMap(rec => getTopNPricedProducts(rec._2, 3))
top3PricedProductsPerCategory.count = 1344

val top2ricedProductsPerCategory = productsGroupByCategory.flatMap(rec => getTopNPricedProducts(rec._2, 2))

top2PricedProductsPerCategory.count = 1344

so there is no difference between different parameters of topN, it is not filter out the topN items.

I also checked the result of top2PricedProductsPerCategory and top3PricedProductsPerCategory, they are the same data set.