簡單理解scala集合上常用的方法
集合上常用的方法
map
1.對映:對集合中的每一個元素進行執行某一項操作
2.返回值型別,正常情況不變,原來集合是什麼型別,就返回什麼型別
3.元素型別,根據我們函式的返回值型別
val f = (x: Int) => x * 10 val arr: Array[Int] = Array(1, 2, 3, 6, 7, 9) //對上面陣列的數字進行處理,每個數字乘以10 //第一種方法 val map1: Array[Int] = arr.map(t => t * 10) println(map1.toList) //第二種方法 val map2: Array[Int] = arr.map((x: Int) => x * 10) println(map2.toList) //第三種方法 val map3: Array[Int] = arr.map(f) println(map3.toList) val map4: Array[Boolean] = arr.map(x => x % 2 == 0) // List(false, true, false, true, false, false) println(map4.toList)
Filter
Filter方法,過濾出滿足條件的資料,返回資料的型別,和之前的一模一樣
val list1: List[Int] = List(1, 2, 4, 7, 8, 3)
val filter1: List[Int] = list1.filter(tp => tp % 2 == 0)
println(filter1)
val filter2: List[Int] = list1.filterNot(tp => tp % 2 == 0)
println(filter2)
//輸出
List(2, 4, 8)
List(1, 7, 3)
Flatten 壓平
val list: List[Array[Int]] = List(Array(1, 2, 3, 56), Array(4, 7, 8, 9)) val flatten: List[Int] = list.flatten println(flatten) //輸出 List(1, 2, 3, 56, 4, 7, 8, 9)
FlatMap=Flatten+Map
scala> val arr=Array("hello spark","hello scala") arr: Array[String] = Array(hello spark, hello scala) scala> val arrSplit=arr.map(str=>str.split(" ")) arrSplit: Array[Array[String]] = Array(Array(hello, spark), Array(hello, "", scala)) scala> arrSplit.flatten res0: Array[String] = Array(hello, spark, hello, "", scala) scala> arr.flatMap(str=>str.split(" ")) res1: Array[String] = Array(hello, spark, hello, "", scala)
Count,find
Count:統計滿足條件的個數
Find:統計滿足條件的第一個元素
val arr: Array[Int] = Array(1, 2, 3, 4, 8, 75, 9, 0)
val count: Int = arr.count(t => t < 3)
println(count)
val list1: List[Int] = List(1, 2, 4, 7, 8, 3)
val count2: Int = list1.count(_ < 3)
println(count2)
val find1: Option[Int] = list1.find(_ > 3)
println(find1)
//輸出
3
2
Some(4)
Foreach
對集合中每一個元素進行操作,返回值:Unit
scala> val list=List(1,4,2,6,7,9,3)
list: List[Int] = List(1, 4, 2, 6, 7, 9, 3)
scala> list.foreach(println)
1
4
2
6
7
9
3
GroupBy 按照指定條件來分組
scala> val arr=Array(("hello",1),("world",1),("hello",1),("spark",1))
arr: Array[(String, Int)] = Array((hello,1), (world,1), (hello,1), (spark,1))
scala> arr.groupBy(x=>x._1)
res3: scala.collection.immutable.Map[String,Array[(String, Int)]] = Map(spark -> Array((spark,1)), world -> Array((world,1)), hello -> Array((hello,1), (hello,1)))
SortBy,SortWith,Sorted
1.Sorted:預設排序
2.SortBy:引數是一個函式,指定排序的規則,預設是升序
3.SortWith:接收兩個引數,兩個引數進行比較
4.在函式中,如果變數只出現一次,那麼此時我們可以使用_來代替
scala> val list=List(1,3,4,5,4,6,3,2,5,6)
list: List[Int] = List(1, 3, 4, 5, 4, 6, 3, 2, 5, 6)
scala> list.sorted
res5: List[Int] = List(1, 2, 3, 3, 4, 4, 5, 5, 6, 6)
scala> list.sortBy(t=>t)
res6: List[Int] = List(1, 2, 3, 3, 4, 4, 5, 5, 6, 6)
scala> list.sortWith((a,b)=>a>b)
res7: List[Int] = List(6, 6, 5, 5, 4, 4, 3, 3, 2, 1)
scala> list.sortWith((a,b)=>a<b)
res8: List[Int] = List(1, 2, 3, 3, 4, 4, 5, 5, 6, 6)
scala> list.sortWith(_>_)
res9: List[Int] = List(6, 6, 5, 5, 4, 4, 3, 3, 2, 1)
scala> list.sortWith(_<_)
res10: List[Int] = List(1, 2, 3, 3, 4, 4, 5, 5, 6, 6)
交集,並集,差集,去重
1.並集:union
2.交集:intersect
3.差集:diff
4.去重:distinct
scala> val arr=Array(1,2,4,5,6,7,2)
arr: Array[Int] = Array(1, 2, 4, 5, 6, 7, 2)
scala> val arr1=Array(1,3,4)
arr1: Array[Int] = Array(1, 3, 4)
//並集
scala> arr union arr1
res15: Array[Int] = Array(1, 2, 4, 5, 6, 7, 2, 1, 3, 4)
//交集
scala> arr intersect arr1
res16: Array[Int] = Array(1, 4)
//arr1的差集
scala> arr diff arr1
res17: Array[Int] = Array(2, 5, 6, 7, 2)
//arr的差集
scala> arr1 diff arr
res18: Array[Int] = Array(3)
//去重
scala> res15.distinct
res19: Array[Int] = Array(1, 2, 4, 5, 6, 7, 3)
Grouped(n:Int)->n是用來分組的個數
按照指定元素個數來分組
scala> val arr=Array(1,3,43,45,5,6,7,3,2,8)
arr: Array[Int] = Array(1, 3, 43, 45, 5, 6, 7, 3, 2, 8)
scala> arr.grouped(2)
res20: Iterator[Array[Int]] = non-empty iterator
scala> res20.foreach(arr=>println(arr.toList))
List(1, 3)
List(43, 45)
List(5, 6)
List(7, 3)
List(2, 8)
MkString
scala> arr
res22: Array[Int] = Array(1, 3, 43, 45, 5, 6, 7, 3, 2, 8)
scala> arr.mkString
res23: String = 134345567328
scala> arr.mkString("@")
res24: String = [email protected]@[email protected]@[email protected]@[email protected]@[email protected]
scala> arr.mkString("^A")
res25: String = 1^A3^A43^A45^A5^A6^A7^A3^A2^A8
To Until其實他們都還可以指定步長
scala> 1 to 10
res31: scala.collection.immutable.Range.Inclusive = Range 1 to 10
scala> 1.to(10,2)
res32: scala.collection.immutable.Range.Inclusive = inexact Range 1 to 10 by 2
scala> 10.to(1,-3)
res33: scala.collection.immutable.Range.Inclusive = Range 10 to 1 by -3
MapValues
和map類似,只不過mapvalues處理的Map集合中的value值,key保持不變
val map1: Map[String, Int] = Map[String, Int]("a" -> 100, "b" -> 200, "c" -> 300)
//第一種方法
println("第一種")
map1.mapValues(t => t * 10).foreach(println)
//第二種方法
println("第二種")
map1.map(t => (t._1, t._2 * 10)).foreach(println)
//輸出
第一種
(a,1000)
(b,2000)
(c,3000)
第二種
(a,1000)
(b,2000)
(c,3000)
Reduce ReduceLeft ReduceRight
1.都是元素的歸併,元素的聚合,具體如何聚合,取決於我們的函式
2.Reduce呼叫的是reduceLeft,但他倆之間的區別是,reduce的引數必須是同一種類型
3.ReduceRight是從右往左進行計算,兩兩計算,再將結果和第三個結果進行計算
4.ReduceLeft也是,只不過是從左往右進行計算
val arr: Array[List[String]] = Array[List[String]](List("1"), List("2"))
val redu1: List[String] = arr.reduce(_ ++ _)
val redu2: List[String] = arr.reduce(_ ::: _)
println("redu1:" + redu1)
println("redu2:" + redu2)
val arr1: Array[Int] = Array(1, 2, 3, 5, 6)
val r1: Int = arr1.reduce(_ + _)
//這個地方的+代表相加
val r11: Int = arr1.reduce(_ - _)
val r12: Int = arr1.reduceLeft(_ - _)
val r13: Int = arr1.reduceRight(_ - _)
//求和
val r14: Int = arr1.reduce((a, b) => a + b)
//乘積
val r15: Int = arr1.reduce(_ * _)
println("r1:" + r1)
println("r11:" + r11)
println("r12:" + r12)
println("r13:" + r13)
println("r14:" + r14)
println("r15:" + r15)
val arr2: Array[String] = Array("a", "b", "c", "d")
val r2: String = arr2.reduce(_ + _) //這個地方的1+代表連線
println("r2:" + r2)
val arr3: Array[List[Int]] = Array(List(1, 2, 3, 5, 6), List(3, 4, 5, 7, 8))
val r3 = arr3.reduce(_ ++ _)
val r31 = arr3.reduce(_ ::: _)
val r32: List[Int] = arr3.reduceRight(_ ::: _)
println("r3:" + r3)
println("r31:" + r31)
println("r32:" + r32)
}
//輸出
redu1:List(1, 2)
redu2:List(1, 2)
r1:17
r11:-15
r12:-15
r13:3
r14:17
r15:180
r2:abcd
r3:List(1, 2, 3, 5, 6, 3, 4, 5, 7, 8)
r31:List(1, 2, 3, 5, 6, 3, 4, 5, 7, 8)
r32:List(1, 2, 3, 5, 6, 3, 4, 5, 7, 8)
Fold FoldLeft FoldRight
1.Fold:帶初始值的歸併,和reduce相比一樣,就比reduce多了有一個初始值
2.FoldLeft:從左向右歸併
3.FoldRight:從右向左歸併
val arr: Array[Int] = Array(1, 3, 5, 7,11)
println(arr.fold(10)(_ + _))
println(arr.foldRight(10)(_ + _))
println(arr.foldLeft(10)(_ + _))
println("=========================")
println(arr.fold(10)(_ - _))
println(arr.foldLeft(10)(_ - _))
//(1-(3-(5-(7-(11-10)))))
//兩兩相減
println(arr.foldRight(10)(_ - _))
//輸出
37
37
37
=========================
-17
-17
-3
Aggregate
Aggregate:聚合
1.單機版的aggregate呼叫的是fold
2.第一個函式:區域性聚合
3.第二個函式:全域性聚合
val arr: Array[List[Int]] = Array[List[Int]](List(1, 3), List(2), List(2, 6))
val result: Int = arr.aggregate(10)((a, b) => a + b.sum, _ + _)
println("result:" + result)
val arr1: Array[Int] = Array(1, 2, 4, 5, 6, 7, 9)
//在scala中,沒有區域性和全域性的概念,只會執行第一個函式,第二個函式不做處理
val result2: Int = arr1.aggregate(0)(_ + _, _ * _)
println("result2:" + result2)
//輸出
result:24
result2:34
初始值和累加值的補充
元素值和累加值是不固定的
val list: List[List[Int]] = List[List[Int]](List(1, 2), List(3), List(5, 7))
//初始值和累加值如何實現
//a:累加值 b:元素值
//累加值:List(1,2) 元素值(3)
//List(1,2)+List(3)=List(1,2,3)
//累加值:List(1,2,3) 元素值(5,7)
//List(1,2,3)+List(5,7)=List(1,2,3,5,7)
//reduce
val result: List[Int] = list.reduce((a, b) => (a ++ b))
//fold
val result2: List[Int] = list.fold(List(11))((a, b) => (a ++ b))
//累加值:List(11) 元素值:List(1,2)
//每個元素的最大值相加:2+5+7
val result3: Int = list.aggregate(0)((a, b) => a + b.max, _ + _)
println("redult:" + result)
println("redult2:" + result2)
println("redult3:" + result3)
//輸出
redult:List(1, 2, 3, 5, 7)
redult2:List(11, 1, 2, 3, 5, 7)
redult3:12
Take(n:Int) Slice
1.take(n):獲取n個元素
2.Slice:擷取元素,傳遞的都是索引值,區間[)
val list: List[Int] = List[Int](1, 2, 3, 6, 7, 9)
//擷取前兩個元素
val result: List[Int] = list.take(2)
println("result:" + result)
val result2: List[Int] = list.slice(0, 3)
println("result2:" + result2)
//輸出
result:List(1, 2)
result2:List(1, 2, 3)