Scala 入门笔记-方法备忘

list 操作

val list1 = List("hello java", "hello scala hello c", "hi c++")

// 空格分隔+压平
val list2 = list1.split(" ").flatten
val list3 = list.flatMap(_.split(" ")

// 平行计算的求和
val arr = Array(1, 2, 3, 4, 5, 6, 7)

  // 和线程有关,每个线程计算一部分:((1+2)+(3+4+5)+(6+7))
val res = arr.par.sum

// reduce
val res2 = arr.reduce(_+_)
val res3 = arr.reduceLeft(_+_)

val res4 = arr.par.reduce(_+_)  // 并行在多线程里面运行


// fold
arr.fold(10)(_-_)


// 聚合 list 里面有list
val list = List(List(1, 2, 3), List(3, 4, 5), List(4, 8, 9))
list.flatten.reduce(_+_)
list.aggregate(0)(_+_.sum, _+_)

-----------------------

val l1 = List(5, 6, 4, 7)
val l2 = List(1, 2, 3, 4)
// 并集
l1 union l2

// 交集
l1 intersect l2

// 差集
l1 diff l2 

  

猜你喜欢

转载自www.cnblogs.com/sunnystone85/p/11357408.html
今日推荐