Groovy-更改嵌套地图中字符串的值 [英] Groovy - Change value of string in nested map

查看:65
本文介绍了Groovy-更改嵌套地图中字符串的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Groovy中具有以下数据结构

I have the following data structure in Groovy

我想知道Groovy是否提供一种简单的方法(最好是单行)来遍历此数据结构,找到具有键"code"的TreeMap条目,并将trim()函数应用于其值.正如您在图像中看到的那样,键"code"的值是"1880".我要修剪它并使其成为"1880".谢谢.

I want to know if Groovy offers a simple way (Preferably one-liner) to iterate through this data structure, find the TreeMap entry which has a key of "code" and apply the trim() function to it's value. As you can see in the image, the value the key "code" is "1880 ". I want to trim it and make it "1880". Thanks.

推荐答案

我尝试过此方法,它对您的数据有效:

I tried this one and it worked for your data:

ageTypes = ageTypes.collect { it.each { it.value = (it.value instanceof String) ? it.value.trim() : it.value } }

UPD我相信可以通过某种方式对其进行优化.但是需要检查方法.

UPD I believe it can be optimized somehow. But need to check how.

这篇关于Groovy-更改嵌套地图中字符串的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆