Nye*_*ard 5 groovy dictionary list
如果我在Groovy中得到一个包含2个或多个带有某个值的变量的列表,并且想查看它是否包含给定的文本字符串,我将执行以下操作:
def msg = '''
Hello Mars!
'''
def msg1 = '''
Hello world!
'''
def list = [msg, msg1]
list.findAll { w ->
if(w.contains("Hello"))
{
println w
}
else
{
println "Not there"
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我不想打印值,而是要打印包含文本的变量名。列表是否有可能还是我需要制作地图?
您需要使用Map,因为从键到值的映射。
def msg = '''
Hello Mars!
'''
def msg1 = '''
Hello world!
'''
def map = [msg: msg, msg1: msg1]
map.findAll { key, value ->
if (value.contains("Hello")) {
println key
}
else {
println "Not there"
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9233 次 |
| 最近记录: |