集合计算函数
1、集合计算简单函数
1 object Test13_SimpleFunction { 2 def main(args: Array[String]): Unit = { 3 val list = List(-1, -2, 1, 2, 3, 4, 5, 6) 4 val list1 = List(("a", 22), ("b", 3), ("c", 33), ("d", 8), ("e", 11)) 5 6 //1、求和 7 var sum = 0 8 for (elem <- list) { 9 sum += elem 10 } 11 println(sum) 12 //另一种 13 println(list.sum) 14 15 //2、求乘 16 println(list.product) 17 18 //3、最大值、最小值 19 println(list.max) 20 println(list.min) 21 22 //查看集合中元组的最大值和最小值 23 println(list1.maxBy(_._2)) 24 println(list1.minBy(_._1)) 25 26 //4、排序 27 //5-1 sorted, 从小到大 28 val sortlist = list.sorted 29 println(sortlist) 30 31 //传入一个隐式参数,从大到小 32 println(list.sorted(Ordering[Int].reverse)) 33 34 //5-2 sortBy, 从小到大 35 println(list1.sortBy(_._2)) 36 37 //传入一个隐式参数, 从大到小 38 println(list1.sortBy((_._2))(Ordering[Int].reverse)) 39 40 //5-3 sortWith 41 // 从小到大,如果a < b , 不用交换,反之 42 println(list.sortWith( (a: Int, b: Int) => {a < b})) 43 //简写 44 println(list.sortWith(_ < _)) 45 46 //从大到小, 如果a > b , 不用交换,反之 47 println(list.sortWith((a, b) => {a > b})) 48 //简写 49 println(list.sortWith(_>_)) 50 } 51 }
2、集合计算高级函数
1 object Test14_HighLevelFunction { 2 def main(args: Array[String]): Unit = { 3 val list = List(1, 2, 3, 4, 5, 6, 7 ,8 , 9) 4 5 //1、过滤, 选取偶数 6 val evenlist = list.filter((elem:Int) => {elem % 2 ==0}) 7 println(evenlist) 8 //简化, 选取奇数 9 println(list.filter(_ %2 == 1)) 10 println("-----------------------") 11 12 //2、map 13 //把集合中每个数乘2 14 println(list.map(_ * 2)) 15 //平方 16 println(list.map(x => x * x)) 17 println("------------------------") 18 19 //3、扁平化 20 val nestedList: List[List[Int]] = List(List(1, 2, 3), List(4, 5, 6), List(7, 8, 9)) 21 val flatList = nestedList(0) ::: nestedList(1) ::: nestedList(2) 22 println(flatList) 23 24 val flatList2 = nestedList.flatten 25 println(flatList2) 26 println("---------------------------") 27 28 //4、扁平映射 29 //将一组字符创进行分词,并保存成单词的列表 30 val string: List[String] = List("hello java", "hello scala", "hello world", "hello scala") 31 val splitList = string.map(string => string.split(", ")) 32 //扁平化处理 33 val flattenList = splitList.flatten 34 println(flattenList) 35 36 //合起来叫扁平映射 37 val flatmapList = string.flatMap(_.split(",")) 38 println(flatmapList) 39 println("-----------------------") 40 41 //5、分组groupBy 42 //分成奇偶两组 43 val groupMap = list.groupBy(_ % 2) 44 val groupMap2 = list.groupBy(data => if (data %2 ==0) "偶数" else "奇数") 45 println(groupMap) 46 println(groupMap2) 47 48 //给定一组词汇,按照单词的首字母进行分组 49 val wordList = List("china", "ccc", "aaa", "fff", "dddd", "aaaa", "dd") 50 println(wordList.groupBy((_.charAt(0)))) 51 } 52 }
1 object Test15_HighLevelFunction_Reduce { 2 def main(args: Array[String]): Unit = { 3 val list = List(1, 2, 3, 4, 5) 4 5 //1、reduce 6 println(list.reduce( ((a, b) => a + b) )) 7 //从左往右加 8 println(list.reduceLeft( _ + _)) 9 //从右往左加 10 println(list.reduceRight(_ + _)) 11 println("------------------") 12 13 val list1 = List(3, 5 ,6 ,7 ) 14 println(list1.reduce(_-_)) 15 //从左往右减 16 println(list1.reduceLeft(_-_)) 17 // 3- (5- (6- 7)) 18 println(list1.reduceRight(_-_)) 19 20 //fold 21 //10 + 1 + 2 + 3 + 4 + 5 22 println(list.fold(10)(_ + _)) 23 //0 - 1 - 2 - 3 - 4 - 5 - 6 24 println(list.foldLeft(0)(_ - _)) 25 26 } 27 }