scala – 如何在`Option [Map [String,String]]`上运行`.get(“k
发布时间:2020-12-16 18:57:24 所属栏目:安全 来源:网络整理
导读:当我像这样创建一个Option [Map [String,String]]时 scala val x = Some(Map("foo" - "bar"))x: Some[scala.collection.immutable.Map[String,String]] = Some(Map(foo - bar)) 为什么这个电话有效: scala x.get("foo")res0: String = bar 由于x是实例Optio
当我像这样创建一个Option [Map [String,String]]时
scala> val x = Some(Map("foo" -> "bar")) x: Some[scala.collection.immutable.Map[String,String]] = Some(Map(foo -> bar)) 为什么这个电话有效: scala> x.get("foo") res0: String = bar 由于x是实例Option,并且没有方法get接受case类上的参数Some和该类是final,这不应该起作用. IDE没有提供任何提示,为什么这样做. 解决方法
Option有一个没有参数列表的get方法.你只需使用没有参数列表的名称get就可以调用它:
scala> val x = Some(Map("foo" -> "bar")) x: Some[scala.collection.immutable.Map[String,String]] = Some(Map(foo -> bar)) scala> x.get // Note: no arguments res0: scala.collection.immutable.Map[String,String] = Map(foo -> bar) 你得到的东西显然是地图. get之后的(“foo”)应用于Map.请注意,这是在Map上调用apply方法的快捷语法.因此,x.get(“foo”)等同于x.get.apply(“foo”). scala> x.get("foo") // Shortcut syntax res2: String = bar scala> x.get.apply("foo") // For this res3: String = bar (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 玩转processlist,高效追溯MySQL活跃连接数飙升根因
- scala – 类的实例与其伴随对象之间的关系
- twitter-bootstrap – 在页面重新加载后,使用twitter boots
- angularjs – 如何在ui-router状态转换之间调出“进行中”加
- scala – Spark:当加入2个大型DF时,大小超过Integer.MAX_V
- yum安装报错:NOKEY
- Windows批处理删除所有目录下的图片类型文件
- WPF调用WebService实现天气预报
- Couldn't register ***.**** with the bootstrap serve
- twitter-bootstrap – 自定义Bootstrap中的列数3