數(shù)組 :new Array[Int](8)與Array[Int](8)的區(qū)別:第一種8個(gè)元素,第二個(gè)定義一個(gè)值為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)---相當(dāng)于val arr2=Array(8)該數(shù)組只有一個(gè)元素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只有一個(gè)元素
scala> arr1(0)=1
scala> arr1(0)
res3: Int = 1
變長(zhǎng)數(shù)組:scala.collection.mutable.ArrayBuffer
1、定義變長(zhǎng)數(shù)組arr4
scala> val arr4=ArrayBuffer[Int](8)
arr4: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(8)
scala> arr4.length
res7: Int = 1
2、查看arr4中元素:只有一個(gè)元素8
scala> arr4
res9: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(8)
2、在數(shù)組的開始插入兩個(gè)數(shù)1,2
scala> arr4.insert(0,1,2)
scala> arr4
res12: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 8)
倒序遍歷數(shù)組元素:reverse函數(shù)
scala> for(i<-(0 until arr1.length).reverse) println{arr1(i)}
生成新的數(shù)組:yield關(guān)鍵字
scala> val arr=for(e<-arr1) yield e*3
arr: Array[Int] = Array(3, 0, 0, 0, 0, 0, 0, 0)
定長(zhǎng)數(shù)組變?yōu)樽冮L(zhǎng)數(shù)組:toBuffer函數(shù)不會(huì)改變?cè)瓟?shù)組類型
scala> arr1.toBuffer
res17: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 0, 0, 0, 0, 0, 0, 0
)
用這種方式接收toBuffer的結(jié)果
scala> val arr5=arr1.toBuffer
arr5: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 0, 0, 0, 0, 0, 0, 0)
數(shù)組常用算法
排序
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
數(shù)組中所有元素相加:reduce方法
scala> arr5.reduce(_+_):兩個(gè)數(shù)相加結(jié)果賦給第一個(gè)占位符的數(shù)字,再與第二個(gè)占位符的數(shù)組相加,循環(huán)第一步。
res24: Int = 1
占位符:兩個(gè)占位符的數(shù)字必須不同
map方法:以某種方式改變數(shù)組中每個(gè)元素的值
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
小結(jié):var val mutable immutable 定長(zhǎng)數(shù)組 變長(zhǎng)數(shù)組
定義可變映射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)
第二種方式:以鍵值對(duì)為一個(gè)整體加入
注意這種方式一個(gè)鍵值對(duì)要用括號(hào)括起來,否則會(huì)認(rèn)為"liuruoying"是第一個(gè)鍵值對(duì),而99是第二個(gè)鍵值對(duì)
scala> map1+=("liuruoying",99):15: error: type mismatch;
found? : String("liuruoying")
required: (String, Int)
map1+=("liuruoying",99)
正確加入鍵值對(duì)的方式
scala> map1+=(("liuruoying",99))
res33: map1.type = Map(liudehua -> 90, liuruoying -> 99)
第三種方式:put ?注意這種方式鍵值對(duì)不加括號(hào)
scala> map1.put("zhagngwuji",100)
res34: Option[Int] = None
加括號(hào)之后報(bào)錯(cuò):
scala> map1.put(("zhagngwuji",100)):15: error: not enough arguments for method put: (key: String, value: Int)Option[Int].Unspecified value parameter value.
將鍵值對(duì)從hashmap中移除:
當(dāng)前hashmap中鍵值對(duì)
scala> map1
res37: scala.collection.mutable.HashMap[String,Int] = Map(liudehua -> 90, zhagngwuji -> 100, liuruoying -> 99)
測(cè)試1:鍵值對(duì):錯(cuò)誤:要求remove中參數(shù)必須是string類型,即key的類型
scala> map1.remove(("zhagngwuji",100))
error: type mismatch; found? : (String, Int)
required: String ? ? ?
正確的方式是傳遞一個(gè)key進(jìn)去:
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(分布式流計(jì)算框架)中的地鐵5號(hào)線)
定義一個(gè)元祖
scala> val array=Array((1,"liudehua",20,"F"),(2,"liuruoying"))
array: Array[Product with Serializable] = Array((1,liudehua,20,F), (2,liuruoying
))
元祖轉(zhuǎn)換為映射
錯(cuò)誤方式:
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結(jié)合:拉鏈操作與映射操作
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)
兩個(gè)集合合并
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的一些方法:頭、尾(尾巴很長(zhǎng))、求和、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
對(duì)比第一天的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修飾,因此在調(diào)用getLines方法時(shí)才去初始化file ,此時(shí)報(bào)錯(cuò)找不到文件。
java.io.FileNotFoundException: f:\serenity\scala\word.txt (系統(tǒng)找不到指定的文件
。)
總結(jié):以上是函數(shù)式編程語法的學(xué)習(xí)(高階函數(shù)),算子:flatMap map reduceByKey collect