//创建一个List
val lst0 = List(1,7,9,8,0,3,5,4,6,2)
//将lst0中每个元素乘以10后生成一个新的集合
val lst1 = lst0.map(_*10)
println(lst1)
//将lst0中的偶数取出来生成一个新的集合
val lst2 = lst0.filter(_%2 ==0)
//将lst0排序后生成一个新的集合
val lst3 = lst0.sorted
//反转顺序
val lst4 = lst0.sorted.reverse
//将lst0中的元素4个一组,类型为Iterator[List[Int]]
val lst5 = lst0.grouped(4)
//将Iterator转换成List
val lst6 = lst5.toList
//将多个list压扁成一个List
val lst7 = lst6.flatten
val lines = List("hello tom hello jerry", "hello jerry", "hello kitty")
//先按空格切分,在压平
val lst8= lines.map(_.split(" ")).flatten
//并行计算求和
val lst9 =lst0.par.reduce(_+_)
val lst10 = lst0.par.fold(0)(_+_)
//化简:reduce
//将非特定顺序的二元操作应用到所有元素
val lst11= lst0.fold(0)(_+_)
//安装特点的顺序
val lst12= lst0.foldLeft(0)(_+_)
//折叠:有初始值(无特定顺序)
val lst13 = lst0.par.fold(10)(_+_)
val lst14 = lst0.fold(10)((x,y ) => x+y)
//折叠:有初始值(有特定顺序)
val lst15 = lst0.par.foldLeft(10)(_+_)
val lst16 = lst0.foldLeft(10)((x,y)=>x+y)
//聚合
val arr = List(List(1, 2, 3), List(3, 4, 5), List(2), List(0))
//聚合操作, _+_.sum 初始值+集合中第一个元素的sum
// _+_ 各个元素的sum,依次相加求和
val i = arr.aggregate(0)(_+_.sum,_+_)
val l1 = List(5,6,4,7)
val l2 = List(1,2,3,4)
//求并集
val ls0 = l1.union(l2)
//求交集
val ls1 = l1.intersect(l2)
//求差集
val ls2 = l1.diff(l2)
val lines = List("hello tom hello jerry", "hello jerry", "hello kitty")
//wordcount
lines.flatMap(_.split(" ")).map((_, 1)).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2))
lines.flatMap(_.split(" ")).map((_, 1)).groupBy(_._1).map(t=>(t._1, t._2.size)).toList.sortBy(_._2).reverse
网友评论