- Given an object,
MyObj
which for our purposes holdsString message
among other fields. - Given a HashMap,
Map <MultiKey<String>,MyObj> map
.
I want to loop through the HashMap finding any element where the MyObj's message is searchValue
. I'm essentially trying to use stream().anyMatch()
on a Map, I simply want to know if searchValue
exists even once anywhere - i.e. short circuiting is preferable.
Foreach loop:
map.forEach((k,v) -> {
if (v.message.equalsIgnoreCase(searchValue)) {
return true;
}
}
The issue with this is that it doesn't terminate early, neither break
or return
can be used in this lambda to terminate the loop early.
I see the stream anyMatch() function:
map.entrySet().stream().anymatch(....
but I can't figure out the proper syntax - if that will work in this case at all (can it still be used if I'm not comparing the map's elements, rather I'm comparing each element's fields).