scala学习(3)-集合

本文详细介绍了Scala中的可变数组与不可变数组,以及List、Set、Map等数据结构的使用,包括衍生集合操作、高级计算方法(如排序、过滤、映射等)和并行计算示例,以及实际应用中的单词计数和队列管理。
摘要由CSDN通过智能技术生成
一、可变数组与不可变数组
package chapter01

import scala.collection.mutable.ArrayBuffer

object ArrayTest {

  def main(args: Array[String]): Unit = {

    //不可变数组
    val arr:Array[Int] = new Array[Int](5)

    val arr2 = Array(12,23,34,45,56)

    val ints = arr :+ 12

    ints.foreach(println)

    //可变数组
    val ints1 = new ArrayBuffer[Int]()
    val ints2 = ArrayBuffer(12,23,34)
    println(ints2)

    ints1 += 5 //最后加
    6 +=: ints1 //最前面加
    ints1.append(23) //最后加
    ints1.prepend(34) //最前面加
    ints1.insert(2, 12, 34) //指定位置加
    println(ints1)

    val array = ints2.toArray //可变数组转变成不可变数组
    val buffer = arr2.toBuffer //不可变数组转变成可变数组

  }

}
二、列表List-Set-Map
package chapter01

import scala.collection.mutable.ListBuffer
import scala.collection.mutable

object ListTest {

  def main(args: Array[String]): Unit = {

    val list = List(12, 34, 45)
    val ints = list :+ 10 //后面加
    val list2 = 20 +: list //前面加

    val ints1 = list.::(41) //往前面加
    val ints2 = Nil.::(98)
    val ints3 = 56 :: 23 :: Nil

    //列表合并,扁平化
    val ints4 = ints1 ::: ints2
    val ints5 = ints1 ++ ints2

    //可变list
    val listBuffer = ListBuffer(90, 20)
    val buffer = new ListBuffer[Int]()
    buffer.append(23)
    31 +=: listBuffer += 23 //冒号结尾都是从右到做添加

    val buffer2 = listBuffer ++ buffer
    listBuffer ++=: buffer //listBuffer合并到了buffer里面

    //无序的,不可变的set
    val set1 = Set(12,34,34)
    val set2 = set1 + 20
    val set3 = set1 ++ set2 //合并
    val set4 = set1 - 12
    //可变set
    val set5 = mutable.Set(12,23,34,45)
    set5 += 23
    set5.add(67)
    set5 -= 23
    set5.remove(67)
    val set6 = mutable.Set(89)
    set5 ++= set6 //set6合并到set5

    //不可变Map
    val map1: Map[String, Int] = Map("a" -> 12, "b" -> 13)
    map1.foreach(println)
    map1.foreach((kv: (String, Int)) => println(kv))
    for (key <- map1.keys){
      println(s"$key --> ${map1.get(key).get}")
    }
    println(map1.getOrElse("c", 0))
    //可变Map
    val map2: mutable.Map[String, Int] = mutable.Map("a" -> 12, "b" -> 13)
    map2.put("c", 34)
    map2 += (("d", 23))
    map2.update("d", 90)
    map2.default("a")
    val map3: mutable.Map[String, Int] = mutable.Map("e" -> 12, "b" -> 14)
    map2 ++= map3 //map3合并到map2,相同key取map2的
    val map4 = map2 ++ map3
    
  }

}
三、衍生集合
package chapter01

object DerivedCollection {

  def main(args: Array[String]): Unit = {

    val list1 = List(1,2,3,4)
    val list2 = List(3,4,5,6,7)

    val set1 = Set(1,2,3,4)
    val set2 = Set(3,4,5,6,7)


    println(list1.head)
    println(list1.tail)//去掉头的都是尾
    println(list1.last)
    println(list1.init)//去掉尾的都是头
    println(list1.take(3))
    println(list1.takeRight(2))
    println(list1.drop(3))
    println(list1.dropRight(2))

    //取两个集合的并集。set一样,set会去重
    val union = list1.union(list2)
    println(list1 ::: list2)
    println(set1 ++ set2)

    val intersect = list1.intersect(list2)//交集
    val diff = list1.diff(list2)//差集,属于list1,不属于list2

    val tuples = list1.zip(list2)//拉链一一配对形成元组

    //滑窗(3,4,5), (4,5,6),(5,6,7)形成3个list
    list2.sliding(3, 1)
    
    
  }
  
}
四、高级计算
package chapter01

object CollectionFunc {

  def main(args: Array[String]): Unit = {

    val list1 = List(1,2,20,8,9)
    val list2 = List(5,6,19,8,10)
    val list3 = List(("a", 2), ("b", 1), ("c", 5),("d", 8))

    println(list3.maxBy((tuple: (String, Int)) => tuple._2))
    println(list3.maxBy(_._2))//简化

    println(list1.sorted(Ordering[Int].reverse)) //从大到小

    println(list3.sortBy(_._2)(Ordering[Int].reverse))

    println(list1.sortWith((a:Int, b:Int) => {a < b}))

    println(list1.sortWith(_ < _)) //从小到大

    val ints = list2.filter(_ % 2 == 0)
    val map = list2.map( _ * 2)
    //扁平化
    val nestedList: List[List[Int]] = List(List(1,2,3), List(8,9))
    val flatten = nestedList.flatten
    //扁平映射
    val strings = List("hello, world", "hello java")
    val splits = strings.map(string => string.split(" "))
    val flattenList = splits.flatten
    val flatmapList = strings.flatMap(_.split(" ")) //简化
    //分组group by
    val groupMap = list2.groupBy(_%2)
    //归约
    val reduce = list2.reduce(_+_)
    val fold = list2.fold(10)(_+_)
    val foldRight = list2.fold(11)(_-_) //10-(8-(16-(9-(5-11))))
  }

}
五、应用-单词计数
package chapter01

import scala.collection.mutable

object CollectionApp {

  def main(args: Array[String]): Unit = {

    val map1 = Map("a" -> 1, "b" -> 2, "c" -> 5)
    val map2 = mutable.Map("a" -> 2, "b" -> 4, "d" -> 6)

    //每一个map1的kv对合并到map2中去,map2作为初始值
    val map3 = map1.foldLeft(map2)(
      (mergedMap, kv) => {
        val key = kv._1
        val value = kv._2
        mergedMap(key) = mergedMap.getOrElse(key, 0) + value
        mergedMap
      }
    )

    //单词计数
    val strings = List("hello world", "hello java", "hello spark from scala")
    val flatMapList = strings.flatMap(_.split(" "))
    //Map(world -> List(world), java -> List(java), spark -> List(spark), scala -> List(scala), from -> List(from), hello, -> List(hello,), hello -> List(hello, hello))
    val groupMap = flatMapList.groupBy(word => word)
    val countMap = groupMap.map(kv =>(kv._1, kv._2.length))
    val sortList = countMap.toList.sortWith(_._2 > _._2)
    //List((hello,3), (world,1), (java,1))
    println(sortList.take(3))

    //单词计数-复杂版
    val strings1 = List(("hello world", 2), ("hello java", 3), ("hello spark from scala", 4))
    //List(hello world hello world , hello java hello java hello java , hello spark from scala hello spark from scala hello spark from scala hello spark from scala )
    val strings2 = strings1.map(
      kv => {
        (kv._1.trim + " ") * kv._2
      }
    )
    strings2.flatMap(_.split(" "))
        .groupBy(word => word)
        .map(kv => (kv._1, kv._2.size))
        .toList
        .sortBy(_._2)(Ordering[Int].reverse)
        .take(3)
    println(strings2)

    //单词计数思路二:直接基于预统计的结果进行转换,效率更高
    //将字符串打散为单词,并结合对应的个数包装成二元组
    //List((hello,2), (world,2), (hello,3), (java,3), (hello,4), (spark,4), (from,4), (scala,4))
    val tuples = strings1.flatMap(
      tuple => {
        val arrayString = tuple._1.split(" ")
        arrayString.map(word => (word, tuple._2))
      }
    )
    //2、对二元组按照单词进行分组
    //Map(world -> List((world,2)), java -> List((java,3)), spark -> List((spark,4)), scala -> List((scala,4)), from -> List((from,4)), hello -> List((hello,2), (hello,3), (hello,4)))
    val preCountTuple = tuples.groupBy(_._1)
    //3、叠加统计每个单词的总数
    //Map(world -> 2, java -> 3, spark -> 4, scala -> 4, from -> 4, hello -> 9)
    val countTuple = preCountTuple.mapValues(
      tupleList => tupleList.map(_._2).sum
    )
    //  4、转换成list排序
    val resultTuple = countTuple.toList.sortWith(_._2 > _._2).take(3)
    println(countTuple)

  }

}
六、队列与并行计算
package chapter01

import scala.collection.immutable.Queue
import scala.collection.mutable

object QueueTest {

  def main(args: Array[String]): Unit = {
    //可变队列
    val queue = new mutable.Queue[String]()
    queue.enqueue("a", "c", "b")
    val str = queue.dequeue()
    //不可边队列
    val queue1 = Queue("m", "n")
    val queue2 = queue1.enqueue("o")

    //并行计算
    val result = (1 to 10).map(
      x => Thread.currentThread().getId
    )
    val result2 = (1 to 10).par.map(
      x => Thread.currentThread().getId
    )
    
  }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值