如果map包含子字符串Scala [closed],则从map中过滤键

jdg4fx2g  于 2023-10-18  发布在  Scala
关注(0)|答案(1)|浏览(162)

**已关闭。**此问题需要debugging details。它目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答这个问题。
2个月前关闭。
Improve this question
我有一个map类型Map[String,String],当我需要过滤几个键时,它没有关键字Some。当key不包含Some时,我需要从Map中删除这些key。
举例来说:

scala>     for ((k,v) <- map) println(s"key: $k, value: $v")

key:List(DoubleMetric(Column,Completeness,Some((first IS NOT NULL),value:0.6666666666666不符合限制要求
key:List(DoubleMetric(Column,Uniform,Some(CASE WHEN(count()OVER(PARTITION BY first unspecifiedframe$())= 1)THEN true ELSE false END),value:None键:List(DoubleMetric(Column,Minimum,Some(CAST(year AS DOUBLE),值:没有一
key:List(DoubleMetric(Column,Compliance,,Some(CAST((COALESCE(CAST(year AS DECIMAL(20,10),0.0)>= 0)AS INT),value:None键:List(DoubleMetric(Column,Maximum,Some(CAST(year AS DOUBLE),值:1991.0不符合约束要求!
key:List(DoubleMetric(Column,Compliance,Some(CAST((id IS NULL)OR(id IN(1,2,3,4,5)AS INT),value:0.6666666666666不符合约束要求!
key:List(DoubleMetric(Dataset,Size,*,Success(3.0),None)),value:3不符合约束条件!
预期结果:
key:List(DoubleMetric(Column,Completeness,Some((first IS NOT NULL),value:0.6666666666666不符合限制要求
key:List(DoubleMetric(Column,Uniform,Some(CASE WHEN(count()OVER(PARTITION BY first unspecifiedframe$())= 1)THEN true ELSE false END),value:None键:List(DoubleMetric(Column,Minimum,Some(CAST(year AS DOUBLE),值:没有一
key:List(DoubleMetric(Column,Compliance,,Some(CAST((COALESCE(CAST(year AS DECIMAL(20,10),0.0)>= 0)AS INT),value:None键:List(DoubleMetric(Column,Maximum,Some(CAST(year AS DOUBLE),值:1991.0不符合约束要求!
key:List(DoubleMetric(Column,Compliance,Some(CAST((id IS NULL)OR(id IN(1,2,3,4,5)AS INT),value:0.6666666666666不符合约束要求!
我试过filterKeys方法。但它并没有像预期的那样工作。需要帮助!

62lalag4

62lalag41#

看看你的输出,并对你的类型做最小的假设,我相信这就是你想要的:

val substring = "Some"

for ((k,v) <- map if k.toString().contains(substring)) println(s"key: $k, value: $v")

相关问题