g**********y 发帖数: 14569 | 1 I find this annoyance in ArrayList/HashSet/HashMap...
If I want to loop through iterator and delete some elements on condition, I
have to do it:
1. Loop through iterator and find all elements satisfy condition, save them
in a collection.
2. Delete temp collection elements from original collection.
I have to do two steps because iterator can't be broken during loop. Is
there a better/simpler way to do this? I have no limitation on the data
structure.
Thanks. | g*****g 发帖数: 34805 | 2 This is probably the best practice anyway, you know
delete one item at a time from an Array can be N times
slower than what you are doing here.
I
them
【在 g**********y 的大作中提到】 : I find this annoyance in ArrayList/HashSet/HashMap... : If I want to loop through iterator and delete some elements on condition, I : have to do it: : 1. Loop through iterator and find all elements satisfy condition, save them : in a collection. : 2. Delete temp collection elements from original collection. : I have to do two steps because iterator can't be broken during loop. Is : there a better/simpler way to do this? I have no limitation on the data : structure. : Thanks.
| m******t 发帖数: 2416 | 3 You can certainly do iterator.remove() while in the loop. That's what
AbstractCollection.removeAll does anyway.
I
them
【在 g**********y 的大作中提到】 : I find this annoyance in ArrayList/HashSet/HashMap... : If I want to loop through iterator and delete some elements on condition, I : have to do it: : 1. Loop through iterator and find all elements satisfy condition, save them : in a collection. : 2. Delete temp collection elements from original collection. : I have to do two steps because iterator can't be broken during loop. Is : there a better/simpler way to do this? I have no limitation on the data : structure. : Thanks.
|
|