【问题标题】:Get values from map by list of keys in groovy通过groovy中的键列表从地图中获取值
【发布时间】:2020-07-19 15:24:28
【问题描述】:
我有一个字符串列表,我想使用这个键列表从地图中获取值。
例如:
ListOfKeys = ["one", "two", "three"]
map =["one":1,"two":2,"three":3,"four":4]
我应该只得到前三个,因为它们在地图中,其余的不在。
谢谢
【问题讨论】:
标签:
list
dictionary
groovy
key
【解决方案1】:
ListOfKeys = ["one", "two", "three"]
map =["one":1,"two":2,"three":3,"four":4]
def values = ListOfKeys.collectEntries{ map[it] }
【解决方案2】:
ListOfKeys = ["one", "two", "three"];
map =["one":1,"two":2,"three":3,"four":4];
result = ListOfKeys.collectEntries{[it,map[it]]}
println result