You can remove an item from a Collection - including a Set - whilst iterating over it as long as you iterate over it using an Iterator and call Itertator.remove() to do the remove. However, this will only allow you to remove the current item in the iteration.
You can't remove another item or add one as this will cause a ConcurrentModificationException, since if you change the Set it's not clear how you would iterate over it.
Also, unlike a List, is doesn't quite make sense to talk about replacing an entry in a Set. In a List items have a define order so, for example, if the second entry was "A" you could replace it with "B". Items in a Set don't have an order so you can't replace one with another directly, all you can do is remove the old one and add the new one.
Depending quite on what you want to do, your best approach might be to loop over a copy of the Set:
Set<Object> originalSet = someMethod();
for (Object item : new HashSet<Object>(originalSet)) {
//do stuff which modifies originalSet
}
However, you'd have to account for the fact that the objects you iterated over would be the original values and wouldn't reflect any changes you'd made.
If this won't do, then it might make sense to find another way of process the items in the Set without simply iterating over them by keeping track of which nodes you've processed.
Something like this might do but could probably be improved depending on what you're doing:
Set<Object> originalSet = somemethod();
//Keep track of items we've already processed
Set<Object> processed = new HashSet<Object>();
//toDo is used to calculate which items in originalSet aren't in processed
Set<Object> toDo = new HashSet(originalSet);
toDo.removeAll(processed);
while (! toDo.isEmpty()) {
//Get an object from toDo
Object item = toDo.iterator().next();
//Do some processing
//Can update originalSet or even remove from processed if we want to re-do an item
//Recalculate what's left to do
processed.add(item);
toDo = new HashSet(originalSet);
toDo.removeAll(processed);
}