从映射中过滤包含子字符串 “Scala” 的键。

huangapple go评论98阅读模式
英文:

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&gt;     for ((k,v) &lt;- map) println(s&quot;key: $k, value: $v&quot;)

> 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 = &quot;Some&quot;

for ((k,v) &lt;- map if k.toString().contains(substring)) println(s&quot;key: $k, value: $v&quot;)

huangapple
  • 本文由 发表于 2023年8月5日 05:22:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76839161.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定