当前位置:网站首页>Scala immutable map, variable map, map conversion to other data types

Scala immutable map, variable map, map conversion to other data types

2022-07-27 04:33:00 But don't ask about the future

Preface

  scala in Map It's an iterative Key value pair (key/value) structure . All values can be obtained through the key .Map The keys in are unique


1. immutable Map

object T14 {
    
  def main(args: Array[String]): Unit = {
    
    //scala in map key Can't repeat   disorder 
    //scala Middle key value pair , Can be used directly K->V The way 
    
    //key Phase at the same time , It will cover 
    val map = Map("a" -> 1, "b" -> 2, "c" -> 3, "a" -> 5)
    println(map)

    //  Add data 
    val map1 = map + ("d" -> 7)
    println(map1)

    //  Delete data 
    val map2 = map - "a"
    println(map2)

    //  Modifying data 
    val map3 = map.updated("c", 9)
    println(map3)

    // Merge map
    val map4 = map ++ Map(1 -> "f", 3 -> "h")
    println(map4)

    //  Traversal data 
    map.foreach(println)
  }
}

 Insert picture description here


2. variable Map

   Need to introduce import scala.collection.mutable

import scala.collection.mutable

object T15 {
    
  def main(args: Array[String]): Unit = {
    
    val map = mutable.Map("a" -> 1, "b" -> 2, "c" -> 3)
    println(map)

    //key non-existent , The insert ; There is , Update 
    map.put("a", 3)
    println(map)

    // variable Map + Add data , Generate new map
    val map1 = map + ("e" -> 5)
    println(map1 eq map)

    // variable Map += Add data , Return to the original map Oneself 
    val map2 = map += ("e" -> 5)
    println(map2 eq map)

    // Modify the data 2 Ways of planting 
    map.update("c", 200)
    println(map)
    
    map("c") = 300
    println(map)

    // Delete data 
    map.remove("c")
    // map - ("c")
    // map -= ("c")
    println(map)
    
    
    // Empty data 
    map.clear()
    println(map)
  }
}

 Insert picture description here

import scala.collection.mutable

object T16 {
    
  def main(args: Array[String]): Unit = {
    
    // java  if key There is , Then get value, If it does not exist , Then return to null,hashmap Can store null keys and null values 
    //scala  Can store null values , Empty keys cannot be stored 

    val map = mutable.Map("a" -> 1, "b" -> 2, "c" -> 3, "d" -> null)

    //map according to key Query data , The query results will be packaged 
    // The type of the returned result is Option[V] type , Yes 2 Objects ,Some( Valuable )、None( No value ), To avoid null pointer exceptions 

    // about key Occasion of existence , So the return result is Some
    val value1 = map.get("a")
    println(value1)

    val value2 = map.get("d")
    println(value2)

    // about key Occasions that do not exist , So the return result is None
    val value3 = map.get("e")
    println(value3)

    // obtain key Corresponding specific value 
    println(map.get("a").get)
    println(map.get("d").get)

    // from None In the object get value , Exceptions will occur 
    // println( map.get("e").get)
    //  First judge whether it is empty or set the default value when it is empty 
    println(map.get("e").isEmpty)
    println(map.get("e").getOrElse(-1))
    // More brief way 
    map.getOrElse("e",-1)
  }
}

 Insert picture description here

import scala.collection.mutable

object T17 {
    
  def main(args: Array[String]): Unit = {
    
    val map = mutable.Map("a" -> 1, "b" -> 2, "c" -> 3, "d" -> null)

    //mao  key   iterator 
    // val keys = map.keys
    // val iterator1 = keys.iterator

    val iterator1 = map.keysIterator
    while (iterator1.hasNext) {
    
      println(map.get(iterator1.next()).get)
    }
    //mao  value   iterator 
    // val values = map.values
    // val iterator2 = values.iterator
    val iterator2 = map.valuesIterator
    while (iterator2.hasNext) {
    
      println(iterator2.next())
    }
  }
}

3. Map Convert to other data types

object T18 {
    
  def main(args: Array[String]): Unit = {
    
    val map1 = mutable.Map("a" -> 1, "b" -> 2, "c" -> 3)

    val set: Set[(String, Int)] = map1.toSet
    val list: List[(String, Int)] = map1.toList
    val array: Array[(String, Int)] = map1.toArray

    println(set.mkString(","))
    println(list.mkString(","))
    println(array.mkString(","))
  }
}

 Insert picture description here

原网站

版权声明
本文为[But don't ask about the future]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/208/202207270256067869.html