大数据系列修炼-Scala课程38
2016-09-20 21:15
267 查看
大数据系列修炼-Scala课程38
核心内容:
1、List伴生对象操作方法代码实战
List的object(伴生对象)中常用的方法:
1>apply:构造一个List实例对象
2>range:指定一个范围是左闭右开的List对象,同时可以指定两个元素之间的步长
3>zip:拉链操作、unzip:反拉链操作、将之前zip获得的集合结果进行反操作、生成两个List集合
4>flatten与concat:将内部集合中的所有元素联合构成一个新的集合,即统一构成一个List
实例程序1:
核心内容:
1、List伴生对象操作方法代码实战
1、List伴生对象操作方法代码实战 |
1>apply:构造一个List实例对象
2>range:指定一个范围是左闭右开的List对象,同时可以指定两个元素之间的步长
3>zip:拉链操作、unzip:反拉链操作、将之前zip获得的集合结果进行反操作、生成两个List集合
4>flatten与concat:将内部集合中的所有元素联合构成一个新的集合,即统一构成一个List
实例程序1:
object regex { //apply //apply:生成一个List对象 val list = List.apply(10,20,30) //> list : List[Int] = List(10, 20, 30) val list1 = list.map((x:Int)=>x+10) //> list1 : List[Int] = List(20, 30, 40) println(list1) //> List(20, 30, 40) val list2 = List(10,20,30) //> list2 : List[Int] = List(10, 20, 30) //range val list3 = List.range(1 , 5) //> list3 : List[Int] = List(1, 2, 3, 4) println(list3.mkString("[", "\t", "]")) //> [1 2 3 4] println(List.range(10, 1, -2)) //> List(10, 8, 6, 4, 2) //zip的拉链操作与unzip的反拉链操作 val list4 = "spark".toList //> list4 : List[Char] = List(s, p, a, r, k) val list5 = List(1,2,3,4,5) //> list5 : List[Int] = List(1, 2, 3, 4, 5) val list6 = list4.zip(list5) //> list6 : List[(Char, Int)] = List((s,1), (p,2), (a,3), (r,4), (k,5)) println(list6) //> List((s,1), (p,2), (a,3), (r,4), (k,5)) println(list6.mkString("[", "\t", "]")) //> [(s,1) (p,2) (a,3) (r,4) (k,5)] val (a,b) = list6.unzip //> a : List[Char] = List(s, p, a, r, k) //| b : List[Int] = List(1, 2, 3, 4, 5) println(a) //> List(s, p, a, r, k) println(b) //> List(1, 2, 3, 4, 5) //flatten与concat:将集合中的所有元素联合构成一个新的集合 val list7 = List(List("spark","hadoop"),List(10,20,30),List(80.8,98.8)).flatten //> list7 : List[Any] = List(spark, hadoop, 10, 20, 30, 80.8, 98.8) println(list7) //> List(spark, hadoop, 10, 20, 30, 80.8, 98.8) val list8 = List.concat(List("spark","hadoop"),List(10,20,30),List(80.8,98.8)) //> list8 : List[Any] = List(spark, hadoop, 10, 20, 30, 80.8, 98.8) println(list8) //> List(spark, hadoop, 10, 20, 30, 80.8, 98.8) //map2操作:map2可以同时对两个集合执行map操作 }
相关文章推荐
- 大数据系列修炼-Scala课程23
- 大数据系列修炼-Scala课程30
- 大数据系列修炼-Scala课程44
- 大数据系列修炼-Scala课程03
- 大数据系列修炼-Scala课程05
- 大数据系列修炼-Scala课程96(2)
- 大数据系列修炼-Scala课程26
- 大数据系列修炼-Scala课程01
- 大数据系列修炼-Scala课程09
- 大数据系列修炼-Scala课程97
- 大数据系列修炼-Scala课程04
- 大数据系列修炼-Scala课程10
- 大数据系列修炼-Scala课程53
- 大数据系列修炼-Scala课程15
- 大数据系列修炼-Scala课程24(2)
- 大数据系列修炼-Scala课程54
- 大数据系列修炼-Scala课程61
- 大数据系列修炼-Scala课程12
- 大数据系列修炼-Scala课程99
- 大数据系列修炼-Scala课程55