zoukankan      html  css  js  c++  java
  • Map Class Example

    Here's a quick look at how to use the Scala Map class, with a colllection of Map class examples.

    The immutable Map class is in scope by default, so you can create an immutable map without an import, like this:

    val states = Map("AL" -> "Alabama", "AK" -> "Alaska")
    

    To create a mutable Map, import it first:

    var states = scala.collection.mutable.Map("AL" -> "Alabama")
    

    Adding, removing, and updating mutable Map elements

    The following examples show how to add, remove, and update elements in a mutable Scala Map:

    // create an empty map
    var states = scala.collection.mutable.Map[String, String]()
    
    // create a map with initial elements
    var states = scala.collection.mutable.Map("AL" -> "Alabama", "AK" -> "Alaska")
    
    // add elements with +=
    states += ("AZ" -> "Arizona")
    states += ("CO" -> "Colorado", "KY" -> "Kentucky")
    
    // remove elements with -=
    states -= "KY"
    states -= ("AZ", "CO")
    
    // update elements by reassigning them
    states("AK") = "Alaska, The Big State"
    

    Iterating over Scala maps

    Once you have a Map, you can iterate over it using several different techniques. I prefer using the for loop (or for comprehension):

    scala> val m1 = Map("fname" -> "Al", "lname" -> "Alexander")
    
    scala> for ((k,v) <- m1) printf("key: %s, value: %s
    ", k, v)
    key: fname, value: Al
    key: lname, value: Alexander

    This page has some other Map and for loop examples, which I've reproduced here:

    // version 1 (tuples)
    m1 foreach (x => println (x._1 + "-->" + x._2))
    
    // version 2 (foreach and case)
    m1 foreach {case (key, value) => println (key + "-->" + value)}
    

    You can choose whatever format you prefer.

     

    A few more ways to iterate over a Scala Map

    To demonstrate a more "real world" example of looping over a Scala Map, while I was working through some programming examples in the book,Programming Collective Intelligence, I decided to code them up in Scala.

    To begin with, I defined my Scala Map like this:

    val p1Ratings = Map("Lady in the Water"-> 3.0, 
                        "Snakes on a Plane"-> 4.0,
                        "You, Me and Dupree"-> 3.5)
    

    In my case, when I'm iterating over the Map I'm really just interested in the Map keys, so the cleanest way to loop over every Map element is like this:

    p1Ratings.keys.foreach( (movie) => 
      if (p2Ratings.contains(movie)) similarItems += (movie -> true)
    )
    

    While I chose that looping method in my code, I could also use the "tuples" approach, where movie is a Tuple, and I only use the first element of the Tuple, which happens to be my keys:

    p1Ratings foreach ( (movie) => 
      if (p2Ratings.contains(movie._1)) similarItems += (movie._1 -> true)
    )
    

    In that approach, I ignore the second element of each Tuple, because I don't need it. (Which is why I don't like this approach for this instance.)

    In a similar approach, I loop over the Map as shown next, creating a field named rating1 which I again don't use because I don't need it:

    for ((movie1, rating1) <- p1Ratings) {
      if (p2Ratings.contains(movie1)) similarItems += (movie1 -> true)
    }
    

    These last two approaches will work better, and look a little more logical, if you need to access the key and value for each map element, but in my case, since I don't need to values, I'm using the first approach shown above.

  • 相关阅读:
    论文笔记4
    论文笔记3
    论文笔记2
    论文笔记1
    论文笔记
    AFG与AWG的比较
    Linux下“有线线缆被拔出”问题的解决
    python生成excel格式座位表
    PythonTip--一马当先--bfs
    python pygame--倒计时
  • 原文地址:https://www.cnblogs.com/seaspring/p/5645277.html
Copyright © 2011-2022 走看看