• 4、map和Tuple


    一、map

    1、创建map

    //创建一个不可变的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)
    
    
    
    //创建一个可变的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
    res5: 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()


    2、访问Map的元素

    ##获取指定key对应的value,如果key不存在,会报错
    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> val age = ages("Leo")
    age: Int = 30
    
    
    
    ##使用contains判断key是否存在
    scala> val age = if (ages.contains("Leo")) ages("Leo") else 0
    age: Int = 30
    
    
    
    ##getOrElse函数判断key是否存在
    scala> val age = ages.getOrElse("Leo",0)
    age: Int = 30


    3、修改Map的元素

    ##更新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") 
    res3: Int = 31
    
    
    
    ##增加多个元素
    scala> ages += ("Mike" -> 35, "Tom" -> 40)
    res4: ages.type = Map(Jen -> 25, Mike -> 35, Tom -> 40, Jack -> 23, Leo -> 31)
    
    scala> ages
    res5: scala.collection.mutable.Map[String,Int] = Map(Jen -> 25, Mike -> 35, Tom -> 40, Jack -> 23, Leo -> 31)
    
    
    
    ##移除元素
    scala> ages -= "Mike"
    res6: ages.type = Map(Jen -> 25, Tom -> 40, Jack -> 23, Leo -> 31)
    
    scala> ages
    res7: scala.collection.mutable.Map[String,Int] = Map(Jen -> 25, Tom -> 40, Jack -> 23, Leo -> 31)
    
    
    
    ##更新不可变的map
    scala> val ages2 = ages + ("Mike" -> 36, "Tom" -> 40)
    ages2: scala.collection.immutable.Map[String,Int] = Map(Mike -> 36, Tom -> 40, Leo -> 30, Jack -> 23, Jen -> 25)
    
    scala> ages2
    res0: scala.collection.immutable.Map[String,Int] = Map(Mike -> 36, Tom -> 40, Leo -> 30, Jack -> 23, Jen -> 25)
    
    
    
    ##移除不可变map的元素
    scala> val ages3 = ages - "Tom"
    ages3: scala.collection.immutable.Map[String,Int] = Map(Leo -> 30, Jen -> 25, Jack -> 23)
    
    scala> ages3
    res1: scala.collection.immutable.Map[String,Int] = Map(Leo -> 30, Jen -> 25, Jack -> 23)


    4、遍历map

    ##遍历map的entrySet
    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> for ((key, value) <- ages) println(key + ":" + value)
    Leo:30
    Jen:25
    Jack:23
    
    
    
    ##遍历map的key
    scala> for (key <- ages.keySet) println(key)
    Leo
    Jen
    Jack
    
    
    
    ##遍历map的value
    scala> for (value <- ages.values) println(value) 
    30
    25
    23
    
    
    
    ##生成新map,反转key和value
    scala> for ((key, value) <- ages) yield (value, key)
    res8: scala.collection.immutable.Map[Int,String] = Map(30 -> Leo, 25 -> Jen, 23 -> Jack)


    5、SortedMap和LinkedHashMap

    ##SortedMap可以自动对Map的key的排序
    scala> val ages = scala.collection.immutable.SortedMap("Leo" -> 30,"Alice" -> 15, "Jen" -> 25)
    ages: scala.collection.immutable.SortedMap[String,Int] = Map(Alice -> 15, 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("Alice") = 25
    
    scala> ages("Jen") = 26
    
    scala> ages
    res12: scala.collection.mutable.LinkedHashMap[String,Int] = Map(Leo -> 30, Alice -> 25, Jen -> 26)


    二、Tuple

    map的元素类型Tuple

    1、定义、访问Tuple

    ##定义
    scala> val t = ("leo", 30, "Jen")
    t: (String, Int, String) = (leo,30,Jen)
    
    
    ##访问
    scala> t._1
    res15: String = leo
    
    scala> t._2
    res16: Int = 30
    
    scala> t._3
    res17: String = Jen
    
    
    
    ##zip操作
    scala> val names = Array("leo","jack","mike")
    names: Array[String] = Array(leo, jack, mike)
    
    scala> val ages = Array(30,25,27)
    ages: Array[Int] = Array(30, 25, 27)
    
    scala> val nameages = names.zip(ages)
    nameages: Array[(String, Int)] = Array((leo,30), (jack,25), (mike,27))
    
    scala> for ((name, age) <- nameages) println(name + ": " + age)
    leo: 30
    jack: 25
    mike: 27
  • 相关阅读:
    07-JSP
    06-ServletContext和ServletConfig
    05-请求转发和重定向Login
    04-session学习
    03-cookie学习
    02-servlet生命周期和doget()_dopost()方法
    01-request和respone学习
    05-jQuery案例
    04-jQuery的事件机制和动画效果
    03-jQuery操作元素
  • 原文地址:https://www.cnblogs.com/weiyiming007/p/10997547.html
Copyright © 2020-2023  润新知