I think it might be possible to continue to do this iteratively and still simplify it a lot. Maybe I'm misunderstanding the algorithm, but can't you get rid of the changed field and just iterate until you've removed everything from the map as having been completely processed? and then in the inner loop you only break if you don't need to know the rest of the states to know the answer. And then use an iterator to iterate over the map and call iterator.remove so that you don't have to break out of the for loop every time you change it.