Scala学习笔记04_Map与Tuple

作者: padluo | 来源:发表于2018-03-26 18:06 被阅读24次

    创建Map

    // 创建一个不可变的Map
    scala> val ages = Map("Leo" -> 30, "Jen" -> 25, "Jack" -> 23)
    
    scala> val ages = Map("Leo" -> 30, "Jen" -> 25, "Jack" -> 23)
    ages: scala.collection.immutable.Map[String,Int] = Map(Leo -> 30, Jen -> 25, Jack -> 23)
    
    scala> ages("Leo") = 31
    <console>:15: error: value update is not a member of scala.collection.immutable.Map[String,Int]
           ages("Leo") = 31
           ^
    // 创建一个可变的Map
    scala> val ages = scala.collection.mutable.Map("Leo" -> 30, "Jen" -> 25, "Jack" -> 23)
    ages: scala.collection.mutable.Map[String,Int] = Map(Jen -> 25, Jack -> 23, Leo -> 30)
    
    scala> ages("Leo") = 31
    
    scala> ages("Leo")
    res43: Int = 31
    
    scala> ages
    res44: scala.collection.mutable.Map[String,Int] = Map(Jen -> 25, Jack -> 23, Leo -> 31)
    // 使用另外一种方式定义Map元素
    scala> val ages = Map(("Leo",30), ("Jen",25), ("Jack",23))
    ages: scala.collection.immutable.Map[String,Int] = Map(Leo -> 30, Jen -> 25, Jack -> 23)
    // 创建一个空的HashMap
    scala> val ages = new scala.collection.mutable.HashMap[String,Int]
    ages: scala.collection.mutable.HashMap[String,Int] = Map()
    
    scala> val ages = new scala.collection.mutable.HashMap[String,Int]()
    ages: scala.collection.mutable.HashMap[String,Int] = Map()
    
    scala> val ages = new scala.collection.mutable.Map[String,Int]()
    <console>:13: error: trait Map is abstract; cannot be instantiated
           val ages = new scala.collection.mutable.Map[String,Int]()
                      ^
    

    访问Map的元素

    // 获取指定key对应的value,如果key不存在,会报错
    scala> val ages = scala.collection.mutable.Map("Leo" -> 30, "Jen" -> 25, "Jack" -> 23)
    ages: scala.collection.mutable.Map[String,Int] = Map(Jen -> 25, Jack -> 23, Leo -> 30)
    
    scala> val leoAge = ages("Leo")
    leoAge: Int = 30
    
    scala> val leoAge = ages("leo")
    java.util.NoSuchElementException: key not found: leo
      at scala.collection.MapLike$class.default(MapLike.scala:228)
      at scala.collection.AbstractMap.default(Map.scala:59)
      at scala.collection.mutable.HashMap.apply(HashMap.scala:65)
      ... 32 elided
    // 使用contains函数检查key是否存在
    scala> val leoAge = if(ages.contains("leo")) ages("leo") else 0
    leoAge: Int = 0
    // getOrElse函数
    scala> val leoAge = ages.getOrElse("leo",0)
    leoAge: Int = 0
    

    修改Map的元素

    // 更新Map的元素
    scala> ages
    res45: scala.collection.mutable.Map[String,Int] = Map(Jen -> 25, Jack -> 23, Leo -> 30)
    
    scala> ages("Leo") = 31
    
    scala> ages
    res47: scala.collection.mutable.Map[String,Int] = Map(Jen -> 25, Jack -> 23, Leo -> 31)
    // 增加多个元素
    scala> ages += ("Mike" -> 35, "Tom" -> 50)
    res48: ages.type = Map(Jen -> 25, Mike -> 35, Tom -> 50, Jack -> 23, Leo -> 31)
    // 移除元素
    scala> ages -= "Mike"
    res49: ages.type = Map(Jen -> 25, Tom -> 50, Jack -> 23, Leo -> 31)
    // 创建不可变的Map
    scala> val ages = Map("Leo" -> 30, "Jack" -> 40, "Jen" -> 25)
    ages: scala.collection.immutable.Map[String,Int] = Map(Leo -> 30, Jack -> 40, Jen -> 25)
    // 更新不可变的Map
    scala> val ages2 = ages + ("Mike" -> 36, "Tom" -> 60)
    ages2: scala.collection.immutable.Map[String,Int] = Map(Mike -> 36, Tom -> 60, Leo -> 30, Jack -> 40, Jen -> 25)
    // 原来Map已有的元素也可更新
    scala> val ages2 = ages + ("Leo" -> 31)
    ages2: scala.collection.immutable.Map[String,Int] = Map(Leo -> 31, Jack -> 40, Jen -> 25)
    // 移除不可变Map的元素
    scala> val ages3 = ages - "Jack"
    ages3: scala.collection.immutable.Map[String,Int] = Map(Leo -> 30, Jen -> 25)
    

    遍历Map

    // 遍历Map的entrySet
    scala> ages
    res50: scala.collection.immutable.Map[String,Int] = Map(Leo -> 30, Jack -> 40, Jen -> 25)
    
    scala> for((key,value) <- ages) println(key + ": " + value)
    Leo: 30
    Jack: 40
    Jen: 25
    // 遍历Map的key
    scala> for(key <- ages.keySet) println(key)
    Leo
    Jack
    Jen
    // 遍历Map的Value
    scala> for(value <- ages.values) println(value)
    30
    40
    25
    // 生成新Map,反转key和value
    scala> for((key, value) <- ages) yield (value, key)
    res54: scala.collection.immutable.Map[Int,String] = Map(30 -> Leo, 40 -> Jack, 25 -> Jen)
    

    SortedMap和LinkedHashMap

    // SortedMap可以自动对Map的key的排序
    scala> val ages = scala.collection.immutable.SortedMap("Leo" -> 30, "Jack" -> 20, "Jen" ->25)
    ages: scala.collection.immutable.SortedMap[String,Int] = Map(Jack -> 20, Jen -> 25, Leo -> 30)
    // LinkedHashMap可以记住插入entry的顺序
    scala> val ages = new scala.collection.mutable.LinkedHashMap[String, Int]
    ages: scala.collection.mutable.LinkedHashMap[String,Int] = Map()
    
    scala> ages("leo") = 30
    
    scala> ages("jack") = 20
    
    scala> ages("jen") = 25
    
    scala> ages
    res3: scala.collection.mutable.LinkedHashMap[String,Int] = Map(leo -> 30, jack -> 20, jen -> 25)
    

    Map的元素类型Tuple

    // 简单Tuple
    scala> val t = ("leo", 30)
    t: (String, Int) = (leo,30)
    // 访问Tuple
    scala> t._1
    res4: String = leo
    
    scala> t._2
    res5: Int = 30
    // zip操作
    scala> val names = Array("leo", "jack", "jen")
    names: Array[String] = Array(leo, jack, jen)
    
    scala> val ages = Array(30, 20, 25)
    ages: Array[Int] = Array(30, 20, 25)
    
    scala> val nameAges = names.zip(ages)
    nameAges: Array[(String, Int)] = Array((leo,30), (jack,20), (jen,25))
    
    scala> for((name, age) <- nameAges) println(name + ": " + age)
    leo: 30
    jack: 20
    jen: 25
    

    本文首发于steem,感谢阅读,转载请注明。

    https://steemit.com/@padluo


    微信公众号「padluo」,分享数据科学家的自我修养,既然遇见,不如一起成长。

    数据分析

    读者交流电报群

    https://t.me/sspadluo


    知识星球交流群

    知识星球读者交流群

    相关文章

      网友评论

        本文标题:Scala学习笔记04_Map与Tuple

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