sacala 关于集合常用的操作
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)
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 = 1@3@43@45@5@6@7@3@2@8
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
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)