scala01
1、数据类型
Byte的数据范围:-128-127
Char:单引号扩起来
val c1:Char = ‘a’
c1.toInt // 97
2、在scala中,如何退出循环
**导入scala.util.control.Breaks.{break,breakable},
breakable{
处理逻辑
break
}
scala02
1、函数和方法的区别
2、默认值
val name:String= _ null
val age:Int= _ 0
val money:Double = _ 0.0
3、继承
先调用老子的构造器,再调用儿子的构造器
4、伴生对象、伴生类**
object class同名(名字) 伴生类和伴生对象
名字() ==> object apply 一般情况下 在这个apply方法里面是new 伴生类 *****
// low
new Array[Int](4)
// 一般这样
Array(1,2,3,4)
// 底层实际执行了如下代码
def apply[T: ClassTag](xs: T*): Array[T] = {
val array = new Array[T](xs.length)
var i = 0
for (x <- xs.iterator) { array(i) = x; i += 1 }
array
}
对象() ==> class apply 一般不用
5、class vs case class的区别
1、case class重写了hashCode() 和equals()方法
2、case class重写了toString方法
3、还有序列化的区别。case SparkSQL讲
6、case class vs case object
case object后面不能加参数 case object T1
case class后面必须加参数 case class(name:String…)
scala03
1、scala里面的三大集合类型
seq、map、set
2、可变数组ArrayBuffer(scala.collection.mutable.ArrayBuffer)
++= vs ++=:
------------------------------------------------------------------------
val bb1 = ArrayBuffer(1,2,3,4,5)
val bb2 = ArrayBuffer(10,20,30)
// bb1 ++= bb2
// bb1发生改变,bb2不变
scala> bb1 ++= bb2
res71: bb1.type = ArrayBuffer(1, 2, 3, 4, 5, 10, 20, 30)
scala> bb1
res73: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 10, 20, 30)
scala> bb2
res74: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(10, 20, 30)
--------------------------------------------------------------------------
val bb3 = ArrayBuffer(1,2,3,4,5)
val bb4 = ArrayBuffer(10,20,30)
scala> bb3 ++=: bb4
res0: bb3.type = ArrayBuffer(1, 2, 3, 4, 5, 10, 20, 30)
scala> bb3
res1: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5)
scala> bb4
res2: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 10, 20, 30)
3、val a = Map(“pk” ->30 , “ruoze” ->31)
a.get(“spark.executer.nums”).getOrElse(2) 传配置常用
4、Nil是什么? 不可变的空的List
5、构建一个List包含3个元素,不能用List定义
val l3 = 1::2::3::Nil
6、自己写map、filter、foreach、reduce,不允许使用scala api
以上四个的原理必须明白。Scala04开篇有总结
map:进来一个函数,集合里的每一个元素都作用上相同的函数
reduce:两两做什么事情
scala04
1、手撕mr里的窗口函数
2、find算子源码:返回匹配上的第一个元素
def find(p: A => Boolean): Option[A] = {
val i = prefixLength(!p(_))
if (i < length) Some(this(i)) else None
}
3、zip:相等的位置拼起来,变成一个tuple,没有的就匹配不到
def zip[A1 >: A, B, That](that: GenIterable[B])(implicit bf: CanBuildFrom[Repr, (A1, B), That]): That = {
val b = bf(repr)
val these = this.iterator
val those = that.iterator
while (these.hasNext && those.hasNext)
b += ((these.next(), those.next()))
b.result()
}
array.find(_ > 3) // Some(4) 返回第一个匹配到的
array.find(_ > 30) // None
val a = List(1,2,3,4)
val b = List("A",2,"C","D")
a.zip(b) // 相等的位置拼起来,变成一个tuple
val c = List("a","b","c","d","e")
a.zip(c) // List[(Int, String)] = List((1,a), (2,b), (3,c), (4,d))
a.zipAll(c,"-","~") // 类似左外右外,inner left right
b.zipWithIndex // 拼上索引列,sql里求连续用到
4、unapply:如果你的调用的Class,其实底层走的是Object的unapply方法
class Person(val name:String, val age:Int)
object Person {
def unapply(person:Person) : Option[(String,Int)] = { // 必须是unapply方法,不能是apply方法
if(person != null) {
Some(person.name, person.age)
} else {
None
}
}
}
val p = new Person("pk",30)
p match {
case Person(name,age) => println(name, age) // 如果你的调用的Class,其实底层走的是Object的unapply方法
case _ =>
}
Scala05 工作/面试
隐式参数
scalike jdbc
3、协变和逆变
协变:父->子
逆变:子->父
class Person
class User extends Person // Person > User
class Child extends User // Child < User
class Test[+User]
class Test2[-User]
val test2:Test[User] = new Test[Child] //class Test[+User]
val test3:Test2[User] = new Test2[Person] //class Test2[-User]
reduceLeft 【B>:Int】(op:(B,Int) => B)
X.reduceLeft的下界是Int型,传入一个tuple(B型,Int型),输出一个B型