1.可变参数
/**
* 可变参数,在参数变量通配符后面添加一个*
*/
def add(ints: Int*): Int = {
var sum = 0;
for(v <- ints){
sum += v
}
sum
}
//可变参数一般放在参数列表的末尾
def add2(initValue: Int, ints: Int*): Int = {
var sum = initValue
for(v <- ints){
sum += v
}
sum
}
def makePerson(parames: Any*): Unit = {
}
def main(args: Array[String]): Unit = {
println(add(1,3,4,2,5))
}
//参数定义时,可以给定一个默认值
def add(a:Int = 6, b:Int = 7) = {
a +b
}
def main(args: Array[String]): Unit = {
println(add())//调用时,如果不传递参数,即会使用函数或者方法的默认值
println(add(8,9))//调用时,如果传递了参数值,则使用传递的参数值
println(add(9))//调用时,9覆盖了a=6
println(add(b=9))
println(add(a=9, b=9))
}
2.高阶函数
将其他的函数作为参数或结果是函数的函数
//函数有两个参数,一个是另一个函数,一个是整数
def apply(f: Int => String, v: Int) = f(v)
//函数的返回值是一个字符串
def layout(x: Int) = "[" + x.toString() + "]"
println(apply(layout, 101))
3.部分参数应用函数
如果函数传递所有预期的参数,则表示直接调用该函数;如果函数传递部分参数,则会产生一个新的函数,且没有传递的参数作为该函数的参数。
object PartParamFunc extends App {
//定义输出的方法,参数为date,message
def log(date: Date, message: String) = {
println(s"$date, $message")
}
val date = new Date()
//调用log的时候,传递了一个具体的时间参数,message为待定参数
//logBoundDate成了一个新的函数,只有log的部分参数(message)
val logBoundDate = log(date, _: String)
//调用logBoundDate的时候,只需要传递的message参数即可
logBoundDate("fuck jerry")
log(date, "fuck mark")
}
4.柯里化
柯里化指的是将原接受两个参数的函数变成新的接受一个参数的函数的过程。新的函数返回一个以原有第二个参数为的函数。
5.偏函数
object ScalaPartialFuncation {
def func(str: String) : Int = {
if(str.equals("a")) 97
else 0
}
//偏函数:PartialFunction[参数类型,返回值类型]
def func1: PartialFunction[String, Int] = {
case "a" => 97
case _ => 0
}
def f1: PartialFunction[Int, Int] = {
case i: Int => i*10
}
def main(args: Array[String]): Unit = {
println(func("a"))
println(func1("a"))
val arr = Array(1,2,3,4)
val collect = arr.collect(f1)
println(collect.toBuffer)
val arr1 = Array[Int](1,2,3,4,5)
arr1.map(x => x * 10)
arr1.map(_ * 10)
arr1.map{case x: Int => x * 9}//在转译时,会转化成偏函数
}
}
6.数组
数组长度不可变,内容可变。
map操作
object ArrayOpt {
def main(args: Array[String]): Unit = {
val arr = Array(1,3,5,8,9)
//val arr = Array.apply(1,3,5,8,9)
//map映射
val fx = (x: Int) => x * 10
//arr经过map映射操作之后会返回一个新的数组
val r1 = arr.map(fx)
//arr.map((x: Int) => x * 10)
//arr.map(_*10)
}
}
flatten扁平化操作
flatMap
//faltten扁平化操作
val arr1: Array[String] = Array("hello hello tom", "hello jerry")
//Array(Array("hello", "hello", "tom"), Array("hello", "jerry"))
val r2: Array[Array[String]] = arr1.map(_.split(" "))
//Array("hello", "hello", "hello", "tom", "jerry")
val arr3 = r2.flatten
//flatMap等价于先Map后flatten
val arr4 = arr1.flatMap(_.split(" "))
groupBy
val arr1: Array[String] = Array("hello hello tom", "hello jerry")
//Array("hello", "hello", "tom", "hello", "jerry")
val r3 = arr1.flatMap(_.split(" "))
//Map("hello" -> Array("hello", "hello", "hello"), tom -> Array("tom"), "jerry" -> Array("jerry"))
.groupBy(x => x)
//Map("hello":3, "tom":1, "jerry":1)
//.map(x => x._2.length)
.mapValues(x => x.length)
//List((hello,3),(tom,1),(jerry,1))
.toList
//List((tom,1), (jerry,1), (hello,3))
.sortBy(x => x._2)
7.复习
object 回顾 {
/**
* 可变参数
* def method1(str: String, a: Any*)
*
* 默认值方法
* def method2(a: Int = 6, x: String = "a")
* method2()
* method2(x="b", a=8)
*
* 高阶函数:参数是函数或者返回值是函数
* def method3(f: Int => Int, c: Int) = f(c)
*
* 柯里化
* def method4(a: Int)(b: Int, c: Int)
* method(0)(4,5)
*
* 偏函数:PartialFunction[参数类型,函数返回值类型]
* def method5 :PartialFuntion[Any, String] = {
* case x: Int => x.toString
* }
*
* 数组的定义
* Array:长度不可变,内容可变
* val arr = Array(1,4,6) =>
*
* 10 => 10*10 => 100
* map => 映射 将集合进行某种操作,并且返回一种新的集合。
* arr.map((x: Int) => x * 10)
* arr.map(x => x * 10)
* arr.map(_ * 10)
*
* flatten 扁平化操作
* Array[Array[String]] = Array(Array("1", "2"), Array("4", "5"))
* 扁平化操作后:
* Array[String] = Array("1", "2", "4", "5")
*
* flatMap
* map + flatten
*
* foreach 对集合进行循环操作
* arr.foreach(f: Int => Unit)
*/
}
WordCount实例
object WordCount {
def main(args: Array[String]): Unit = {
val words = Array("hello tome hello jim", "hello hatano hello 菲菲")
//words数组中的每一个元素进行切分
//Array(Array("hello", "tome", "hello", "jim"), Array("hello", "hatano", "hello", "菲菲"))
val wordSplit = words.map((x: String) => x.split(" "))
//将数组中的Array扁平化
//Array("hello", "tome", "hello", "jim", "hello", "hatano", "hello", "菲菲")
val flWords: Array[String] = wordSplit.flatten
//分组
//map("hello" -> Array("hello", "hello", "hello"), "tome" -> Array("tome"))
val mapWords: Map[String, Array[String]] = flWords.groupBy((wd: String) => wd)
//(hello,4),(tom,1),...
val wordResult: Map[String, Int] = mapWords.map(wdKV => (wdKV._1, wdKV._2.length))
//map不支持排序,需要将map转换成List,调用sortBy方法进行排序
val sortResut: List[(String, Int)] = wordResult.toList.sortBy(t => t._2)
sortResut.foreach(t => println(t))
}
}
8.集合使用
List集合内容不可变
ArrayBuffer可变集合
HashMap可变集合
List
list + list 会产生一个新的List,list本身长度不可以改变。
拼接list
count和Filter
sortBy
grouped:方法返回一个迭代器
以指定的大小分组
fold:第一个参数作为初始值
fold(初始值)(操作函数)
foldLeft
list.foldLeft(0)(_ - _)执行过程
sum=0-1
sum=sum-2
sum=sum-3
sum=-6
foldRight
list.foldRight(0)(_ - _)执行过程
sum=1-0
sum=2-sum
sum=3-sum
sum=2
aggregate在单机非并行化集合下调用的是foldLeft
后来被封装为reduceByKey
Union:并集
intersect:交集
diff:差集
zip
mkString
slice截取list集合
9.Set
Set特性:无需不重复
定义不可变Set:
val set = Set(1,2,3)
定义可变Set:
val hset = collection.mutable.HashSet(1,3,4)
hset.add(5)
remove:
hset.remove(3)
hset ++ Set(0,9)
创建Map
删除
获取元素
创建HashMap
如何查看对象可操作的方法:
mmp. + Tab(键)
get方法:当获取的值不存在时,
getOrElse方法:如果获取的值不存在时,返回一个默认值
10.元组
创建元组和获取元组中的元素:
wordCount