1.foreach遍历
object demo5{
def main(args:Array[String]){
//foreach遍历:
val listA = List(1,2,3,4,5)
//foreach接收的是一个函数:其返回值是一个unit(空)类型
println(listA.foreach((x:Int)=>print(x.toString+" ")))
//使用类型推断简化函数定义:
println(listA.foreach(x=>print(x.toString+" ")))
//使用下划线简化函数定义:
//函数参数只出现过一次的情况
println(listA.foreach(print(_)))
1 2 3 4 5 ()
1 2 3 4 5 ()
12345()
2.Map映射
//Map映射:
//将数据类型转换,接收的也是一个函数,应用到每个元素,返回新的列表
println(listA.map(a=>a*2+1))
//使用’_‘简化时,需要注意使用条件,并省略’=>‘
println(listA.map(_ * 3))
List(3, 5, 7, 9, 11)
List(3, 6, 9, 12, 15)
3.flatMap扁平化映射
//flatMap扁平化映射
//先Map再flatten
//eg:提取多个列表中的单词:
val listB = List("scala spark hadoop flink kafka","mysql orecle postgres hive mangodb")
val temp1 = listB.map(_.split(" "))
val listC = temp1.flatten
println(listC)
//直接使用flatMap
println(listB.flatMap(_.split(" ")))
List(scala, spark, hadoop, flink, kafka, mysql, orecle, postgres, hive, mangodb)
List(scala, spark, hadoop, flink, kafka, mysql, orecle, postgres, hive, mangodb)
4.filter过滤
//filter过滤
val listD = List(1,2,3,4546,45745,879,324,114,1234,346,778,78,6)
println(listD.filter(_%3==0))
List(3, 879, 324, 114, 78, 6)
5.sorted,sortBy,sortWith排序
//sortBy,sorted,sortWith
val listF = List(1,2,3,4,4325,3643,141,346,785,1243,124,3124,124)
//sorted排序
println(listF.sorted)
//sortBy对字段排序,如下按照单词
val listG = List("01 hadoop","02 apple","03 zxvf")
println(listG.sortBy(_.split(" ")(1)))
//sorfWith自定义排序
val listH = List(23,123,3245,456,67,532,131,7,97,21)
println(listH.sortWith((x,y)=>if (x<y) true else false))
//简化如下:
println(listH.sortWith(_<_))
List(1, 2, 3, 4, 124, 124, 141, 346, 785, 1243, 3124, 3643, 4325)
List(02 apple, 01 hadoop, 03 zxvf)
List(7, 21, 23, 67, 97, 123, 131, 456, 532, 3245)
List(7, 21, 23, 67, 97, 123, 131, 456, 532, 3245)
6.groupBy分组
//groupBy分组:
val listI = List("zhangsan"->"男","lisi"->"男","liyan"->"女")
val temp2 = listI.groupBy(x=>x._2)
println(temp2)
println(temp2.map(x=>x._1 -> x._2.size))
println((temp2.map(x=>x._1 -> x._2.size).toList))
}
}
Map(男 -> List((zhangsan,男), (lisi,男)), 女 -> List((liyan,女)))
Map(男 -> 2, 女 -> 1)
List((男,2), (女,1))