Fast algorithm to remove a number of elements from an ArrayList

前端 未结 4 1320
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-19 18:38

Say an ArrayList is of size n.

In my case, I often need to remove from 1 to n elements with different indexes from an ArrayList.

By using visualvm profiler,

4条回答
  •  鱼传尺愫
    2021-01-19 19:18

    Check out the list of datastructures here. Pick one depending on your requirements. Like Guarev mentioned, a HashMap is probably your best bet. Hashmaps have the advantage of a constant time for insert, search, and delete.

    ArrayLists are not a good structure for a storing a lot of data, as the memory usage quickly goes through the roof, and search/delete times get very expensive very quickly.

提交回复
热议问题