美文网首页
Scala-元组、集合

Scala-元组、集合

作者: printf200 | 来源:发表于2019-04-13 13:12 被阅读0次

    1、元祖
    映射是K/V对偶的集合,对偶是元组的最简单形式,元组可以装着多个不同类型的值。
    1)、创建元祖
    //创建元祖时,用小括号那个多个元素括起来,元素之间用逗号隔开,元素的类型可以不一样,元素个数也可以任意多个

    scala> val tp = (1,3.3,"scala")
    tp: (Int, Double, String) = (1,3.3,scala)
    
    2)、获取元祖的值
    scala> tp._1
    res37: Int = 1
    
    scala> tp._2
    res38: Double = 3.3
    
    scala> tp._3
    res39: String = scala
    
    //使用下面这种方式更直观
    scala> val t , (a, b, c) = (2, 2.2, "scala")
    t: (Int, Double, String) = (2,2.2,scala)
    a: Int = 2
    b: Double = 2.2
    c: String = scala
    
    scala> x
    <console>:9: error: not found: value x
                  x
                  ^
    
    scala> a
    res43: Int = 2
    
    scala> b
    res44: Double = 2.2
    
    scala> c
    res45: String = scala
    
    //对偶元祖加到map中
    scala> val m = Map(1 -> "a", 2 -> "b")
    m: scala.collection.mutable.Map[Int,String] = Map(2 -> b, 1 -> a)
    
    scala> val t = (3,"c")
    t: (Int, String) = (3,c)
    
    scala> m += t
    res40: m.type = Map(2 -> b, 1 -> a, 3 -> c)
    
    scala> m
    res41: scala.collection.mutable.Map[Int,String] = Map(2 -> b, 1 -> a, 3 -> c)
    

    3)、将对偶集合转换成映射
    scala> val arr = Array((1,"a"),(2,"b"))
    arr: Array[(Int, String)] = Array((1,a), (2,b))

    scala> val t = arr.toMap
    t: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b)

    scala> t
    res46: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b)

    4)、拉链操作
    使用zip命令可以将集合中对应的多个值绑定在一起

    scala> val arr1 = Array(1,2,3)
    arr1: Array[Int] = Array(1, 2, 3)
    
    scala> val arr2 = Array("a","b","c")
    arr2: Array[String] = Array(a, b, c)
    
    scala> var arr3 = arr2.zip(arr1)
    arr3: Array[(String, Int)] = Array((a,1), (b,2), (c,3))
    
    scala> arr3
    res47: Array[(String, Int)] = Array((a,1), (b,2), (c,3))
    

    //如果其中一个元素要多,那么省去最后多余的
    //即:如果两个数组的元素个数不一致,拉链操作后生成的数组的长度为较小的那个数组的元素个数

    scala> val arr4 = Array("a","b","c","d")
    arr4: Array[String] = Array(a, b, c, d)
    
    scala> var arr5 = arr4.zip(arr1)
    arr5: Array[(String, Int)] = Array((a,1), (b,2), (c,3))
    //arr4中多余的"d"就给省去了
    scala> arr5
    res49: Array[(String, Int)] = Array((a,1), (b,2), (c,3))
    

    2、集合
    Scala的集合有三大类:序列Seq、集Set、映射Map,所有的集合都扩展自Iterable特质(trait)
    在Scala中集合有可变(mutable)和不可变(immutable)两种类型,immutable类型的集合初始化后就不能改变了(注意与val修饰的变量进行区别)
    1)、序列
    可认为java的List
    //创建不可变集合

    scala> val list = List(1,2,3,4)
    list: List[Int] = List(1, 2, 3, 4)
    
    //获取序列中角标为0的元素
    scala> list(0)
    res50: Int = 1
    
    //由于集合是不可变的所以企图修改其中的值是行不通的
    scala> list(0) = 6
    <console>:10: error: value update is not a member of List[Int]
                  list(0) = 6
    
    //向集合中添加元素生成新的集合
    //将0插入到lst1的前面生成一个新的List
    scala> val list2 = 0 :: list
    list2: List[Int] = List(0, 1, 2, 3, 4)
    
    scala> list2
    res52: List[Int] = List(0, 1, 2, 3, 4)
    
    scala> val list3 = list.::(0)
    list3: List[Int] = List(0, 1, 2, 3, 4)
    
    scala> val list4 = 0 +: list
    list4: List[Int] = List(0, 1, 2, 3, 4)
    
    scala> val list5 = list.+:(0)
    list5: List[Int] = List(0, 1, 2, 3, 4)
    
    //将5添加到list最后面
    scala> val list6 = list :+ 5
    list6: List[Int] = List(1, 2, 3, 4, 5)
    
    //将两个list合并生成一个新的list
    scala> list
    res57: List[Int] = List(1, 2, 3, 4)
    
    scala> val lst = List(1,5,6,7)
    lst: List[Int] = List(1, 5, 6, 7)
    
    scala> val ls = list ++ lst
    ls: List[Int] = List(1, 2, 3, 4, 1, 5, 6, 7)
    
    //将lst插入到list前生成一个新的list
    scala> val ls2 = lst ++: list
    ls2: List[Int] = List(1, 5, 6, 7, 1, 2, 3, 4)
    
    下面是可变序列
    //导入可变集合的所有包
    scala> import scala.collection.mutable._
    import scala.collection.mutable._
    
    //创建可变序列(list)
    scala> val lst = ListBuffer[Int]()
    lst: scala.collection.mutable.ListBuffer[Int] = ListBuffer()
    
    //直接赋值初始化
    scala> val lst2 = ListBuffer(4,5,6,7)
    lst2: scala.collection.mutable.ListBuffer[Int] = ListBuffer(4, 5, 6, 7)
    //添加元素,以下有两种方式添加元素。注意:没有生成新的集合
    scala> lst += 1
    res58: lst.type = ListBuffer(1)
    
    scala> lst += 2
    res59: lst.type = ListBuffer(1, 2)
    
    scala> lst.app
    append        appendAll     apply         applyOrElse
    
    scala> lst.append
    append      appendAll
    
    scala> lst.append(3,4)
    
    scala> lst
    res61: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4)
    
    // 将lst2中元素追加到lst中,注意:没有生成新的集合
    scala> lst ++= lst2
    res62: lst.type = ListBuffer(1, 2, 3, 4, 4, 5, 6, 7)
    
    scala> lst
    res63: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4, 4, 5, 6, 7)
    
    //两个集合合并,生成了新的集合
    scala> val lst3 = lst ++ lst2
    lst3: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4, 4, 5, 6, 7, 4, 5, 6, 7)
    

    2)、Set
    不可变Set

    scala> import scala.collection.immutable._
    import scala.collection.immutable._
    
    scala> val s1 = new HashSet[Int]()
    s1: scala.collection.immutable.HashSet[Int] = Set()
    
    //将元素1和s1合并生成一个新的set,原有set不变
    scala> s1 + 1
    res64: scala.collection.immutable.HashSet[Int] = Set(1)
    
    //发现原来的set中还是空
    scala> s1
    res65: scala.collection.immutable.HashSet[Int] = Set()
    
    scala> res64
    res66: scala.collection.immutable.HashSet[Int] = Set(1)
    
    //创建set的时候直接初始化
    //重复的元素会去掉
    scala> val set1 = Set(1,2,3,3)
    set1: scala.collection.immutable.Set[Int] = Set(1, 2, 3)
    
    //两个set相加,生成新的set,重复的元素会自动去掉,且元素排序不是按加入的时候顺序,这样这是set的特性
    scala> val set2 = set1 ++ Set(3,4,5)
    set2: scala.collection.immutable.Set[Int] = Set(5, 1, 2, 3, 4)
    
    可变set
    scala> import scala.collection.mutable._
    import scala.collection.mutable._
    
    scala> val set1 = Set(1,2,3,3)
    set1: scala.collection.mutable.Set[Int] = Set(1, 2, 3)
    
    //两种方式添加元素,不会生成新的set
    scala> set1.add(4)
    res69: Boolean = true
    
    scala> set1 += 5
    res70: set1.type = Set(1, 5, 2, 3, 4)
    
    scala> set1
    res71: scala.collection.mutable.Set[Int] = Set(1, 5, 2, 3, 4)
    
    //删除元素的两种方式
    scala> set1 -= 5
    res72: set1.type = Set(1, 2, 3, 4)
    
    scala> set1.remove(4)
    res73: Boolean = true
    
    scala> set1
    res74: scala.collection.mutable.Set[Int] = Set(1, 2, 3)
    

    3)、map
    这里只演示可变map了,都是跟上面一样,操作跟java集合操作类似(需要导入可变集合的包,上面已经导入过)

    //创建可变map
    scala> val map = new HashMap[String,Int]
    map: scala.collection.mutable.HashMap[String,Int] = Map()
    
    //两种方式添加元素,不会生成新的集合
    scala> map("zs") = 88
    
    scala> map("ls") = 87
    
    scala> map
    res81: scala.collection.mutable.HashMap[String,Int] = Map(zs -> 88, ls -> 87)
    
    scala> map.put("ww",99)
    res82: Option[Int] = None
    
    scala> map
    res83: scala.collection.mutable.HashMap[String,Int] = Map(ww -> 99, zs -> 88, ls -> 87)
    

    //两种方式移除元素,不会生成新的集合

    scala> map -= "ww"
    res84: map.type = Map(zs -> 88, ls -> 87)
    
    scala> map.remove("zs")
    res85: Option[Int] = Some(88)
    
    scala> map
    res86: scala.collection.mutable.HashMap[String,Int] = Map(ls -> 87)
    
    

    相关文章

      网友评论

          本文标题:Scala-元组、集合

          本文链接:https://www.haomeiwen.com/subject/xaxxuqtx.html