1. 程式人生 > >How to transform a CompactBuffer to List

How to transform a CompactBuffer to List

You can simply change CopmpactBuffer to list with toList method

val r_grp = rm.groupByKey
// After groupBykey
(2,CompactBuffer(Bhupesh, Amit, Ratan, Dinesh, Sheela))
(1,CompactBuffer(Lokesh, Pavan, Tejas, Kumar, Venkat))

Simply use toList to convert to List and sortBy to sort it

val com = r_grp.map(x => (x._1, x._2.toList.sortBy(x => x)))
com.foreach(println)

Output :

(1,List(Kumar, Lokesh, Pavan, Tejas, Venkat))
(2,List(Amit, Bhupesh, Dinesh, Ratan, Sheela))