简体   繁体   English

如何使用地图返回 Double

[英]How to return Double with map

I try to making token lexical analyzer and I want return Double type using map in scala我尝试制作令牌词法分析器,我想在 scala 中使用 map 返回 Double 类型

    var m = Map[String,Double]()
    def parseItem(tok: Tokenizer): Double = {
    val startPos = tok.tokenPos
    val t = tok.token
    tok.next()
    //(other codes)
    else if (t.isIdentifier) {
      var t1 = tok.token
      if(t1.text == "=") {
        tok.next()
        var t2 = tok.token
        if(t2.isNumber) {
          m += (t.text -> t2.number)
          println("Idenrifier sorted :" + t.text)
          0
        }else if(t2.isIdentifier && m.get(t2.text) == None){
            println("Error!!! RHS is Wrong identifier!!!")
            throw new SyntaxError(startPos, "expected number, identifier, or '('")
        }else{
          m += (t.text -> m.get(t2.text))
          println("Idenrifier sorted :" + t.text)
          0
        }
      }else{
          m.get(t.text)
      }

the error code is :Option[Double] I think return type is Double But I can't understanding this error错误代码是:Option[Double] 我认为返回类型是 Double 但我无法理解这个错误

  m.get(t.text) is of type Option[Double] 

You can use the apply method of the Map but keep in mind that the apply method returns the value associated with a given key directly, without wrapping it in an Option.您可以使用 Map 的 apply 方法,但请记住,apply 方法直接返回与给定键关联的值,而无需将其包装在 Option 中。 If the key is not defined in the map, an exception is raised.如果映射中未定义键,则会引发异常。

You can use m(t.text) or m.apply(t.text)您可以使用m(t.text)m.apply(t.text)

Or you can use m getOrElse (t.text, defaultValue) ,which returns the value associated with key t.text in the m map , or the defaultValue if not found.或者您可以使用m getOrElse (t.text, defaultValue) ,它返回与m映射中的键t.text关联的值,如果未找到则返回 defaultValue。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Scala中返回Map - How to return Map in scala 参数为double时如何返回None? - How to return None when parameter is double? 如何对 scala 中的 map 进行排序,其中键为 List[Double],值为 double。 我想用双重排序? - How to sort a map in scala where the key is List[Double] and the value is double. And I wanna sort with double? Scala 如何在以下列表中与 Map[String, Double] 类型的地图相交:List[Map[String, Double]] - Scala How to Intersect Maps of types Map[String, Double] inside a List of: List[Map[String, Double]] 尝试从RDD返回Map [(String,String),(Double,Double)]时无法将java.lang.String强制转换为java.lang.Double错误 - java.lang.String cannot be cast to java.lang.Double Error when trying to return Map[(String, String),(Double, Double)] from RDD 返回地图 - Return in a map 如何将 map 字符串数组数组(Array[Array[String]])转换为 double 数组数组(Array[Array[Double]])? - how to map array of array of string(Array[Array[String]]) into array of array of double(Array[Array[Double]])? 如何选择从map函数返回值 - How to optionally return value from map function 如何在scala中返回值类型为String的Map - How to return a Map with Value Type as String in scala 如何将元素添加到不可变数组并返回 map - how to add element to immutable array and return that map
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM