【问题标题】: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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-29
      • 2011-05-10
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      • 2011-08-09
      • 1970-01-01
      • 2022-08-16
      相关资源
      最近更新 更多