【问题标题】:How to find all keys and convert to boolean如何查找所有键并转换为布尔值
【发布时间】:2017-01-12 18:21:38
【问题描述】:

我有一个包含整数值但采用字符串格式的地图

我正在尝试根据这样的键模式过滤它们:

m.filter(f => f.startsWith("user_id_"))

键/值对如下所示:

"user_id_a" -> "23453245"

所以我想按模式过滤键,然后将值转换为 Long 并将其作为 List 返回。

【问题讨论】:

  • 地图是字符串 -> 布尔值?
  • Map[String, String] 带有整数值,抱歉更正了。

标签: scala dictionary


【解决方案1】:
val m: Map[String,Int]
val filtered = m.filter(_._1.startsWith("user_id_"))
val modified = filtered.map(x => (x._1,x._2.toLong)).toList // If you  want the output to have both string and value
val modified2 = filtered.map(x => x._2.toLong).toList // If you want the output to have only the values

【讨论】:

  • m.filter(f => f._1.startsWith("user_id_")).map(t => t._2.toLong).toList
【解决方案2】:
yourMap
 .filter{case (key, value) => key.startsWith("user_id_")}
 .mapValues(_.toLong)
 .toList

我不明白你如何拥有一个带有布尔值的地图,但示例是字符串 -> 字符串。我以为是错别字

【讨论】:

    猜你喜欢
    • 2021-06-27
    • 2016-10-07
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    • 2011-12-26
    • 1970-01-01
    相关资源
    最近更新 更多