scala> val a = new Array[String](5)
a: Array[String] = Array(null, null, null, null, null)
scala> a.length
res2: Int = 5
scala> a(1) = "hello"
scala> val b = new Array(5)
b: Array[Nothing] = Array(null, null, null, null, null)
声明数组,不指定类型返回的是Nothing类型,Nothing是Throwable,定义时候没问题,用的时候回抛错
以上都是用new 声明的
但是声明数组也可以
scala> val a = Array("a","b","c")
a: Array[String] = Array(a, b, c)
这里面的Array就是通过 object下的apply方法完成实例化class
不可变长度scala
import scala.collection.mutable.ArrayBuffer
scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBufferscala> val c = ArrayBuffer[Int]()
c: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()scala> c += 1
res0: c.type = ArrayBuffer(1)scala> c += 2
res1: c.type = ArrayBuffer(1, 2)scala> c += 3
res2: c.type = ArrayBuffer(1, 2, 3)scala> c += (4,5,6)
res3: c.type = ArrayBuffer(1, 2, 3, 4, 5, 6)scala> c ++=Array(7,8,9)
res4: c.type = ArrayBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9)scala> c.insert(0,0)
scala> c
res7: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
remove
insert
trimEnd
变长数组变定长数组
scala> c.toArray
res13: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> val a = Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)scala> a.reduce(_ + _)
res20: Int = 15scala>
scala> a.reduceLeft(_ + _)
res21: Int = 15scala> a.reduceRight(_ + _)
res22: Int = 15scala>
scala> a.reduceLeft((a,b)=>{
| println(a)
| println(b)
| println("---------")
| a + b
| })
1
2
---------
3
3
---------
6
4
---------
10
5
---------
res23: Int = 15scala>
scala> a.reduceRight((a,b)=>{
| println(a)
| println(b)
| println("---------")
| a + b
| })
4
5
---------
3
9
---------
2
12
---------
1
14
---------
res24: Int = 15
scala> val a = List(1,2,3,4)
a: List[Int] = List(1, 2, 3, 4)scala> val b = List("A","B","C","D")
b: List[String] = List(A, B, C, D)scala> a.zip(b)
res25: List[(Int, String)] = List((1,A), (2,B), (3,C), (4,D))scala> val c = List("A","B","C","D","E")
c: List[String] = List(A, B, C, D, E)scala> a.zip
zip zipAll zipWithIndexscala> a.zip(c)
res26: List[(Int, String)] = List((1,A), (2,B), (3,C), (4,D))
flatMap等同于 flatten + map
flatten将数据拍平,map操作不会对数据结构有变化,
flatMap将数据进行map操作并大瓶
scala> val f = Array(Array(1,2),Array(3,4),Array(5,6))
f: Array[Array[Int]] = Array(Array(1, 2), Array(3, 4), Array(5, 6))scala> f.flatten
res30: Array[Int] = Array(1, 2, 3, 4, 5, 6)scala> f.flatMap(_.map(_*2))
res31: Array[Int] = Array(2, 4, 6, 8, 10, 12)scala> f.map(_.map(_*2))
res32: Array[Array[Int]] = Array(Array(2, 4), Array(6, 8), Array(10, 12))
scala> val d = Array("hadoop,spark","bigdata,study")
d: Array[String] = Array(hadoop,spark, bigdata,study)scala>
scala> d.map(x=>x.split(",")).flatten
res33: Array[String] = Array(hadoop, spark, bigdata, study)scala> d.flatMap(x => x.split(","))
res34: Array[String] = Array(hadoop, spark, bigdata, study)
find查找的,满足条件的元素里的第一个元素
scala> val array = Array(1,2,3,4,5,6)
array: Array[Int] = Array(1, 2, 3, 4, 5, 6)scala> array.find( _ > 3)
res35: Option[Int] = Some(4)
Some继承了Option,
Optin有两个,一个是Some,另一个是None
获取东西获取到了是some,获取不到是None
按照字典序升序进行排序
scala> val aaa = Array("c","b","a")
aaa: Array[String] = Array(c, b, a)scala> aaa.sorted
res40: Array[String] = Array(a, b, c)
降序排
scala> val bbb = Array(3,1,2) bbb: Array[Int] = Array(3, 1, 2) scala> bbb.sortBy(x=> -x) res41: Array[Int] = Array(3, 2, 1)
自定义分组
scala> val arrs = Array(("a",5),("c",5),("a",20),("e",30))
arrs: Array[(String, Int)] = Array((a,5), (c,5), (a,20), (e,30))scala> arrs.groupBy(x => x._1)
res38: scala.collection.immutable.Map[String,Array[(String, Int)]] = Map(e -> Array((e,30)), a -> Array((a,5), (a,20)), c -> Array((c,5)))scala> arrs.groupBy(x => x._2)
res39: scala.collection.immutable.Map[Int,Array[(String, Int)]] = Map(20 -> Array((a,20)), 5 -> Array((a,5), (c,5)), 30 -> Array((e,30)))
scala> val data = Array("hello,world,hello","hello,world")
data: Array[String] = Array(hello,world,hello, hello,world)scala> val words = data.flatMap(_.split(","))
words: Array[String] = Array(hello, world, hello, hello, world)scala> val wordWithOne = words.map((_,1))
wordWithOne: Array[(String, Int)] = Array((hello,1), (world,1), (hello,1), (hello,1), (world,1))scala> val groupByData = wordWithOne.groupBy(_._1)
groupByData: scala.collection.immutable.Map[String,Array[(String, Int)]] = Map(world -> Array((world,1), (world,1)), hello -> Array((hello,1), (hello,1), (hello,1)))scala> val result = groupByData.mapValues(x => x.map(t=>t._2).sum)
result: scala.collection.immutable.Map[String,Int] = Map(world -> 2, hello -> 3)scala> result.toList.sortBy(-_._2)
res44: List[(String, Int)] = List((hello,3), (world,2))
在scal的命令行工具,定义一个 名称为map的 Map类型的变量
scala> val map = scala.collection.mutable.Map("hadoop" -> 123, "spark" -> 345, "scala" -> 678)
map: scala.collection.mutable.Map[String,Int] = Map(spark -> 345, hadoop -> 123, scala -> 678)
scala> map.get("hadoop")scala> map.get("hadoop")
res4: Option[Int] = Some(123)scala> map.get("flink")
res5: Option[Int] = Nonescala> map.get("hadoop").get
res6: Int = 123scala> map.get("flink").get
java.util.NoSuchElementException: None.get
at scala.None$.get(Option.scala:347)
at scala.None$.get(Option.scala:345)
... 32 elidedscala> map.get("flink").getOrElse("tmp_flink")
res2: Any = tmp_flink
在源码中我们可以看到Opetion是一个抽象的类,抽象类是不能直接使用的,需要通过其他方式复写这个类来实现底层的功能
sealed abstract class Option[+A] extends Product with Serializable
Option的实现类有两个,一个是None,另一个是Some
None类的源码
case object None extends Option[Nothing] { def isEmpty = true def get = throw new NoSuchElementException("None.get") }
Some类的源码
final case class Some[+A](x: A) extends Option[A] { def isEmpty = false def get = x }
scala> class Person(val name:String,val age:Int)
defined class Personscala> val p1 = new Person("scala",10)
p1: Person = Person@65ae6ba4scala> val p2 = new Person("scala",10)
p2: Person = Person@4f47d241scala> p1 == p2
res0: Boolean = false
scala> case class Person(name:String,age:Int)
defined class Personscala> val p3 = Person("scala",10)
p3: Person = Person(scala,10)scala> val p4 = Person("scala",10)
p4: Person = Person(scala,10)scala> p3 == p4
res1: Boolean = true
scala> case class T1
:1: error: case classes without a parameter list are not allowed;
use either case objects or case classes with an explicit `()' as a parameter list.
case class T1
^scala> case Object T1
:1: error: illegal start of definition
case Object T1
^
case class 样例类 必须要有参数列表
case object 样例对象 必须不能加参数列表
1.case class 不用实例化,class调用过程需要实例化
2.什么场景相等会返回:
为什么case class的类能相等,class不相等
case class 一定是重写了 toString,hashCode,equals的方法
3.case class默认就实现了序列化的方法
Trait 类(特质)似于理解java里的接口Interface
如果要用多个接口,在scala
不管抽象类还是Trait,第一个要用extend,后续的都要用with
object MatchApp { /** * 模式匹配 * @param args */ def main(args: Array[String]): Unit = { def matchList(list:List[String]): Unit = { list match { case "bigdata"::Nil => println("Hello: bigdata") // 只能匹配只有bigdata一个元素 case x::y::Nil => println(s"Hi: $x , $y") // 能匹配集合中有两个元素的 case "java"::tail => println("HI:jepson and others") // 匹配java开头的 case _ => println("......") } } matchList(List("bigdata")) matchList(List("hadoop","spark")) matchList(List("java","spark","scala","hadoop")) matchList(List("spark","scala","hadoop","java")) } }
scala 柯里化
/**
* 偏函数 PartialFunction
* A: 输入参数类型
* B:输出参数类型
*
* 包在花括号内没有match的一组case语句
*/
val values = Array("hadoop", "spark", "scala")
val name = values(Random.nextInt(values.length))
def say:PartialFunction[String,String] = {
case "hdfs" => "存储框架"
case "spark" => "计算框架"
case "scala" => "语言"
case _ => "真不知道是啥"
}println(say(name))
def main(args: Array[String]): Unit = { val content = Source.fromFile("data/file.txt") def read(): Unit ={ for(line <- content.getLines()){ println(line) } } read() }