Removing items from a collection in java while iterating over it

后端 未结 10 1774

I want to be able to remove multiple elements from a set while I am iterating over it. Initially I hoped that iterators were smart enough for the naive solution below to wor

10条回答
  •  眼角桃花
    2020-11-28 09:05

    You should call Iterator.remove method.

    Also note, that on most java.util collections the remove method will generate exception if the contents of the collection have changed. So, if the code is multi-threaded use extra caution, or use concurrent collections.

提交回复
热议问题