元組Tuplehtml
Array ArrayByffferjava
List ListBufferes6
Map HashMap數組
Setapp
scala.collection.mutable._ide
sorted sortBy sortwith函數
scala> import scala.collection._oop import scala.collection._ui
scala> var map = mutable.Map(""->1)es5 map: scala.collection.mutable.Map[String,Int] = Map("" -> 1)
scala> var map = immutable.Map(1->1) map: scala.collection.immutable.Map[Int,Int] = Map(1 -> 1) |
集合上的方法
map方法
scala> val change=(x:Int)=>x*10 change: Int => Int = <function1>
scala> arr.map(change) res1: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70)
scala> arr.map(change) res2: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70)
scala> arr.map((x:Int)=>x*10) res3: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70)
scala> arr.map(x=>x*10) res4: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70)
scala> arr.map(_*10) res5: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70) |
scala> arr.map(x=>if(x%2==1)x*10 else x) res6: Array[Int] = Array(10, 2, 30, 4, 50, 6, 70)
scala> arr.map(if(_%2==1)_*10 else _) <console>:16: error: missing parameter type for expanded function ((x$1) => x$1.$percent(2).$eq$eq(1)) arr.map(if(_%2==1)_*10 else _) ^ <console>:16: error: missing parameter type for expanded function ((x$2) => x$2.$times(10)) arr.map(if(_%2==1)_*10 else _) ^
scala> var arr = Array(1,3,2,4,13,11,9) arr: Array[Int] = Array(1, 3, 2, 4, 13, 11, 9)
scala> arr.sortBy(x=>x) res8: Array[Int] = Array(1, 2, 3, 4, 9, 11, 13)
scala> arr.sortBy(_) <console>:16: error: missing parameter type for expanded function ((x$1) => arr.sortBy(x$1)) arr.sortBy(_)
scala> arr.sortBy(-_) res10: Array[Int] = Array(13, 11, 9, 4, 3, 2, 1)
scala> arr.sortBy(3*_) res11: Array[Int] = Array(1, 2, 3, 4, 9, 11, 13) |
下劃線的使用
map方法將一個集合中的每一個元素都進行遍歷處理,怎麼處理是map方法中的函數處理結果,返回的集合類型不變,可是集合中的元素可能會發生改變,元素個數不變
scala> arr res12: Array[Int] = Array(1, 3, 2, 4, 13, 11, 9)
scala> arr.map(_>3) res13: Array[Boolean] = Array(false, false, false, true, true, true, true)
scala> var arr = Array(("zhangsan",2000),("lisi",3000),("zhaosi",3500)) arr: Array[(String, Int)] = Array((zhangsan,2000), (lisi,3000), (zhaosi,3500))
scala> arr.map(t=>t._2*10) res14: Array[Int] = Array(20000, 30000, 35000)
scala> arr.map(t=>(t._1,t._2*10)) res15: Array[(String, Int)] = Array((zhangsan,20000), (lisi,30000), (zhaosi,35000)) |
list set map
filter進行條件過濾
scala> arr.filter(t=>t._2>2500) res17: Array[(String, Int)] = Array((lisi,3000), (zhaosi,3500))
scala> var arr = Array(1,2,3,4,5,6,7,8) arr: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)
scala> arr.filter(_%2==0) res18: Array[Int] = Array(2, 4, 6, 8)
scala> arr.filterNot(_%2==0) res19: Array[Int] = Array(1, 3, 5, 7) |
filterNot與filter相反,符合條件的元素都不要
壓平flatten
scala> var arr = Array(1,2,3,4) arr: Array[Int] = Array(1, 2, 3, 4)
scala> var arr1 = Array(arr,arr,arr) arr1: Array[Array[Int]] = Array(Array(1, 2, 3, 4), Array(1, 2, 3, 4), Array(1, 2, 3, 4))
scala> arr1.map(t=>t.map(_*10)) res20: Array[Array[Int]] = Array(Array(10, 20, 30, 40), Array(10, 20, 30, 40), Array(10, 20, 30, 40))
scala> arr1.flatten res21: Array[Int] = Array(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4)
scala> var arr2 = Array(arr1,arr1,arr1) arr2: Array[Array[Array[Int]]] = Array(Array(Array(1, 2, 3, 4), Array(1, 2, 3, 4), Array(1, 2, 3, 4)), Array(Array(1, 2, 3, 4), Array(1, 2, 3, 4), Array(1, 2, 3, 4)), Array(Array(1, 2, 3, 4), Array(1, 2, 3, 4), Array(1, 2, 3, 4)))
scala> arr2.flatten res22: Array[Array[Int]] = Array(Array(1, 2, 3, 4), Array(1, 2, 3, 4), Array(1, 2, 3, 4), Array(1, 2, 3, 4), Array(1, 2, 3, 4), Array(1, 2, 3, 4), Array(1, 2, 3, 4), Array(1, 2, 3, 4), Array(1, 2, 3, 4)) |
flatten是壓平的意思
scala> var arr = Array("hello world hello jack","hello tom tom spark","jerry lucy") arr: Array[String] = Array(hello world hello jack, hello tom tom spark, jerry lucy)
scala> arr.map(_.split(" ")) res23: Array[Array[String]] = Array(Array(hello, world, hello, jack), Array(hello, tom, tom, spark), Array(jerry, lucy))
scala> res23.flatten res24: Array[String] = Array(hello, world, hello, jack, hello, tom, tom, spark, jerry, lucy) |
練習題:
scala> var arr = Array("a","b","c","d") arr: Array[String] = Array(a, b, c, d)
scala> var arr = Array("e","f","c","d") arr: Array[String] = Array(e, f, c, d)
scala> var arr1 = Array("a","b","c","d") arr1: Array[String] = Array(a, b, c, d)
scala> arr zip arr1 res25: Array[(String, String)] = Array((e,a), (f,b), (c,c), (d,d)) 變成 Array(a,b,c,d,e,f,....)
scala> res25.map(t=>Array(t._1,t._2)) res27: Array[Array[String]] = Array(Array(e, a), Array(f, b), Array(c, c), Array(d, d))
scala> res27.flatten res28: Array[String] = Array(e, a, f, b, c, c, d, d) |
flatten操做通常都攜帶map
flatMap = map+flatten
scala> res25.flatMap(t=>Array(t._1,t._2)) res31: Array[String] = Array(e, a, f, b, c, c, d, d) |
groupby按照條件進行分組
scala> var arr = Array(("hello",1),("hello",1),("word",1)) arr: Array[(String, Int)] = Array((hello,1), (hello,1), (word,1))
scala> arr.groupBy(t=>t._1) res32: scala.collection.immutable.Map[String,Array[(String, Int)]] = Map(word -> Array((word,1)), hello -> Array((hello,1), (hello,1)))
scala> var arr = Array(1,2,3,4,5,5,6,7,8) arr: Array[Int] = Array(1, 2, 3, 4, 5, 5, 6, 7, 8)
scala> arr.groupBy(_>5) res33: scala.collection.immutable.Map[Boolean,Array[Int]] = Map(false -> Array(1, 2, 3, 4, 5, 5), true -> Array(6, 7, 8))
scala> arr.groupBy(x=>if(x>5)"gt 5" else "lte 5") res34: scala.collection.immutable.Map[String,Array[Int]] = Map(gt 5 -> Array(6, 7, 8), lte 5 -> Array(1, 2, 3, 4, 5, 5))
scala> var arr = Array("hello","hey","hi","spark","smart","sun") scala> arr.groupBy(_.substring(0,1)) res36: scala.collection.immutable.Map[String,Array[String]] = Map(h -> Array(hello, hey, hi), s -> Array(spark, smart, sun))
scala> var arr = Array(("hello",5),("tom",3),("spark",32)) arr: Array[(String, Int)] = Array((hello,5), (tom,3), (spark,32))
scala> arr.groupBy(t=>t._2>10) res37: scala.collection.immutable.Map[Boolean,Array[(String, Int)]] = Map(false -> Array((hello,5), (tom,3)), true -> Array((spark,32))) |
groupby的返回值是一個map集合,key是分組的元素,value值是原來數據的一個片斷
grouped分組(片斷分組)
scala> arr res46: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)
scala> arr.grouped(2) res47: Iterator[Array[Int]] = non-empty iterator
scala> res47.foreach(x=>println(x.toList)) List(1, 2) List(3, 4) List(5, 6) List(7, 8)
scala> arr.grouped(2) res49: Iterator[Array[Int]] = non-empty iterator
scala> res49.foreach(println(_.toList)) <console>:17: error: missing parameter type for expanded function ((x$1) => x$1.toList) res49.foreach(println(_.toList)) |
scala> arr.grouped(2) res49: Iterator[Array[Int]] = non-empty iterator
scala> res49.foreach(println(_.toList)) <console>:17: error: missing parameter type for expanded function ((x$1) => x$1.toList) res49.foreach(println(_.toList)) ^
scala> val func = (x:Int)=>println(x) func: Int => Unit = <function1>
scala> arr.foreach(println) 1 2 3 4 5 6 7 8
scala> arr.foreach(func) 注意函數和方法的調用規範,注意函數和方法的使用狀況 var func = (x:Int)=>println(x)這種狀況下println是func函數中的一個內容,是其中調用的方法 arr.foreach(println) println方法做爲一個函數在函數中做爲參數了 arr.foreach(println(_)) 這個println做爲方法被調用了,至關於在函數中調用方法 arr.foreach(func) |
scala中的wordcount
object WordCount { |
集合的並集 交集 差集
union intersect diff
scala> var arr = Array(1,2,3,4,5) arr: Array[Int] = Array(1, 2, 3, 4, 5)
scala> var arr1 = Array(3,4,5,6,7) arr1: Array[Int] = Array(3, 4, 5, 6, 7)
scala> arr union arr1 res53: Array[Int] = Array(1, 2, 3, 4, 5, 3, 4, 5, 6, 7)
scala> arr intersect arr1 res54: Array[Int] = Array(3, 4, 5)
scala> arr diff arr1 res55: Array[Int] = Array(1, 2)
scala> arr1 diff arr res56: Array[Int] = Array(6, 7) |
count方法
scala> arr.count def count(p: Int => Boolean): Int
scala> arr.count(_>3) res59: Int = 2
scala> arr.count(_%2==0) res60: Int = 2 |
符合某個條件的元素有幾個
find找尋,查找
scala> arr res61: Array[Int] = Array(1, 2, 3, 4, 5)
scala> arr.find override def find(p: Int => Boolean): Option[Int]
scala> arr.find(_>3) res62: Option[Int] = Some(4)
scala> arr.find(_>10) res63: Option[Int] = None
scala> res62.get res64: Int = 4 |
符合某個條件的元素,可是隻能查詢出來第一個
to until 步長
scala> 0 to (10,2) res71: scala.collection.immutable.Range.Inclusive = Range(0, 2, 4, 6, 8, 10)
scala> var arr = Array(1,2,3,4,5,6,7,8,9,10) arr: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
scala> for(e<- 1 to (arr.length-1,2))yield arr(e) res72: scala.collection.immutable.IndexedSeq[Int] = Vector(2, 4, 6, 8, 10) |
隨堂練習:
1.寫一個函數或者方法 getValue(x:Array[Int],v:Int)求出這個數組中大於v的值得個數,小於v得值得個數,等於v這個值得個數,要求一次性返回
def getReverse2(arr:Array[Int]):String={ |
mkString()能夠指定集合中元素得分隔符,拼接成字符串
做業題:
val d1 = Array(("bj", 28.1), ("sh", 28.7), ("gz", 32.0), ("sz", 33.1))
val d2 = Array(("bj", 27.3), ("sh", 30.1), ("gz", 33.3))
val d3 = Array(("bj", 28.2), ("sh", 29.1), ("gz", 32.0), ("sz", 30.5))
以上每一個城市得溫度,求出每一個城市溫度得平均值
答案https://blog.csdn.net/qq_43006059/article/details/83414216
保留小數
第二題:
val lst = List("Id1-The Spark", "Id2-The Hadoop", "Id3-The Spark")
https://wenku.baidu.com/view/570229b3e109581b6bd97f19227916888486b9ce.html
mapValue方法處理的是map集合中的數據
scala> arr.map(t=>(t._1,t._2+10)) res74: scala.collection.Map[String,Int] = Map(zhangsan -> 30, lisi -> 40, wangwu -> 35)
scala> arr.mapValues(_+10) res75: scala.collection.Map[String,Int] = Map(zhangsan -> 30, lisi -> 40, wangwu -> 35)
scala> val arr = Array(("zhangsan",20),("lisi",30),("wangwu",25)) arr: Array[(String, Int)] = Array((zhangsan,20), (lisi,30), (wangwu,25))
scala> arr.mapValues(_+10) <console>:16: error: value mapValues is not a member of Array[(String, Int)] arr.mapValues(_+10) |
mapvalues是遍歷map集合中每個元素的value值,key不變,只能遍歷map不能遍歷數組形式的元組
reduce reduceLeft reduceRight
scala> arr.reduce def reduce[A1 >: Int](op: (A1, A1) => A1): A1
scala> arr.reduce((a,b)=>a+b) res78: Int = 28
scala> arr.reduce(_+_) res79: Int = 28
scala> var arr =Array(("zhangsan",30000),("reba",800000),("nazha",750000)) arr: Array[(String, Int)] = Array((zhangsan,30000), (reba,800000), (nazha,750000))
scala> arr.map(_._2.sum) <console>:16: error: value sum is not a member of Int arr.map(_._2.sum) ^
scala> arr.map(_._2).sum res81: Int = 1580000
scala> arr.reduce((a,b)=>a._2+b._2) <console>:16: error: type mismatch; found : Int required: (String, Int) arr.reduce((a,b)=>a._2+b._2) |
def reduce[A1 >: A](op: (A1, A1) => A1): A1
源碼中規定reduce中放入的函數參數類型,返回值類型,都必須是調用reduce的集合中泛型的本類型和父類型
scala> arr.reduce((a,b)=>a) res84: (String, Int) = (zhangsan,30000)
scala> arr.reduce((a,b)=>("",a._2+b._2)) res85: (String, Int) = ("",1580000) |
scala> arr.reduce((a:Any,b:Any)=>a.asInstanceOf[(String,Int)]._2+b.asInstanceOf[(String,Int)]._2) java.lang.ClassCastException: java.lang.Integer cannot be cast to scala.Tuple2 at $anonfun$1.apply(<console>:16) at $anonfun$1.apply(<console>:16) at scala.collection.IndexedSeqOptimized$class.foldl(IndexedSeqOptimized.scala:57) at scala.collection.IndexedSeqOptimized$class.reduceLeft(IndexedSeqOptimized.scala:74) at scala.collection.mutable.ArrayOps$ofRef.reduceLeft(ArrayOps.scala:186) at scala.collection.TraversableOnce$class.reduce(TraversableOnce.scala:208) at scala.collection.mutable.ArrayOps$ofRef.reduce(ArrayOps.scala:186) ... 32 elided
scala> arr.reduce((a:Any,b:Any)=>("",a.asInstanceOf[(String,Int)]._2+b.asInstanceOf[(String,Int)]._2)) res88: (String, Int) = ("",1580000) |
注意reduce接收的數據和返回值的數據必須是一個種類的
def reduce[A1 >: A](op: (A1, A1) => A1): A1 = reduceLeft(op)
reduce的底層使用的是reduceLeft
reduceLeft和reduce是一個方法,可是reduceLeft容許參數和返回值不同
scala> arr.reduce((a:Any,b:(String,Int))=>("",a.asInstanceOf[(String,Int)]._2+b._2).asInstanceOf[Any]) <console>:16: error: type mismatch; found : (Any, (String, Int)) => Any required: (Any, Any) => Any arr.reduce((a:Any,b:(String,Int))=>("",a.asInstanceOf[(String,Int)]._2+b._2).asInstanceOf[Any]) ^
scala> arr.reduceLeft((a:Any,b:(String,Int))=>("",a.asInstanceOf[(String,Int)]._2+b._2).asInstanceOf[Any]) res92: Any = ("",1580000) |