mapValues高阶函数

tech2024-10-15  2

mapValues高阶函数

对象类型泛型:Map类型。只对集合中value做操作、key保持不变。一般用于求和,分组最大,分组最小,均值。常用于分组求聚合。

高阶函数(算子)--------sql,用sql语句思路翻译高阶函数。

def f1(): Unit ={ var linelist: List[(String, Int)] = List(("hello scala hello world",4), ("hello world",3),("hello hadoop",2),("hello hbase",1)) val flatmapRDD: List[(String,Int)] = linelist.flatMap(t=>{ var line:String = t._1 //"hello scala hello world" val word: Array[String] = line.split(" ") //"hello" "scala" "hello" "world" val mapRDD: Array[(String, Int)] = word.map(w=>(w,t._2)) //("hello",4),("scala",4).... mapRDD }) val groupRDD: Map[String, List[(String, Int)]] = flatmapRDD.groupBy(t=>t._1) val stringToint: Map[String, Int] = groupRDD.mapValues(data=>data.map(tt=>tt._2).sum) println(stringToint.mkString("|")) //world -> 7|hadoop -> 2|scala -> 4|hello -> 14|hbase -> 1 }

案例二:规约reduce

def f1(): Unit ={ var list = List(1,2,3,4) val result: Int = list.reduce((left, right)=>left+right) //如果形参在代码体中只被使用过一次,就可以使用下划线 val result1: Int = list.reduce(_+_) println(result) //10 println(result1) //10 }

模式匹配

类比java的switch

scala的模式匹配如果能匹配上会执行case后面的语句,默认会有自动的break语句,但是不可见。如果都匹配不上,会自动执行case_。

=>类似java中的: 。

一个case中可以写代码体,要添加{}。

案例一:

def f1(): Unit ={ var oper="3" var res=0 var num1 =10 var num2 =2 oper match { case "+"=>res=num1+num2 case "-"=>res=num1-num2 case "*"=>res=num1*num2 case "/"=>res=num1/num2 case _=>println("nothing") } println(res)

案例二:模式匹配—守卫

匹配所有的后面可以写条件,一个模式匹配中可以同时存在多个匹配所有但是用于执行第一个。

def f1(): Unit ={ for (ch<-"+-3!"){ var temp1 = 0 ch match { case '+' => temp1= 1 case '-' => temp1= -1 case _ => temp1 = 3 case _ => temp1 = 4 } println(temp1)//1 -1 3 3 } def f1(): Unit ={ for (ch<-"+-3!"){ var temp1 = 0 ch match { case '+' => temp1= 1 case '-' => temp1= -1 case _ if ch.toString.equals("3")=> temp1 = 3 case _ => temp1 = 4 } println(temp1)//1 -1 3 4 }

scala的模式匹配也是和java一样按照顺序执行的。

def f1(): Unit ={ for (ch<-"+-3!"){ var temp1 = 0 ch match { case _ => temp1 = 4 case '+' => temp1= 1 case '-' => temp1= -1 case _ if ch.toString.equals("3")=> temp1 = 3 } println(temp1)//4 4 4 4 }

案例三:匹配变量

case关键字后边可以写变量名,意味着match表达式的值会赋值给这个变量

注意:在模式匹配中如果没有任何匹配会报错。(scala.MatchError)

def f1(): Unit ={ var ch = 'v' ch match { case '+' =>println("hh~") case mChar =>println("hh~ "+mChar) //hh~ v }

案例四:类型匹配

def f1(): Unit ={ //var obj: Int = 0 var obj: Any = 2 obj match { case a:Int => println("对象是int类型: "+a) case a:String => println("对象是string类型: "+a) case a:Array[Int] => println("对象是Array类型: "+a) }

案例五:数组匹配

def f1(): Unit ={ for(arr<-Array(Array(0),Array(1,0))){ var result = arr match { case Array(0) => "0" //精确匹配 case Array(x,y) => x+"-"+y //带有变量的匹配 case Array(0,_*) => "以0开头的数组" //模糊匹配 case _ => "nothing" } println(result) }

案例六:列表匹配

def f1(): Unit ={ for(arr<-Array(List(0),List(1,0),List(1,0,1))){ var result = arr match { case 0::Nil => "0" //精确匹配 case x::y::Nil => x+"-"+y //带有变量的匹配 case 0::tail => "以0开头的数组" //模糊匹配 case _ => "nothing" } println(result) }

案例七:元组匹配

def f1(): Unit ={ for(arr<-Array((0,1),(1,0),(2,1),(1,0,2))){ var result = arr match { case (0,_) => "0" case (y,0) => y case (a,b) => a+" "+b case _ => "nothing" } println(result) }
最新回复(0)