1. 程式人生 > >scala常用操作

scala常用操作

跳迴圈

        breakable {
          lst foreach {
            y =>
              name = y(8)
              break
          }
        }

遍歷CompactBuffer

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))
shareimprove this answer