日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

重游Scala03

發布時間:2024/2/28 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 重游Scala03 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
############################################## /**在spark中map函數和flatMap函數是兩個比較常用的函數。其中? map:對集合中每個元素進行操作, 將集合中的每一個元素映射到某一個函數。 map將一個函數應用于列表的每一個元素并且將其作為一個新的列表返回 flatMap:對集合中每個元素進行操作然后再扁平化。 flat即壓扁,壓平,扁平化, 效果就是將集合中的每個元素的子元素映射到某個函數并返回新的集合。flatMap 應用于每個序列元素會返回包含原始列表所有序列內的元素的列表*/ package com.henu.georgeimport scala.collection.immutable/*** George* 常用函數*/ object MapDemo extends App {val arr = List(List("A",1),List("B",2),List("C",3))map的參數是一個函數,函數的返回值是一個元素。這里不能使用_val arr1: immutable.Seq[Any] = arr.map(x => x(0))arr1.foreach(print)flatten扁平化函數println(arr.flatten)//List(A, 1, B, 2, C, 3)//flatMap的參數是一個函數,函數的返回值是一個集合val arr2 = arr.flatMap(x => List(x(0),x(1)))println(arr2)//List(A, 1, B, 2, C, 3)//如果arr = List(("A",1),("B",2),("C",3)),只能使用flatMap } ############################################## /** 折疊,化簡:將二元函數引用于集合中的函數reduce 折疊,化簡:fold Reduce函數將上一步返回的值作為函數的第一個參數繼續傳遞參與運算,直到list中的所有元素被遍歷。 Fold函數需要一個種子值做為初始值。可以把reduce函數看作是fold函數的簡化版。 */ package com.henu.georgeobject TwoDemo extends App {val list = List(1,2,3,4,5)println(list.reduce(_+_))//15println(list.reduceLeft((x:Int,y:Int) => {print(x,y);x+y}))//(1,2)(3,3)(6,4)(10,5)15println(list.reduceRight((x:Int,y:Int)=>{print(x,y);x+y}))//(4,5)(3,9)(2,12)(1,14)15 // println(list.fold(_+_))//編譯不通過println(list.foldLeft(10)((x:Int,y:Int)=>{print(x,y);x+y}))//(10,1)(11,2)(13,3)(16,4)(20,5)25println(list.foldRight(10)((x:Int,y:Int) => {print(x,y);x+y}))//(5,10)(4,15)(3,19)(2,22)(1,24)25 }############################################## /** roupBy 針對對偶的集合,可以按照一個元素進行分組,返回Map類型。 */ package com.henu.georgeimport scala.collection.immutableobject GroupByDemo extends App {val a = List(("a",22),("b",22),("a",22),("d",22),("b",11))println(a.groupBy(_._1))//Map(b -> List((b,22), (b,11)), d -> List((d,22)), a -> List((a,22), (a,22)))println(a.groupBy(_._2))//Map(11 -> List((b,11)), 22 -> List((a,22), (b,22), (a,22), (d,22)))println(a.groupBy(_._2>0))//Map(true -> List((a,22), (b,22), (a,22), (d,22), (b,11)))val e = List(("George",16000,22),("Honey",10000,22),("George",20000,23),("George",28000,24))val f: immutable.Seq[(String, Int, Int)] = e.map(r => {(r._1,(r._2),(r._3))})f.foreach(print)//(George,16000,22)(Honey,10000,22)(George,20000,23)(George,28000,24) }############################################## /** 這個理解需要結合上面的知識點,掃描,即對某個集合的所有元素做fold操作, 但是會把產生的所有中間結果放置于一個集合中保存。 */ package com.henu.georgeobject ScanDemo extends App {val list1 = 1 to 10000print(list1(9999))//10000val a1 = 1 to 5println(a1)val a2 = a1.scanLeft(1)(_+_)println(a2)//Vector(1, 2, 4, 7, 11, 16)val a3 = a1.scanLeft(10)(_-_)println(a3)//Vector(10, 9, 7, 4, 0, -5)val a4 = a1.scanRight(1)(_+_)println(a4)//Vector(16, 15, 13, 10, 6, 1)val a5 = a1.scanRight(10)(_+_)println(a5)//Vector(25, 24, 22, 19, 15, 10) }############################################## /** 5、函數匯總【擴展內容】 映射函數map、flatMap 折疊函數reduce、fold 排序函數sortBy、sortWith、sorted 過濾函數filter、filterNot 計數函數count 集合函數diff、union、intersect 去重函數distinct 分組函數groupBy、grouped 取數函數take、takeRight、takeWhile 刪除函數drop、dropRight、dropWhile 分割函數span、splitAt、partition 擴展函數padTo 組合函數combinations、permutations 拉鏈函數zip、zipAll、zipWithIndex、unzip、unzip3 還有slice、sliding、updated 參考https://blog.csdn.net/pzw_0612/article/details/45936165 */ ##############################################

?

總結

以上是生活随笔為你收集整理的重游Scala03的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。