Scala集合的常用方法以及Map的可变和不可变

Scala集合的常用方法以及Map的可变和不可变


  

1 Scala集合的常用方法以及Map的可变和不可变

package com.chengzi.collection

import scala.collection.mutable

/**
  * Desc:
  *
  * 对scala集合做一个梳理
  *
  * @author:chengli
  * @date:2021 /3/3 9:24
  */
object CollectionTest {
  def main(args: Array[String]): Unit = {

    println(" Array ///")
    //不可变
    val array1 = new Array[Int](2)
    array1(1) = 4
    println(array1.mkString(","))

    var array2 = Array(1, 2)
    array2(1) = 4
    println(array2.mkString(","))


    //可变
    val array3 = new scala.collection.mutable.ArrayBuffer[Int](2)
    //array3(0) = 2  //没有初始化,不能更改元素

    val array4 = scala.collection.mutable.ArrayBuffer[Int](1, 2)
    array4(1) = 4
    array4.append(3)
    array4.remove(2) //移除下标是2的元素
    println(array4.mkString(","))

    //转换和遍历
    val buffer = array2.toBuffer
    val array = buffer.toArray
    for (index <- 0 until array.length) {
      println(array(index))
    }


    println(" List ")
    //不可变 :+ 和+:集合本身不变化返回新的集合
    val list1 = scala.collection.immutable.List("Hello", "World")
    val list2 = list1 :+ "nihao"
    println(list1.mkString(","))
    println(list2.mkString(","))

    val list3 = List(1, 2, 3)
    val list4 = 4 :: 5 :: 6 :: list3 ::: Nil
    val list5 = 4 :: 5 :: 6 :: list3 :: Nil
    println(list4.mkString(","))
    println(list5.mkString(","))


    //可变
    val list6 = scala.collection.mutable.ListBuffer("Hello", "World")
    list6 += "123"
    list6.append("456")
    list6 ++= List("789")
    val list7 = list6 ++ List("aaa")
    val list8 = list6 :+ "bbb"
    println(list6.mkString(","))
    println(list7.mkString(","))
    println(list8.mkString(","))
    list6.remove(0)
    println(list6.mkString(","))


    println(" Map ")
    //不可变:
    val map1: Map[String, Int] = scala.collection.immutable.Map("A" -> 1, "B" -> 2)
    map1 + ("C"->3)
    println(map1)
    println(if (map1.contains("")) {map1("A")} else {map1.get("B").get})
    //map1("A") = 3  可变map才能修改


    //不可变
    val map2 = new scala.collection.mutable.HashMap[String, Int]
    map2 += ("A" -> 1, "B" -> 2, ("C", 3))
    map2 -= ("D")
    println(map2)

    println(" Set ")
    //不可变
    var set1 = Set(1,2,3)
    set1 += 4  //定义为val则不可以
    println(set1)


    val set2 = mutable.Set(1,2,3)
    set2 += 4
    println(set2)


    println("//可变map和不可变map的应用///")
    val seq = "ABCAAAaAABBBBBCCCCC"
    val charToInt = seq.foldLeft(Map[Char,Int]())(useMap)
    println(charToInt)

    val charToInt1 = seq.foldLeft(mutable.Map[Char,Int]())((map:mutable.Map[Char,Int],char:Char) =>map += (char->(map.getOrElse(char,0) + 1)))
    println(charToInt1)
  }

  def useMap(map:Map[Char,Int],char:Char):Map[Char,Int]={
      map + (char->(map.getOrElse(char,0) + 1))
  }
}

Array ///
0,4
1,4
1,4
1
4
List
Hello,World
Hello,World,nihao
4,5,6,1,2,3
4,5,6,List(1, 2, 3)
Hello,World,123,456,789
Hello,World,123,456,789,aaa
Hello,World,123,456,789,bbb
World,123,456,789
Map
Map(A -> 1, B -> 2)
2
Map(A -> 1, C -> 3, B -> 2)
Set
Set(1, 2, 3, 4)
Set(1, 2, 3, 4)
//可变map和不可变map的应用///
Map(A -> 6, B -> 6, C -> 6, a -> 1)
Map(A -> 6, C -> 6, a -> 1, B -> 6)

Process finished with exit code 0

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值