Spark学习--scala

Scala语法
1.值和变量
val 值,不能发生变化
var 变量,可以发生变化

scala> val a:Int = 1
a: Int = 1
scala> a = 2;
<console>:12: error: reassignment to val
       a = 2;
         ^
scala> var a:Int = 2
a: Int = 2

scala> a = 3
a: Int = 3 

2.值和变量
object //伴生对象,相当于静态类

3.Range变量
1 to 10 1.to(10) immutable包下面都是不可变的:scala.collection.immutable.Range.Inclusive
1 until 10:数组遍历
Range(1,10,2)第三个参数是步长
for(i <- 1 to 5) {println(i)};

scala> 1 to 10
res0: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8,
 9, 10)

scala> 1.to(10)
res1: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8,
 9, 10)

scala> 1 until 10
res2: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)

scala> Range(1,10,2)
res3: scala.collection.immutable.Range = Range(1, 3, 5, 7, 9)

4.懒加载
lazy val a = 1;

5.变长参数

scala> def add(x:Int*){
     | for(i <- x)
     | println(i)
     | }
add: (x: Int*)Unit

scala> add(3,5,6)
3
5
6

6.键值对参数,Map类型
scala.collection.mutable.Map
val m = Map(“k1”->1, “k2”->2)
m.get(“k1”) :有值为Some,没值为None
m(“k1”):
m.getOrElse(“1”, “None”): Any是Object、常用类型的父类。AnyRef是对象类型的父类
m+=(“k3”->3);
m -=(“k3”)
for((_:v)<-m){
println(v)
}

scala> val m =  scala.collection.mutable.Map("K1"->1, "K3"->3)
m: scala.collection.mutable.Map[String,Int] = Map(K1 -> 1, K3 -> 3)

scala> m+=("K2"->2)
res11: m.type = Map(K1 -> 1, K3 -> 3, K2 -> 2)

7.元祖类型
val t = (1,2,”3”, 4);
下标访问,从1开始:t._1,t._3
val a= Array(1,2,3,4) //数组类型
下标访问,用小括号:a(0)
a++=Array(65,5,57,32)
a.remove(1,4)

scala> val t = (1,2,3,4)
t: (Int, Int, Int, Int) = (1,2,3,4)

scala> val t = (1, 3, 2, "4")
t: (Int, Int, Int, String) = (1,3,2,4)

scala> t._4
res12: String = 4

scala> t._2
res13: Int = 3

scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)

scala> val a = Array(1,2,"3",4)
a: Array[Any] = Array(1, 2, 3, 4)

scala> t(3)
<console>:13: error: (Int, Int, Int, String) does not take parameters
       t(3)
        ^

scala> a(3)
res15: Any = 4

scala> a(2)
res16: Any = 3

8.对象

class Person{
  var name:String = _ //需要有下划线
  var age = 18
  private[this] val xx = "xxxx" //即便是实例,也不能访问
}
class Person(val name:String, val age:Int){
  println("主构造器")
  var xx: String = _
  def this(name:String, age:Int, xx:String){
    this(name, age)
    this.xx = xx
  }
  override def toString:String="xxxx" //重写父类方法
}
//如果不写var、val的话,默认为private[this]

9.接口、特质trait

trait Logger{//特质
}
//当一个类没有继承其他类是,可以用extends,有继承其他类时,用with
trait ConsoLogger extends Logger{

}
//特质是从左到右构造出来的,已经构造就不会再构造

10.apply()方法

import java.util._ //下划线_相当于jav里面的*
//对象加(),调用的是object的apply方法
//对象名加(),调用的是class的apply方法
class AT{
  def test = "xxxx"
  def apply()="~~~~"
}

object AT{
  def apply() = "csddsf"
}

object Demo2 {
  def main(args: Array[String]): Unit = {
    var at = new AT();
    println(AT());
    println(at());
  }
}

11.spark基本不用try catch

12.模式匹配,相当于switch case

object Demo3 {
  //打印出一个变量的类型
  def m(x:Any)=x match{
    case o:Int => "int"
    case o:String => "String"
  }

  def main(args: Array[String]): Unit = {
    val x = 2
    val result = x match{
      case 1 => "one"
      case 2 => "two"
      case _ => "none"
    }
    println(result)

    println(m(2));
    println(m("hello"))
  }
}

13.case类

absract class P0001
case class S0001(name:String) extends P0001
case class T0001(name:String) extends P0001
def ma(p:P0001) = {
     p match {
          case S0001(_) => "stu" //_匹配任意类型
          case T0001(_) => "tea"
          case _=>"None"
     }
}

main{
     println(ma(S0001("stu"))); //case类不用new字段
}

14.文件流

object FileIO {
  def main(args: Array[String]): Unit = {
    val file = Source.fromFile(new File("D:/data.txt"))
    for(line <- file.getLines()){
      println(line)
    }
  }
}

15.值函数
def add(x:Int, y:Int)={x+y} :
val add = add :将函数赋别名
_add: (Int, Int) => Int =

scala> def add(x:Int, y:Int) = {x+y}
add: (x: Int, y: Int)Int

scala> add(2,3)
res0: Int = 5

scala> val _add = add_
<console>:11: error: not found: value add_
       val _add = add_
                  ^

scala> val _add = add _
_add: (Int, Int) => Int = <function2>

16.函数嵌套函数

object Basic9 {
  def add3(x:Int, y:Int, z:Int)={
    def add2(x:Int, y:Int)={
      x+y
    }
    add2(add2(x,y),z)
  }

  def main(args: Array[String]): Unit = {
    println(add3(1,2,3))
  }
}

17.匿名值函数
(x:Int)=>x+1
Int => Int =

scala> (x:Int)=>x+1
res1: Int => Int = <function1>

scala> res1(3)
res2: Int = 4

18.map函数
a.map()前面里面的每一个元素取出来,执行里面的function
a.map((x:Int)=>x*2) 匿名函数—-》去掉函数类型 —->
a.map((x)=> x*2)
a.map(_*2)

scala> val a = List(1,2,3,4)
a: List[Int] = List(1, 2, 3, 4)

scala> a.map((x:Int)=>x*2)
res3: List[Int] = List(2, 4, 6, 8)

scala> a.map((x) => x*2)
res4: List[Int] = List(2, 4, 6, 8)

scala> a.map(_*2)
res5: List[Int] = List(2, 4, 6, 8)

scala> def add(x:Int)=x*2
add: (x: Int)Int

scala> a.map(add)
res6: List[Int] = List(2, 4, 6, 8)

19.type代表返回自身
有点糊涂
scala> (x:Int, y:Int) => Int
res3: (Int, Int) => Int.type =

scala> (x:Int, y:Int) => Int
res7: (Int, Int) => Int.type = <function2>

scala> res7(3,4)
res8: Int.type = object scala.Int

20.函数的参数是函数,确定了参数值,对参数的功能有参数函数确定
def fun(f:(Int, Int)=>Int):Int=f(3,4)
def add(x:Int, y:Int)=x+y

scala> def fun(f:(Int,Int)=>Int):Int=f(3,4)
fun: (f: (Int, Int) => Int)Int
scala> def add(x:Int, y:Int) = x+y
add: (x: Int, y: Int)Int

scala> fun(add)
res10: Int = 7

21.由一个函数生成另一个函数
def a(x:Int)=(y:Int)=>x+y

scala> def a(x:Int)=(y:Int)=>x+y
a: (x: Int)Int => Int

scala> a(2)
res11: Int => Int = <function1>

scala> res11(5)
res12: Int = 7
scala> a(4)(9)
res14: Int = 13
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值