一、apply、make、range代碼實戰
println(List.apply(1,2,3)) //1 2 3構成一個集合
println(List.make(3,5)) //5出現3次
println(List.range(1,5)) //左閉右開
println(List.range(9,1,-3)) //左閉右開,步長爲-3
輸出:
List(1, 2, 3)
List(5,5,5)
List(1, 2, 3, 4)
List(9, 6, 3)
二、unzip、flatten、contact、map2代碼實戰
val zipped = "abcde".toList zip List(1,2,3,4,5) //拉鍊操作
println(zipped)
println(zipped.unzip) //拉鍊操作的逆向操作
println
println(List(List('a', 'b'), List('c'), List('d', 'e')).flatten) //把所有集合中的元素放在一起,構成一個大的集合
println(List.concat(List(), List('b'), List('c'))) //作用同flatten
println
println(List.map2(List(10,20), List(10,10)) (_ * _))
//map2可以同時對兩個集合進行map操作
//(_ * _)代表第一個集合相應位置的元素和第二個集合相應位置的元素相乘
輸出:
List((a,1), (b,2), (c,3), (d,4), (e,5))
(List(a, b, c, d, e),List(1, 2, 3, 4, 5))
List(a, b, c, d, e)
List(b, c)
List(100,200)
參考資料來源於大數據夢工廠 深入淺出scala 第38講 由王家林老師講解