How to efficiently (performance) remove many items from List in Java?

后端 未结 12 649
迷失自我
迷失自我 2021-01-31 09:00

I have quite large List named items (>= 1,000,000 items) and some condition denoted by that selects items to be deleted and is true for many (maybe hal

12条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-31 09:39

    As others have said, your first inclination should be to just build up a second list.

    But, if you'd like to also try out editing the list in-place, the efficient way to do that is to use Iterables.removeIf() from Guava. If its argument is a list, it coalesces the retained elements toward the front then simply chops off the end -- much faster than removing() interior elements one by one.

提交回复
热议问题