Removing an entry from the map while iterating it isn't safe.  To do this safely you must use an Iterator's remove() method to whack the entry you don't want anymore.

Worse, the default map returned by getAllRefs() doesn't fully implement the Map contract here.  More specifically, its Iterator is broken and doesn't implement remove().

Also, how are we going to do this efficiently in Gerrit Code Review?  Remember we are getting refs/changes/nn/ccnn/p refs one at a time in this loop.  Doing a database query to see what branch that change is on is horribly inefficient.  Instead it would be more efficient to query Gerrit's database and do a sort-merge-join thing inside of the filter.

So basically I wonder if we shouldn't just pass the entire map into the filter, and let it return to us a map that we work with.  Where that map might be the original map, or the original map after making changes, or just a whole new map instance that holds a selection of the original map's entries.