@Scala Day05
可变数组:
ArrayBuffer
创建:
1. ArrayBuffer(1,2,3)
2. new ArrayBufferInt
:+
+:
++
一般用于可变集合, 表示原地修改集合
+= 尾部
+=: 头部
++= 把后面的集合的元素合并到前面的集合中
++=:
-= 删除元素, 只删除满足的第一个 (set用的比较多)
–= 求差集
package com.syf.scala1015day05
import scala.collection.mutable.ArrayBuffer
/**
* Author Amily
* Date 2021/5/9
*/
object Array2 {
def main(args: Array[String]): Unit = {
// ArrayBuffer.apply(1,2,3,4)
// val buffer2 = new ArrayBuffer[Int]()
val buffer1: ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 1)
val buffer2: ArrayBuffer[Int] = ArrayBuffer(1, 2, 30, 40)
// val b2 = 100 +: buffer1 :+ 10
// println(buffer1)
// 原地修改buffer1, 在他的尾部添加元素
// buffer1 += 100
// 200 +=: buffer1 //
// println(buffer1)
// val b3 = buffer1 ++ buffer2
// buffer1 ++= buffer2 // 把buffer2 的集合追加到buffer1的后面, 更新的是buffer1
// buffer2.++:(buffer1)
// buffer1 ++=: buffer2 // 把buffer1的元素添加到buffer2的前面, 更新的是buffer2
// buffer1 -= 1
// buffer1 --= buffer2
// buffer1 -= (1,2,3) // 已经不推荐使用了
// println(buffer1)
}
}
package com.syf.scala1015day05
import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
/**
* Author Amily
* Date 2021/5/9
*/
object Array3 {
def main(args: Array[String]): Unit = {
// val arr1 = Array(30, 50, 70, 60, 10, 20)
// val buffer: mutable.Buffer[Int] = arr1.toBuffer
// println(buffer)
val b: ArrayBuffer[Int] = ArrayBuffer(10, 20)
println(b.toArray.mkString(","))
}
}
package com.syf.scala1015day05
/**
* Author Amily
* Date 2021/5/9
*/
object Array4 {
def main(args: Array[String]): Unit = {
// 多维数组 (二维数组), 假的, 用一维数组模拟的多维数组
// 2 * 3的
val arr: Array[Array[Int]] = Array.ofDim[Int](2, 3)
// println(arr(0)(1))
for (a1 <- arr) {
for (elem <- a1) {
println(elem)
}
}
}
}
List
就是列表
默认不可变
List专用:
:: 在头部添加元素
::: 合并两个List
package com.syf.scala1015day05
object List1 {
def main(args: Array[String]): Unit = {
// 1.创建有元素的集合
val l1 = List(1, 2, 3)
// 2. 空List
val l2 = List[Int]()
// 3. 空集合
val l3 = Nil
// 4. 向List添加元素
// val l4 = 200 +: l1 :+ 100
val l4 = 200 :: 100 :: l1
println(l4)
}
}
package com.syf.scala1015day05
object List2 {
def main(args: Array[String]): Unit = {
// val list1 = List(30, 50, 70, 60, 10, 20)
// val list2 = List(3, 5, 7, 6, 1, 2)
// val list3 = list1 ++ list2
// val list3 = list1 ::: list2 // === list2.:::(list1)
// println(list3)
// 有人用过
val l1 = ::[Int](1, 1 :: Nil)
println(l1)
}
}
+=
++=
一般只能用于可变结合
其实也可以用户不可变集合
是产生换一个新的不可变集合, 然后把新的不可变集合赋值变量
package com.syf.scala1015day05
import scala.collection.mutable.ArrayBuffer
/**
* Author atguigu
* Date 2020/3/7 9:48
*/
object List3 {
def main(args: Array[String]): Unit = {
var arr1 = ArrayBuffer(30, 50, 70, 60, 10, 20)
println(System.identityHashCode(arr1))
arr1 :+= 100
println(System.identityHashCode(arr1))
println(arr1.mkString(", "))
}
}
Map映射
默认不可变, 具体的实现是HashMap
scala的Map, 把键和值当做元组(对偶)来处理
package com.syf.scala1015day05
/**
* Author Amily
* Date 2021/5/9
*/
object Map1 {
def main(args: Array[String]): Unit = {
// val map1: Map[String, Int] = Map[String, Int](("a", 97), ("b", 98), ("c", 99), ("d", 98))
/*for (kv <- map1) {
println(kv)
}*/
/*for (kv <- map1) {
println(kv._1)
}*/
/*for ((k, _) <- map1) {
println(k)
}*/
/*for ((k, 98) <- map1) {
println(k)
}*/
}
}
package com.syf.scala1015day05
/**
* Author Amily
* Date 2021/5/9
*/
object Map2 {
def main(args: Array[String]): Unit = {
val map1: Map[String, Int] = Map[String, Int](
"a" -> 97,
"x"-> 0,
"b" -> 98,
"c" -> 99)
val map2: Map[String, Int] = Map[String, Int](
"a" -> 970,
"z"->1,
"b" -> 980,
"c" -> 990)
// val map2 = map1 + ("a" -> 102)
val map3 = map1 ++ map2
println(map3)
}
}
package com.syf.scala1015day05
/**
* Author Amily
* Date 2021/5/9
*/
object Map3 {
def main(args: Array[String]): Unit = {
val map1: Map[String, Int] = Map[String, Int](
"a" -> 97,
"x" -> 0,
"b" -> 98,
"c" -> 99)
// 根据key获取值
// val v1: Int = map1("a")
// val v1: Int = map1("f") // 会抛异常
// val v1= map1.get("f")
// 存在就返回对应的value, key不存在, 就返回默认值
val v1 = map1.getOrElse("f", 100)
println(v1)
}
}
package com.syf.scala1015day05
import scala.collection.mutable
/**
* Author Amily
* Date 2021/5/9
*/
object Map4 {
def main(args: Array[String]): Unit = {
val map1: mutable.Map[String, Int] = mutable.Map[String, Int](
"a" -> 97,
"x" -> 0,
"b" -> 98,
"c" -> 99)
// map1 += (("aa", 100))
// map1 += "aa"-> 100
// val v1 = map1.getOrElse("f", 100)
// 如果key'不存在, 则会组合一个新的kv, 添加到可变的map中
// val v1 = map1.getOrElseUpdate("f", 100)
// println(v1)
map1("a") = 100 // 更加scala
map1("f") = 100
map1.update("dd", 200) // 更加java
println(map1)
}
}
拉链:
package com.syf.scala1015day05
/**
* Author Amily
* Date 2021/5/9
*/
object Zip1 {
def main(args: Array[String]): Unit = {
val list1 = List(30, 50, 70, 60, 10)
val list2 = List(3, 5, 7, 6, 1, 2)
// 1. 多余的会被抛弃
// val list3: List[(Int, Int)] = list1.zip(list2)
// 2. 多余会使用默认值来进行匹配
// val list3: List[(Int, Int)] = list1.zipAll(list2, -1, -2)
// 3. 和自己的索引进行zip
val list3: List[(Int, Int)] = list1.zipWithIndex
for ((e,i) <- list3) {
println(i)
}
println(list3)
}
}
package com.syf.scala1015day05
/**
* Author Amily
* Date 2021/5/9
*/
package com.atguigu.scalal1015.day05.collections
object Zip2 {
def main(args: Array[String]): Unit = {
val list = Map("a" -> 1, "b" -> 2, "c" -> 3)
// list中存储的是二维的元组的时候, 才能使用unzip
val t= list.unzip
println(t)
}
}
队列:
FIFO
提供了两个专门操作队列的元素:
入队
出队
栈:
FILO
专门操作栈的元素
push 入栈
pop 出栈
栈顶和栈底
package com.syf.scala1015day05
import scala.collection.mutable
/**
* Author Amily
* Date 2021/5/9
*/
object Queue1 {
def main(args: Array[String]): Unit = {
/*val q1: mutable.Queue[Int] = mutable.Queue[Int](10, 20, 30)
q1.enqueue(100, 200)
val v = q1.dequeue()
println(q1)
println(v)*/
val s1: mutable.Stack[Int] = mutable.Stack[Int](10, 20, 30)
println(s1)
val p: Int = s1.pop()
s1.push(100)
val p2: Int = s1.pop()
println(p2)
}
}
foreach和部分应用函数
package com.syf.scala1015day05
/**
* Author Amily
* Date 2021/5/9
*/
object ForeachDemo1 {
def main(args: Array[String]): Unit = {
val list1 = List(30, 50, 70, 60, 10, 20)
// list1.foreach(x => println(x))
list1.foreach(println)
list1.foreach(println(_))
}
}
filter操作
package com.syf.scala1015day05
object FilterDemo1 {
def main(args: Array[String]): Unit = {
val list1 = List(30, 5, 7, 60, 1, 20)
// val list2 = list1.filter(x => x % 2 == 1)
val list2 = list1.filter(_ % 2 == 1)
println(list2)
}
}
reduce
package com.syf.scala1015day05
object ReduceDemo1 {
def main(args: Array[String]): Unit = {
// 聚合操作
val list1 = List(30, 50, 70, 60, 10, 20)
// val result: Int = list1.reduce((x, y) => x + y)
// val result: Int = list1.reduce(_ + _)
val result: Int = list1.reduceRight(_ + _)
println(result)
}
}
groupby的使用
package com.syf.scala1015day05
/**
* Author Amily
* Date 2021/5/9
*/
object GroupByDemo1 {
def main(args: Array[String]): Unit = {
/*val list1 = List(30, 5, 7, 60, 1, 20)
val map = list1.groupBy(x => if(x % 2 == 0) "偶数" else "奇数")
println(map)*/
val list1 = List("hello", "hello", "world", "atguigu", "hello", "world")
val wordMap: Map[String, List[String]] = list1.groupBy(x => x)
// 对wordMap做一个结构调整
val wordCount = wordMap.map(kv => {
(kv._1, kv._2.size)
})
println(wordCount)
}
}
WordCoun1
package com.syf.scala1015day05
import scala.io.Source
/**
* Author Amily
* Date 2021/5/9
*/
object WordCoun1 {
def main(args: Array[String]): Unit = {
// 读取一个文件的内容, 统计这个文件中,每个单词出现的次数
val path =
"""C:\Users\lzc\Desktop\class_code\2019_10_15\01_scala\scala1015\src\main\scala\com\atguigu\scalal1015\day05\high\WordCoun1.scala"""
// 1. 读文件内容, 放入到集合中 文件中的每一行
val lines = Source.fromFile(path, "utf-8").getLines().toList
// 2. 切割单词 使用非单词字符
val words = lines.flatMap(_.split("\\W+"))
// 3. 把相同的单词分组
val wordGrouped = words.groupBy(w => w)
// 4. 进行map, 计算每个单词的个数
/*val wordCount = wordGrouped.map(kv => {
(kv._1, kv._2.size)
})*/
// val wordCount =wordGrouped.mapValues(v => v.size)
val wordCount = wordGrouped.mapValues(_.size)
// 按照单词的数量降序, 取top3
val result = wordCount.toList.sortBy(_._2)(Ordering.Int.reverse).take(3)
println(result)
}
}
WordCoun2
package com.syf.scala1015day05
import scala.io.Source
/**
* Author Amily
* Date 2021/5/9
*/
object WordCoun2 {
def main(args: Array[String]): Unit = {
// 读取一个文件的内容, 统计这个文件中,每个单词出现的次数
val path = """C:\Users\lzc\Desktop\class_code\2019_10_15\01_scala\scala1015\src\main\scala\com\atguigu\scalal1015\day05\high\WordCoun1.scala"""
// 1. 读文件内容, 放入到集合中 文件中的每一行
val lines = Source.fromFile(path, "utf-8").getLines().toList
// 2. 切割单词 使用非单词字符
val words = lines.flatMap(_.split("\\W+")).filter(_.length > 0)
// 3. 把相同的单词分组
val wordGrouped = words.groupBy(w => w)
// 4. 进行map, 计算每个单词的个数
/*val wordCount = wordGrouped.map(kv => {
(kv._1, kv._2.size)
})*/
// val wordCount =wordGrouped.mapValues(v => v.size)
val wordCount =wordGrouped.mapValues(_.size)
println(wordCount)
}
}
WordCoun3
package com.syf.scala1015day05
/**
* Author Amily
* Date 2021/5/9
*/
object WordCount3 {
def main(args: Array[String]): Unit = {
val tupleList = List(
("Hello hello Scala Spark World", 4),
("Hello Scala Spark", 3),
("Hello Scala", 2),
("Hello", 1))
// Map(hello-> .., scala->...)
// 方法: 1
/*val result = tupleList
.map(kv => (kv._1 + " ") * kv._2)
.flatMap(_.split(" "))
.filter(_.length > 0)
.groupBy(x => x)
.mapValues(_.length)*/
// 方法2: (hello,4), (hello, 4), ...
val wordCoun1: List[(String, Int)] = tupleList.flatMap(kv => {
val line = kv._1 // "Hello hello Scala Spark World"
val count = kv._2 // 4
// line.split(" ").map(x => (x, count))
line.split(" ").map((_, count))
})
println(wordCoun1)
val wordCount1Group = wordCoun1.groupBy(_._1.toLowerCase)
val result = wordCount1Group.mapValues(wordCountList => {
wordCountList.foldLeft(0)(_ + _._2)
})
println(result)
}
}