Scala列表数组学习
数组
不可变数组——定义、查询、增加、循环
//定义数据 var arr: Array[Int] = new Array[Int](5) var arr2 = Array(2, 3, 42, 21, 3) //循环以及查询数据 for (i <- 0 until arr2.length) { println(arr2(i)) } for (i <- arr2.indices) { println(arr2(i)) } for (elem <- arr2) { println(elem) } var iter = arr2.iterator while (iter.hasNext) println(iter.next()) arr2.foreach(println) //增加数据 var newArr = arr2.:+(73) println(newArr)
可变数组——定义,增加(循环与查询与不可变的一样)
//可变数组 var arr3: ArrayBuffer[Int] = new ArrayBuffer[Int]() var arr4 = ArrayBuffer(23, 21, 32) arr3.append(32) arr3.insert(0, 4) arr4.append(5) println(arr3) // (4, 32) println(arr4) // (23, 21, 32, 5)
列表操作
// 创建不可变列表 var list1 = List(1, 2, 3) list1.foreach(println) var list5 = 32 :: 14 :: Nil var list6 = 21 :: 1 :: Nil var list7 = list5 ::: list6 println(list7) // 创建可变列表 val list8 : ListBuffer[Int] = new ListBuffer[Int]() list8.append(15, 23) val list9 = ListBuffer(12, 52, 21) var list3 = list8 ++ list9
Set操作
// 不可变set val set1 = Set(3, 21, 4) println(set1) val set2 = set1 + 20 val set3 = set1 ++ set2 println(set3) // 可变set val set4: mutable.Set[Int] = mutable.Set(12, 32, 33, 23, 43) set4.add(4) set4.remove(12) println(set4)
Map操作
// 可变map val map2 : mutable.Map[String, Int] = mutable.Map("a"-> 13, "b" -> 32) map2.put("c", 23) map2.remove("a") map2.update("b", 2) for (key <- map2.keys) { println(s"${key} --->${map1.get(key)}" ) } // 如果没有对应键 println(map2.getOrElse("c", 0))
集合并集、交集、差集
val list10 = List(1, 2, 3, 5, 8) val list11 = List(2, 3, 9, 10, 22) // 并集, 如果基础集合为Set,会自动去重 val union = list10.union(list11) println(union) // 交集 val intersection = list10.intersect(list11) println(intersection) // 差集 val diff1 = list10.diff(list11) val diff2 = list11.diff(list10) println(diff1, diff2)
集合计算(求和、求乘积、最大值、最小值、排序)
val list12 = List(2, 3, 9, 10, 22) // 求和 println(list12.sum) // 求乘积 println(list12.product) // 最大值、最小值 println(list12.max) println(list12.min) // 排序 var sortedList = list12.sorted // 正序 println(sortedList) // 倒序 println(sortedList.reverse) // 从小到大排序,以下等同 println(list12.sortWith((a: Int, b: Int) => {a < b})) println(list12.sortWith(_ < _))
集合扁平化、分组计算、聚合、fold、merge合并、wordCount计算
var list15 = List(1, 2, 3, 4, 5, 6, 7, 8, 9) // 把集合中每个数乘2 println(list15.map(_ * 2)) // 扁平化 val nestedList: List[List[Int]] = List(List(1, 2, 3), List(4, 5), List(6,7,8,9)) val flatList = nestedList.flatten println(flatList) // 分组 val groupMap : Map[Int, List[Int]] = list15.groupBy(_ % 2) println(groupMap) // 聚合 val reduceList = list15.reduce(_ - _) // 1-2-3-4-5-6-7-8-9 println(reduceList) // fold println(list15.fold(20)(_ + _)) // 20 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 // merge合并 val map3 = Map("a" -> 1, "b" -> 3, "c" -> 5) val map4 = mutable.Map("a" -> 2, "b" -> 2, "c" -> 4) val map5 = map3.foldLeft(map4)( (mergedMap, kv) => { val key = kv._1 val value = kv._2 mergedMap(key) = mergedMap.getOrElse(key, 0) + value mergedMap } ) println(map5) val StringList : List[String] = List( "hello", "hello spark ", "hello world", "hello scala", "hello spark from", "hello scala hehe" ) val wordList = StringList.flatMap(_.split(" ")) println(wordList) val groupMap1: Map[String, List[String]] = wordList.groupBy(word => word) val stringToInt: Map[String, Int] = groupMap1.map(kv => (kv._1, kv._2.length)) val sortList2: List[(String, Int)] = stringToInt.toList.sortWith(_._2 > _._2) println(sortList2)