简体   繁体   English

按列表过滤 Groovy map

[英]Filtering Groovy map by a list

In groovy is there a simple way to filter a map`s indexes by a list.在 groovy 中有一种简单的方法可以通过列表过滤地图的索引。

produce_map        = ["grapes":"fruit","apple":"fruit","tomato":"vegetable","peas:"vegetable"]
green_produce_list = ["grapes","apples",'peas']

<magic code>

//desired result
filterd_map = ['apple':'fruit','grapes':'fruit','peas':'vegetable'] 

Use subMap :使用subMap

Creates a sub-Map containing the given keys.创建一个包含给定键的子映射。 This method is similar to List.subList() but uses keys rather than index ranges.此方法类似于 List.subList() 但使用键而不是索引范围。 The original map is unaltered.原厂 map 不变。

 def orig = [1:10, 2:20, 3:30, 4:40] assert orig.subMap([1, 3] as int[]) == [1:10, 3:30] assert orig.subMap([2, 4] as Integer[]) == [2:20, 4:40] assert orig.size() == 4

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

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