在Groovy中过滤映射键的长度

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

Filter map key length in Groovy

问题

我正在尝试创建一个新地图,从另一个地图中提取键,并尝试插入长度小于13个字符的键。我尝试过像这样过滤:

Map<String, String> maps = entriesOnlyOnLeft.findAll { it.key }.each { it.key.length() < 13 }

输出:

log.info("Key Map ---> "+maps.keySet())
[431486899900600, 280799200020001, 251206899900600, 080196899900604, 350166899900600, 180876899900600, 260896899900600, 372746899900600, 442166899900600, 330446899900600, 401946899900600, 110126899900600, 200696899900600, 410916899900600, 060156899900600, 210416899900600, 040136899900600, 290676899900600, 140216899900600, 020036899900600, 360386899900600, 312016899900600, 280796800220073, 451686899900600, 150306899900600, 280796800110071, 280796899900604, 320546899900600, 492756899900600, 240896899900600, 380386899900600, 000000E04921301, 100376899900600, 480206899900600, 000000E00004101, 280796800330051, 280796800330064, 050196899900600, 170796899900600, 390756899900600, 520016899900600, 000000E04921701, 280796899900075, 280796899900074, 280796899900077, 280796899900076, 280796899900079, 280796899900078, 280796899900065, 280796899900053, 280796899900057, 280796899900040, 280796899900041, 280796899900046, 280796899900045, 280796899900048, 280796899900049, 191306899900600, 030146899900600, 280796899900032, 280796899900035, 280796899900034, 280796899900036, 280796899900039, 280796899900038, 280796800440072, 341206899900600, 160786899900600, 130346899900600, 120406899900600, 510016899900600, 502976899900600, 471866899900600, 270286899900600, 300306899900600, 090596899900600, 000000E04924801, 230506899900600, 462506899900600, 070406899900600]

希望这可以帮助您。

英文:

I am trying to create a new map, extracting from another map the keys and trying to insert the ones that are less than 13 characters long. I have tried to filter like this but it is impossible for me.

Map<String, String> maps = entriesOnlyOnLeft.findAll { it.key }.each { it.key.length() < 13 }

Output:

log.info("Key Map ---> "+maps.keySet())
[431486899900600, 280799200020001, 251206899900600, 080196899900604, 350166899900600, 180876899900600, 260896899900600, 372746899900600, 442166899900600, 330446899900600, 401946899900600, 110126899900600, 200696899900600, 410916899900600, 060156899900600, 210416899900600, 040136899900600, 290676899900600, 140216899900600, 020036899900600, 360386899900600, 312016899900600, 280796800220073, 451686899900600, 150306899900600, 280796800110071, 280796899900604, 320546899900600, 492756899900600, 240896899900600, 380386899900600, 000000E04921301, 100376899900600, 480206899900600, 000000E00004101, 280796800330051, 280796800330064, 050196899900600, 170796899900600, 390756899900600, 520016899900600, 000000E04921701, 280796899900075, 280796899900074, 280796899900077, 280796899900076, 280796899900079, 280796899900078, 280796899900065, 280796899900053, 280796899900057, 280796899900040, 280796899900041, 280796899900046, 280796899900045, 280796899900048, 280796899900049, 191306899900600, 030146899900600, 280796899900032, 280796899900035, 280796899900034, 280796899900036, 280796899900039, 280796899900038, 280796800440072, 341206899900600, 160786899900600, 130346899900600, 120406899900600, 510016899900600, 502976899900600, 471866899900600, 270286899900600, 300306899900600, 090596899900600, 000000E04924801, 230506899900600, 462506899900600, 070406899900600]

答案1

得分: 1

这是翻译好的部分:

"Here's the solution:

Map<String, String> results = entriesOnlyOnLeft.findAll { it.key.length() < 13 }

findAll 中使用的闭包必须返回一个真值结果,才会将该 Map.Entry 包括在结果中。而且 findAll 闭包将为给定的 Map 中的每个项(即 entriesOnlyOnLeft)调用,所以不需要调用 each。此外,each 返回 void,所以不会通过调用 each 返回任何结果给调用者。"

英文:

You almost had it. Here's the solution:

Map<String,String> results = entriesOnlyOnLeft.findAll { it.key.length() < 13 }

The closure used in findAll must return a truthy result in order for it to include that Map.Entry in the results. Also the findAll closure will be invoked for each item in the given Map (ie entriesOnlyOnLeft) so there is no need for a call to each. Plus each returns void so there wouldn't be any results returned to the caller by call each.

答案2

得分: 0

以下是已经翻译好的部分:

    Map<String, String> maps = new HashMap<>();
    entriesOnlyOnLeft.each { it ->
        if (it.key.length() < 13) {
            maps.put(it.key, it.value)
        }
    }
英文:

You can try the following way (Somewhat mutating way)

Map&lt;String, String&gt; maps = new HashMap&lt;&gt;();    
entriesOnlyOnLeft.each { it -&gt;     
    if (it.key.length() &lt; 13) {
        maps.put(it.key, it.value)
    } 
}

huangapple
  • 本文由 发表于 2023年2月19日 02:34:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495523.html
匿名

发表评论

匿名网友

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

确定