简体   繁体   English

Java从ArrayList中删除哈希图,如果它不包含某些键集

[英]java removing hashmap from arraylist if it does not contain certain key sets

Let's say I have arraylist of Hashmap, and Hashmap contain some keys and values 假设我有Hashmap的arraylist,并且Hashmap包含一些键和值

Arraylist d = [{key1=1,key2=2},{key1=1,key3=3}]

I want to remove hashmap that does not contain certain key. 我想删除不包含某些键的哈希图。 for example, I want to remove hashmap that does not have key2. 例如,我要删除没有key2的哈希映射。

result should be: 结果应该是:

d= [{key=1,key2=2}]

How do I approach this? 我该如何处理?

If source is 如果来源是

List<Map<String, Integer>> list;

You can filter it that way: 您可以这样过滤:

List<Map<String, Integer>> newList =
         list.stream().filter(m -> m.containsKey("key2")).collect(Collectors.toList());

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

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