重游Scala03
##############################################
/**在spark中map函數(shù)和flatMap函數(shù)是兩個比較常用的函數(shù)。其中?
map:對集合中每個元素進行操作, 將集合中的每一個元素映射到某一個函數(shù)。
map將一個函數(shù)應(yīng)用于列表的每一個元素并且將其作為一個新的列表返回
flatMap:對集合中每個元素進行操作然后再扁平化。 flat即壓扁,壓平,扁平化,
效果就是將集合中的每個元素的子元素映射到某個函數(shù)并返回新的集合。flatMap
應(yīng)用于每個序列元素會返回包含原始列表所有序列內(nèi)的元素的列表*/
package com.henu.georgeimport scala.collection.immutable/*** George* 常用函數(shù)*/
object MapDemo extends App {val arr = List(List("A",1),List("B",2),List("C",3))map的參數(shù)是一個函數(shù),函數(shù)的返回值是一個元素。這里不能使用_val arr1: immutable.Seq[Any] = arr.map(x => x(0))arr1.foreach(print)flatten扁平化函數(shù)println(arr.flatten)//List(A, 1, B, 2, C, 3)//flatMap的參數(shù)是一個函數(shù),函數(shù)的返回值是一個集合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
}
##############################################
/**
折疊,化簡:將二元函數(shù)引用于集合中的函數(shù)reduce
折疊,化簡:fold
Reduce函數(shù)將上一步返回的值作為函數(shù)的第一個參數(shù)繼續(xù)傳遞參與運算,直到list中的所有元素被遍歷。
Fold函數(shù)需要一個種子值做為初始值。可以把reduce函數(shù)看作是fold函數(shù)的簡化版。
*/
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)
}##############################################
/**
這個理解需要結(jié)合上面的知識點,掃描,即對某個集合的所有元素做fold操作,
但是會把產(chǎn)生的所有中間結(jié)果放置于一個集合中保存。
*/
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、函數(shù)匯總【擴展內(nèi)容】
映射函數(shù)map、flatMap
折疊函數(shù)reduce、fold
排序函數(shù)sortBy、sortWith、sorted
過濾函數(shù)filter、filterNot
計數(shù)函數(shù)count
集合函數(shù)diff、union、intersect
去重函數(shù)distinct
分組函數(shù)groupBy、grouped
取數(shù)函數(shù)take、takeRight、takeWhile
刪除函數(shù)drop、dropRight、dropWhile
分割函數(shù)span、splitAt、partition
擴展函數(shù)padTo
組合函數(shù)combinations、permutations
拉鏈函數(shù)zip、zipAll、zipWithIndex、unzip、unzip3
還有slice、sliding、updated
參考https://blog.csdn.net/pzw_0612/article/details/45936165
*/
##############################################
?
總結(jié)
- 上一篇: 重游Scala02
- 下一篇: 《Dream(梦想)》,无力的我,想放弃