JamesLi1
JamesLi1

Reputation: 1

Java Multiple Maps Check if Contains

I have around 30 HashMaps of data and i have run into a situation where i need to check if a certain value exists inside any of the hashmaps.

For example

if (map1.contains(value)){ //remove the value with map1.remove() }
if (map2.contains(value)){ //remove the value with map2.remove() }
if (map3.contains(value)){ //remove the value with map3.remove() }
//and so on

I could just do that 30 times but im not sure how efficient that would be, is there a much cleaner way to do this? any help would be appreciated thank you.

Upvotes: 0

Views: 594

Answers (1)

pL4Gu33
pL4Gu33

Reputation: 2085

30 Maps are hard and i dont know about your use case.

You can hold it in a list and iterate over it but you should refactor the class.

List<Map<String, Object>> testList = new ArrayList<>();
...//add maps

    for(Map<String, Object> map : testList){
        map.containsKey(...)
    }

Upvotes: 2

Related Questions