简体   繁体   中英

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

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.

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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