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

后端 未结 12 672
迷失自我
迷失自我 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:28

    Rather than muddying my first answer, which is already rather long, here's a second, related option: you can create your own ArrayList, and flag things as "removed". This algoritm makes the assumptions:

    • it's better to waste time (lower speed) during construction than to do the same during the removal operation. In other words, it moves the speed penalty from one location to another.
    • it's better to waste memory now, and time garbage collecting after the result is computeed rather than spend the time up front (you're always stuck with time garbage collecting...).
    • once removal begins, elements will never be added to the list (otherwise there are issues with re-allocating the flags object)

    Also, this is, again, not tested so there's prlolly syntax errors.

    public class FlaggedList extends ArrayList {
      private Vector flags = new ArrayList();
      private static final String IN = Boolean.TRUE;  // not removed
      private static final String OUT = Boolean.FALSE; // removed
      private int removed = 0;
    
      public MyArrayList(){ this(1000000); }
      public MyArrayList(int estimate){
        super(estimate);
        flags = new ArrayList(estimate);
      }
    
      public void remove(int idx){
        flags.set(idx, OUT);
        removed++;
      }
    
      public boolean isRemoved(int idx){ return flags.get(idx); }
    }
    

    and the iterator - more work may be needed to keep it synchronized, and many methods are left out, this time:

    public class FlaggedListIterator implements ListIterator
    {
      int idx = 0;
    
      public FlaggedList list;
      public FlaggedListIterator(FlaggedList list)
      {
        this.list = list;
      }
      public boolean hasNext() {
        while(idx
        

提交回复
热议问题