1. foreach (类似于map, 但是没有返回值):
scala> val l = List(1, 2, 3, 4)l: List[Int] = List(1, 2, 3, 4) // map有返回值 scala> l.map(3 * _)res18: List[Int] = List(3, 6, 9, 12)scala> l.map(println(_)) 1234 res29: List[Unit] = List((), (), (), ())scala> l.map(x => println(3 * x)) 36912 res30: List[Unit] = List((), (), (), ()) // foreach 无返回值 scala> l.foreach(println(_)) 1234 scala> l.foreach(x => println(3 * x)) 36912
2. filter (过滤):
scala> val l = List(1, 2, 3, 4)l: List[Int] = List(1, 2, 3, 4)scala> l.filter(_ % 2 ==0)res31: List[Int] = List(2, 4)scala> l.filter(x => x % 2 ==0)res32: List[Int] = List(2, 4)
3. zip (聚合 => 生成一个装有元组的列表 ) 方法的使用:
scala> val a = List(1, 2, 3, 4)a: List[Int] = List(1, 2, 3, 4)scala> val b = List(11, 12, 13, 14)b: List[Int] = List(11, 12, 13, 14)scala> a.zip(b)res33: List[(Int, Int)] = List((1,11), (2,12), (3,13), (4,14))scala> a zip bres34: List[(Int, Int)] = List((1,11), (2,12), (3,13), (4,14))
4. partition (列表分割):
scala> val l = List(1, 2, 3, 4)l: List[Int] = List(1, 2, 3, 4)scala> l.partition(_ % 2 == 0)res36: (List[Int], List[Int]) = (List(2, 4),List(1, 3))
5. flatten (扁平化:多个List 压缩到一个List中 ):
scala> List(List(1, 2, 3), List(4, 5))res39: List[List[Int]] = List(List(1, 2, 3), List(4, 5))scala> List(List(1, 2, 3), List(4, 5)).flattenres40: List[Int] = List(1, 2, 3, 4, 5)
6. flatMap (map + flatten):
map是针对 大List中小List的操作,所以X得到就是小List, 然后对小List中元素进行map操作
然后对最终的结果:flatten汇总操作
- Spark 中 map函数会对每一条输入进行指定的操作,然后为每一条输入返回一个对象;
- 而flatMap函数则是两个操作的集合——正是“先映射后扁平化”:
操作1:同map函数一样:对每一条输入进行指定的操作,然后为每一条输入返回一个对象
操作2:最后将所有对象合并为一个对象
红色参考地址: http://www.bkjia.com/yjs/927704.html?qqdrsign=CD65
scala> val a = List(List(1, 2, 3), List(4, 5))a: List[List[Int]] = List(List(1, 2, 3), List(4, 5))scala> a.flatMap( _.map(3 * _))res51: List[Int] = List(3, 6, 9, 12, 15)scala> a.flatMap(x => x.map(3 * _))res53: List[Int] = List(3, 6, 9, 12, 15)