开发者

Skip items while iterating over a long list, whats more efficient?

开发者 https://www.devze.com 2023-03-28 07:19 出处:网络
I am working with a java.util.List which is expected to contain approximately 70k Objects. It is built up from an ordered database query, so it is always in the same order every time. This list is bei

I am working with a java.util.List which is expected to contain approximately 70k Objects. It is built up from an ordered database query, so it is always in the same order every time. This list is being iterated over as part of a long running task which can get halted due to external issues. So I need to skip X elements in the list the next time the task runs.

Handling this in the database query that builds the list is not an option for various reasons.

What is the most efficient way to skip over X items in a list before doing the heavy lifting?

int skip = getNumberOfItemsToSkip();
int count = 0;
for(MyThing thing : bigList){
 if(count >= skip){
  //do stuff
 }
}

OR

int skip = getNumberOfItemsToSkip();
int count = 0;
//does subList maintain the order????
List<MyThing> sublist = bigList.subList(skip, bigList.size() - 1);
开发者_如何学Pythonfor(MyThing thing : sublist){
  //do stuff
}

Is there another way?


In this case I would not use the an enhanced for loop. If you access by index, then you could simply start and end at a range that makes since.

for(int i = getNumberOfItemsToSkip(); i < bigList.size(); i++) {
  Foo foo = bigList.get(i);
}


I wouldn't use a List, which requires O(N) to iterate. I'd use a Map with the key from the query and access each element in O(1).


You could do something like:

int skip = getNumberOfItemsToSkip();
for ( int i = skip; i < bigList.size(); i++ ) {
    MyThing = list.get(i);
    // do stuff
}

If you are using an ArrayList then this will be fine, if you are using a LinkedList it will be worse.


You can do it by Hand, you save the iterating over the skipped items.

int skip = getNumberOfItemsToSkip();
int count = 0;

for(int i=0;i<bigList.size();i++){
  if(i >= skip){
    MyThing elem = bigList.get(i);
    //do stuff
  }else{
    i=i+skip-1;
  }
}


the subList will keep everything in the same order (the end index is exclusive so you'll want to use bigList.subList(skip, bigList.size());)

however a ListIterator will do it without that overhead

int skip = getNumberOfItemsToSkip();
int count = 0;
for(Iterator<MyThing> it = bigList.listIterator(skip);it.hasNext();)
 MyThing thing =it.next();
  //do stuff
}

another way is to simply not create the full list when you don't need to (this will depend on how this is generated ofcourse)

0

精彩评论

暂无评论...
验证码 换一张
取 消