Invalidating iterator

Posted by / 04-Nov-2020 07:59

Not much performance concern in building new lists even when fairly large. From what you wrote, it seems that the pairs are not directional, as in (A, B) is equal to (B, A). But then this clashes with the fact that you only remove the second element of the pair when the condition if satisfied.

Your operation could potentially be non-deterministic depending on the ordering in the list.

My question therefore is, are there best practices for such a case (where we have a nested iteration over the same list, and want to modify it in the inner loop), or do I have to accept a loss of performance with no alternative other than ignoring the warning in the documentation and hoping the underlying implementation (which might be outside of my control) doesn't mind?

I mean, there's always the alternative of implementing a linked list myself with a custom iterator that guarantees such a thing, but that seems like too much work.

A third alternative I can think of is to create a second list of "elements to remove", and then go through the list and remove them from the main list.

A is true for the inputs 01 and 11, while B is true for 11, and C for 00.

This means that (A, B) fulfills the condition since A contains all true inputs of B, therefore B is superfluous and can be deleted.

You can then just stop referencing the original list and let garbage collection do the work.

Keep in mind you're working with references in java; you won't be copying objects, but rather references.

invalidating iterator-47invalidating iterator-77invalidating iterator-45

Another characteristic, is that if (A, B) fulfills the condition, (B, A) can only fulfill it if A is of them.