简体   繁体   English

通过 groovy 中的键列表从 map 获取值

[英]Get values from map by list of keys in groovy

I have a list of strings, i would like to get values from a map, using this list of keys.我有一个字符串列表,我想使用这个键列表从 map 获取值。

For example:例如:

ListOfKeys = ["one", "two", "three"]
map =["one":1,"two":2,"three":3,"four":4]

I should get only the first three because they are in the map, the rest are not.我应该只得到前三个,因为它们在 map 中,而 rest 不在。

Thanks谢谢

ListOfKeys = ["one", "two", "three"]
map =["one":1,"two":2,"three":3,"four":4]

def values = ListOfKeys.collectEntries{ map[it] }
ListOfKeys = ["one", "two", "three"];
map =["one":1,"two":2,"three":3,"four":4];

result = ListOfKeys.collectEntries{[it,map[it]]}

println result

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM