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

后端 未结 12 677
迷失自我
迷失自我 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-31 09:33

    Removing a lot of elements from an ArrayList is an O(n^2) operation. I would recommend simply using a LinkedList that's more optimized for insertion and removal (but not for random access). LinkedList has a bit of a memory overhead.

    If you do need to keep ArrayList, then you are better off creating a new list.

    Update: Comparing with creating a new list:

    Reusing the same list, the main cost is coming from deleting the node and updating the appropriate pointers in LinkedList. This is a constant operation for any node.

    When constructing a new list, the main cost is coming from creating the list, and initializing array entries. Both are cheap operations. You might incurre the cost of resizing the new list backend array as well; assuming that the final array is larger than half of the incoming array.

    So if you were to remove only one element, then LinkedList approach is probably faster. If you were to delete all nodes except for one, probably the new list approach is faster.

    There are more complications when you bring memory management and GC. I'd like to leave these out.

    The best option is to implement the alternatives yourself and benchmark the results when running your typical load.

提交回复
热议问题