学完scala的效果:
1、能熟练使用scala编写spark代码 + 能看懂spark的源码
2、能看懂hadoop和spark的RPC的架构和源码 + 能自己设计和模拟实现一个分布式系统
存储 + 计算 + 资源调度
能模拟试下一个HDFS,实现一个计算引擎MapReduce/Spark, 模拟实现YARN
————————————————————————————————————————
1、方法的定义和使用
求两个数的和:
def add(x:Int, y:Int):Int = {
val result = x + y
result
}
add(2, 3) = 5
2、函数的定义很使用
求两个数的和:
val func_add = (x:Int, y:Int) => {x + y}
val func_add = (x:Int, y:Int) => x + y
val func_add: ((Int, Int) => Int) = (x, y) => x + y
func_add(2, 3) = 5
3、函数和方法的高阶使用
函数和方法 都可以作为 函数或者方法的 参数或者返回值
高阶函数:函数 和 方法 可以作为 函数的 返回值 或者 参数
4、数组
定长 + 变长
Array + ArrayBuffer
定义、访问、修改、增删改(主要针对变长数组)、 遍历
val array = Array(1,2,3)
for(a <- array){
println(a)
}
for(index <- 0 unitl array.length){
println(a)
}
定长数组 和 变长数组 的相互转换
val array = Array(1,2,3)
import scala.collection.immutable.ArrayBuffer
val ab: ArrayBuffer[Int] = new ArrayBuffer[Int]()
ab += 3
ab += 4
ab += (5,6,7)
array.toBuffer() // 把定长数组转成变长数组
ab.toArray() // 把变长数组转成定长数组
12、集合
可变 + 不可变
注意要点:对于一个不可变的集合如果进行集合的修改操作,那么其实是生成了一个新的集合返回
List: 列表
注意要点:
List是由一个头元素+一个尾列表组成
val list = 1 :: 2 :: 3 :: Nil
Nil != Null
Set: 集合
1、求 去重
2、求 交集,差集,并集
// 求并集
scala> set1 union set2 或 scala> set1 ++ set2
// 求交集
scala> set1 intersect set2
// 求差集
scala> set1 diff set2
Tuple: 元组
元组中的元素都是不可变的。如果元组中某个值是一个引用对象,那么这个引用对象中的属性是可以改的。
python中的经典面试题:深浅拷贝
元组的定义:
scala> val t1 = (1, 2f, 3.3, true, "huangbo")
t1: (Int, Float, Double, Boolean, String) = (1,2.0,3.3,true,huangbo)
如果使用java实现:
1、请构建一个pojo对象,然后这五个值分别成为这个对象的五个属性值
2、请定义一个Object数组
3、请拼接这些值成为字符串
scala> val t1,(a,b,c,d,e) = (1, 2f, 3.3, true, "huangbo")
t1: (Int, Float, Double, Boolean, String) = (1,2.0,3.3,true,huangbo)
a: Int = 1
b: Float = 2.0
c: Double = 3.3
d: Boolean = true
e: String = huangbo
scala> t1._1
res140: Int = 1
scala> a
res141: Int = 1
特别需要注意的地方:
使用
val t1,(a,b,c,d,e),t2 = {println("aa");(1, 2f, 3.3, true, "huangbo")}
定义元组,要注意的是:{}代码块会执行多次
Map: 映射
定义:
scala> val map1 = Map(("a", 1), ("b", 2), ("c", 3))
map1: scala.collection.immutable.Map[String,Int] = Map(a -> 1, b -> 2, c -> 3)
scala> val map2 = Map("a" -> 1, "b" -> 2, "c" -> 3)
map2: scala.collection.immutable.Map[String,Int] = Map(a -> 1, b -> 2, c -> 3)
增删:
scala> import scala.collection.mutable.HashMap
import scala.collection.mutable.HashMap
scala> val map1 = new HashMap[String,Int]()
map1: scala.collection.mutable.HashMap[String,Int] = Map()
scala> map1.put("a", 1)
res143: Option[Int] = None
scala> map1.remove("a")
res147: Option[Int] = Some(1)
scala> map1 += (("a", 1))
res150: map1.type = Map(b -> 2, a -> 1)
scala> map1 -= "a"
res151: map1.type = Map(b -> 2)
访问:
scala> map2("a")
res152: Int = 1
scala> map2("d")
java.util.NoSuchElementException: key not found: d
at scala.collection.MapLike$class.default(MapLike.scala:228)
at scala.collection.AbstractMap.default(Map.scala:59)
at scala.collection.MapLike$class.apply(MapLike.scala:141)
at scala.collection.AbstractMap.apply(Map.scala:59)
... 32 elided
scala> map2.get("a")
res154: Option[Int] = Some(1)
scala> map2.get("d")
res155: Option[Int] = None
scala> map2.getOrElse("a", 100)
res156: Int = 1
scala> map2.getOrElse("d", 100)
res157: Int = 100
访问方式有三种:
map(key)
map.get(key)
map.getOrElse(key, defaultValue)
遍历:
scala> for((key, value) <- map1){println(key, value)}
(a,1)
(b,2)
(c,3)
scala> for(key <- map1.keys) println(key)
a
b
c
scala> for(value <- map1.values) println(value)
1
2
3
压缩:
zip unzip
scala> val list1 = List("a", "b", "c", "d")
list1: List[String] = List(a, b, c, d)
scala> val list2 = List(1, 2, 3, 4)
list2: List[Int] = List(1, 2, 3, 4)
scala> val listResult = list1 zip list2
listResult: List[(String, Int)] = List((a,1), (b,2), (c,3), (d,4))
scala> val map = listResult.toMap
map: scala.collection.immutable.Map[String,Int] = Map(a -> 1, b -> 2, c -> 3, d -> 4)
scala> map.unzip
res162: (scala.collection.immutable.Iterable[String], scala.collection.immutable.Iterable[Int]) = (List(a, b, c, d),List(1, 2, 3, 4))
scala> res162._1
res163: scala.collection.immutable.Iterable[String] = List(a, b, c, d)
scala> res162._2
res164: scala.collection.immutable.Iterable[Int] = List(1, 2, 3, 4)
13、常用方法总结
map
reduce
filter
foreach
count
.....
关于排序
sorted: 直接按照元素的字典顺序排序
sortBy:按照元素的部分字段的组合的字典顺序来排序
sortWith: 根据自定义的比较规则来排序
scala> val list = List(3,2,6,1,5,4,7,8)
list: List[Int] = List(3, 2, 6, 1, 5, 4, 7, 8)
scala> list.sort
sortBy sortWith sorted
scala> list.sortWith((x, y) => x > y)
res192: List[Int] = List(8, 7, 6, 5, 4, 3, 2, 1)
scala> list.sortWith((x, y) => x < y)
res193: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8)
scala> list.sorted
res194: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8)
scala> list.sortBy(x => x)
res195: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8)
更完整的测试:
scala> case class Student(id:Int, name:String)
defined class Student
scala> var s1 = Student(1, "huangbo")
s1: Student = Student(1,huangbo)
scala> var s2 = Student(2, "xuzheng")
s2: Student = Student(2,xuzheng)
scala> var s3 = Student(3, "wang")
s3: Student = Student(3,wang)
scala> val list1 = List(s2,s3,s1)
list1: List[Student] = List(Student(2,xuzheng), Student(3,wang), Student(1,huangbo))
scala> list1.sortBy(s => s.id)
res199: List[Student] = List(Student(1,huangbo), Student(2,xuzheng), Student(3,wang))
14、scala面向对象
详见:https://blog.csdn.net/qq_1018944104/article/details/85075170