数组 :new Array[Int](8)与Array[Int](8)的区别:第一种8个元素,第二个定义一个值为8的元素
scala> val arr1=new Array[Int](8)
arr1: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0)
scala> val arr2=Array[Int](8)---相当于val arr2=Array(8)该数组只有一个元素8,
arr2: Array[Int] = Array(8)
scala> arr1.length
res4: Int = 8
scala> arr2.length
res5: Int = 1
scala> arr1(0)
res0: Int = 0
scala> arr2(0)
res1: Int = 8
scala> arr2(3)
java.lang.ArrayIndexOutOfBoundsException: 3
... 32 elided 说明arr2只有一个元素
scala> arr1(0)=1
scala> arr1(0)
res3: Int = 1
变长数组:scala.collection.mutable.ArrayBuffer
1、定义变长数组arr4
scala> val arr4=ArrayBuffer[Int](8)
arr4: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(8)
scala> arr4.length
res7: Int = 1
2、查看arr4中元素:只有一个元素8
scala> arr4
res9: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(8)
2、在数组的开始插入两个数1,2
scala> arr4.insert(0,1,2)
scala> arr4
res12: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 8)
倒序遍历数组元素:reverse函数
scala> for(i<-(0 until arr1.length).reverse) println{arr1(i)}
生成新的数组:yield关键字
scala> val arr=for(e<-arr1) yield e*3
arr: Array[Int] = Array(3, 0, 0, 0, 0, 0, 0, 0)
定长数组变为变长数组:toBuffer函数不会改变原数组类型
scala> arr1.toBuffer
res17: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 0, 0, 0, 0, 0, 0, 0
)
用这种方式接收toBuffer的结果
scala> val arr5=arr1.toBuffer
arr5: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 0, 0, 0, 0, 0, 0, 0)
数组常用算法
排序
scala> arr5.sorted
res18: scala.collection.mutable.Buffer[Int] = ArrayBuffer(0, 0, 0, 0, 0, 0, 0, 1
)
倒序排序
scala> arr5.sortWith(_>_)
res22: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 0, 0, 0, 0, 0, 0, 0
)
scala> arr5.sum
res19: Int = 1
scala> arr5.max
res20: Int = 1
scala> arr5.min
res21: Int = 0
数组中所有元素相加:reduce方法
scala> arr5.reduce(_+_):两个数相加结果赋给第一个占位符的数字,再与第二个占位符的数组相加,循环第一步。
res24: Int = 1
占位符:两个占位符的数字必须不同
map方法:以某种方式改变数组中每个元素的值
scala> arr5.map(_+3)
res25: scala.collection.mutable.Buffer[Int] = ArrayBuffer(4, 3, 3, 3, 3, 3, 3,
)
映射:
不可变映射
scala> val scores=Map("tom"->98,"jerry"->100)
scores: scala.collection.immutable.Map[String,Int] = Map(tom -> 98, jerry -> 100)
scala> val scores=Map(("tom",8),("jerry",100))
scores: scala.collection.immutable.Map[String,Int] = Map(tom -> 8, jerry -> 100)
scala> scores("tom")res26: Int = 8
scala> import scala.collection.mutable.map:12: error: object map is not a member of package scala.collection.mutable
可变映射
import scala.collection.mutable.map
scala> import scala.collection.mutable.Map
import scala.collection.mutable.Map
scala> val scores=Map("tom"->98,"jerry"->100)
scores: scala.collection.mutable.Map[String,Int] = Map(tom -> 98, jerry -> 100)
scala> scores("tom")=101
scala> scores("tom")
res28: Int = 101
小结:var val mutable immutable 定长数组 变长数组
定义可变映射hashmap
scala> val map1=new scala.collection.mutable.HashMap[String,Int]()
map1: scala.collection.mutable.HashMap[String,Int] = Map()
向map集合添加元素的三种方式:
第一种方式:为键赋值的方式
scala> map1("liudehua")=90
scala> map1
res30: scala.collection.mutable.HashMap[String,Int] = Map(liudehua -> 90)
第二种方式:以键值对为一个整体加入
注意这种方式一个键值对要用括号括起来,否则会认为"liuruoying"是第一个键值对,而99是第二个键值对
scala> map1+=("liuruoying",99):15: error: type mismatch;
found : String("liuruoying")
required: (String, Int)
map1+=("liuruoying",99)
正确加入键值对的方式
scala> map1+=(("liuruoying",99))
res33: map1.type = Map(liudehua -> 90, liuruoying -> 99)
第三种方式:put 注意这种方式键值对不加括号
scala> map1.put("zhagngwuji",100)
res34: Option[Int] = None
加括号之后报错:
scala> map1.put(("zhagngwuji",100)):15: error: not enough arguments for method put: (key: String, value: Int)Option[Int].Unspecified value parameter value.
将键值对从hashmap中移除:
当前hashmap中键值对
scala> map1
res37: scala.collection.mutable.HashMap[String,Int] = Map(liudehua -> 90, zhagngwuji -> 100, liuruoying -> 99)
测试1:键值对:错误:要求remove中参数必须是string类型,即key的类型
scala> map1.remove(("zhagngwuji",100))
error: type mismatch; found : (String, Int)
required: String
正确的方式是传递一个key进去:
scala> map1.remove("zhagngwuji")
res40: Option[Int] = Some(100)
scala> map1
res41: scala.collection.mutable.HashMap[String,Int] = Map(liudehua -> 90, liuruo
ying -> 99)
元组:不同类型的值的聚集==tuple(storm(分布式流计算框架)中的地铁5号线)
定义一个元祖
scala> val array=Array((1,"liudehua",20,"F"),(2,"liuruoying"))
array: Array[Product with Serializable] = Array((1,liudehua,20,F), (2,liuruoying
))
元祖转换为映射
错误方式:
scala> array.toMap:15: error: Cannot prove that Product with Serializable <:< (T, U).
array.toMap
正确方式:
scala> val array2=Array((1,"liudehua"),(2,"liuruoying"))
array2: Array[(Int, String)] = Array((1,liudehua), (2,liuruoying))
scala> array2.toMap
res43: scala.collection.immutable.Map[Int,String] = Map(1 -> liudehua, 2 -> liur
uoying)
zip与toMap结合:拉链操作与映射操作
scala> val names=Array("liudehua","liuruoying")
names: Array[String] = Array(liudehua, liuruoying)
scala> val scores=Array(99,98,90)
scores: Array[Int] = Array(99, 98, 90)
scala> names.zip(scores)
res44: Array[(String, Int)] = Array((liudehua,99), (liuruoying,98))
scala> res44.toMap
res45: scala.collection.immutable.Map[String,Int] = Map(liudehua -> 99, liuruoyi
ng -> 98)
序列:不可变序列
scala> val list=List(1,2,3)
list: List[Int] = List(1, 2, 3)
scala> list
res46: List[Int] = List(1, 2, 3)
scala> val list2=List(4,5,6)
list2: List[Int] = List(4, 5, 6)
两个集合合并
scala> list++list2
res48: List[Int] = List(1, 2, 3, 4, 5, 6)
可变序列:
import scala.collection.mutable.ListBuffer
scala> val list0=ListBuffer[Int](1,2,3)
list0: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3)
list是不可变集合,不可在操作符前
scala> list++=list0:17: error: value ++= is not a member of List[Int]
list++=list0
^
而list0才是可变,下面这种方式正确:
scala> list0++=list
res50: list0.type = ListBuffer(1, 2, 3, 1, 2, 3)
list的一些方法:头、尾(尾巴很长)、求和、reduce、map等等
scala> list.head
res51: Int = 1
scala> list.tail
res52: List[Int] = List(2, 3)
scala> list.sum
res53: Int = 6
scala> list.reduce(_+_)
res54: Int = 6
综合例子:wordcount
对比第一天的Wordcount例子
array.flatMap((x:String)=>x.split(" ")).map(x=>(x,1)).reduceByKey((_+_)).collect
可变HashSet的使用:
scala> import scala.collection.mutable.HashSet
import scala.collection.mutable.HashSet
scala> val set2=HashSet(1,2,3)
set2: scala.collection.mutable.HashSet[Int] = Set(1, 2, 3)
scala> set2++=HashSet(3,4,5)
res63: set2.type = Set(1, 5, 2, 3, 4)
io.Source读取文件
scala> import scala.io.Source
import scala.io.Source
scala> lazy val file=Source.fromFile("f:/serenity/scala/word.txt")
file: scala.io.BufferedSource =<lazy>
遍历文件中每一行
scala> for(line<-file.getLines)println(line)
由于file被lazy修饰,因此在调用getLines方法时才去初始化file ,此时报错找不到文件。
java.io.FileNotFoundException: f:\serenity\scala\word.txt (系统找不到指定的文件
。)
总结:以上是函数式编程语法的学习(高阶函数),算子:flatMap map reduceByKey collect