英文:
Filter keys from map if it contains substring Scala
问题
I have a map type Map[String,String] when i need to filter few keys when it does not have keyword Some
. When key does not contain Some
, i need to remove those key from Map.
For example:
scala> for ((k,v) <- map) println(s"key: $k, value: $v")
Expected result:
key: List(DoubleMetric(Column,Completeness,Some((first IS NOT NULL)))), value: 0.6666666666666666 does not meet the constraint requirement
key: List(DoubleMetric(Column,Uniqueness,Some(CASE WHEN (count() OVER (PARTITION BY first unspecifiedframe$()) = 1) THEN true ELSE false END))), value: None key: List(DoubleMetric(Column,Minimum,Some(CAST(year AS DOUBLE)))), value: None
key: List(DoubleMetric(Column,Compliance,,Some(CAST((COALESCE(CAST(year AS DECIMAL(20,10)), 0.0) >= 0) AS INT)))), value: None key: List(DoubleMetric(Column,Maximum,Some(CAST(year AS DOUBLE)))), value: 1991.0 does not meet the constraint requirement!
key: List(DoubleMetric(Column,Compliance,Some(CAST(((id IS NULL) OR (id IN (1, 2, 3, 4, 5))) AS INT)))), value: 0.6666666666666666 does not meet the constraint requirement!
key: List(DoubleMetric(Dataset,Size,*,Success(3.0),None)), value: 3 does not meet the constraint requirement!
I tried used filterKeys
method. But it is not working as expected. Need help!
英文:
I have a map type Map[String,String] when i need to filter few keys when it does not have keyword Some
. When key does not contain Some
, i need to remove those key from Map.
For example:
scala> for ((k,v) <- map) println(s"key: $k, value: $v")
> key: List(DoubleMetric(Column,Completeness,Some((first IS NOT
> NULL)))), value: 0.6666666666666666 does not meet the constraint
> requirement
>
> key: List(DoubleMetric(Column,Uniqueness,Some(CASE WHEN (count() OVER
> (PARTITION BY first unspecifiedframe$()) = 1) THEN true ELSE false
> END))), value: None key:
> List(DoubleMetric(Column,Minimum,Some(CAST(year AS DOUBLE)))), value:
> None
>
> key:
> List(DoubleMetric(Column,Compliance,,Some(CAST((COALESCE(CAST(year AS
> DECIMAL(20,10)), 0.0) >= 0) AS INT)))), value: None key:
> List(DoubleMetric(Column,Maximum,Some(CAST(year AS DOUBLE)))), value:
> 1991.0 does not meet the constraint requirement!
>
> key: List(DoubleMetric(Column,Compliance,Some(CAST(((id IS NULL) OR
> (id IN (1, 2, 3, 4, 5))) AS INT)))), value: 0.6666666666666666 does
> not meet the constraint requirement!
>
> key: List(DoubleMetric(Dataset,Size,*,Success(3.0),None)), value: 3
> does not meet the constraint requirement!
Expected result:
> key: List(DoubleMetric(Column,Completeness,Some((first IS NOT
> NULL)))), value: 0.6666666666666666 does not meet the constraint
> requirement
>
> key: List(DoubleMetric(Column,Uniqueness,Some(CASE WHEN (count() OVER
> (PARTITION BY first unspecifiedframe$()) = 1) THEN true ELSE false
> END))), value: None key:
> List(DoubleMetric(Column,Minimum,Some(CAST(year AS DOUBLE)))), value:
> None
>
> key:
> List(DoubleMetric(Column,Compliance,,Some(CAST((COALESCE(CAST(year AS
> DECIMAL(20,10)), 0.0) >= 0) AS INT)))), value: None key:
> List(DoubleMetric(Column,Maximum,Some(CAST(year AS DOUBLE)))), value:
> 1991.0 does not meet the constraint requirement!
>
> key: List(DoubleMetric(Column,Compliance,Some(CAST(((id IS NULL) OR
> (id IN (1, 2, 3, 4, 5))) AS INT)))), value: 0.6666666666666666 does
> not meet the constraint requirement!
I tried used filterKeys
method. But it is not working as expected. Need help!
答案1
得分: 1
I believe this is what you want:
val substring = "Some"
for ((k,v) <- map if k.toString().contains(substring)) println(s"key: $k, value: $v")
英文:
Looking at your output and making minimal assumptions about your types, I believe this is what you want:
val substring = "Some"
for ((k,v) <- map if k.toString().contains(substring)) println(s"key: $k, value: $v")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论