java123999
java123999

Reputation: 7394

Checking that at least Value in HashMap matches?

I am trying to figure out how to iterate through my HashMap to see if at least one Value in it matches what I am looking for.

for(int i=0; i<allDogsInkennels.size(); i++){
    Map<String, DogStatus> allDogsStatus = allDogsInKennels.get(i).getAllStatuses();

}

How can I add an If statement / loop here to check that at least one of the statuses matches e.g. "APPROVED".

Note: String= the Dogs Id ,DogStatus= Enum showing dog's status

Upvotes: 0

Views: 369

Answers (2)

Idos
Idos

Reputation: 15320

You can iterate over the map and stop if you hit a match:

boolean found = false;
for (DogStatus value : allDogsStatus.values()) {
    if (value == DogStatus.APPROVED) {
        found = true;
        break;
    }
}

Even better is to refactor the above into a function that return the boolean.

Edit: I feel dumb for not remembering the containsValue method. That's probably the best approach, even though complexity is the same.

Upvotes: 1

Radiodef
Radiodef

Reputation: 37875

if (allDogsStatus.containsValue(DogStatus.APPROVED)) {
    // ...
}

Upvotes: 3

Related Questions