• Post Reply Bookmark Topic Watch Topic
  • New Topic

Enhance for loop and collection API  RSS feed

 
Syed Saifuddin
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody

I want to do some sort of filtration in ArrayList based on some comdition. I am doing this in enhance for loop but it throws Exception.

sample code is

for(ScoreBoardCategoryBatch innerBatch : productList) {
if (outerBatch.getProductId().equals(innerBatch.getProductId()) && outerBatch.getCategoryName().equals(innerBatch.getCategoryName())) {
outerBatch.setBatchName(outerBatch.getBatchName()+", "+innerBatch.getBatchName());
}
productList.remove(innerBatch);
}

Any idea to accomplish is appreciated

Thanks All
 
Martin Simons
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be safe on this sort of operation (since you are actually removing items from your collection while you are iterating forward through it) I would use the old for loop and iterate through the collection backwords.
 
Syed Saifuddin
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Martin Simons,

Please explain the below statement

I would use the old for loop and iterate through the collection backwords.

What does backwords means in this condition
 
Martin Simons
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


if you iterate from 0 to the end, then when you delete one, you effectively skip the next, since you essentially shift all items one spot to the left, so the item that would normally come next occupies the index you have just finished with.

Going backwards this does not happen, as everything that gets shifted, has already been inspected.
 
Richard Green
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I normally use predicates for this purpose. See PredicateTest.java for an example
http://www.idevelopment.info/data/Programming/java/collections/SUB_Predicate_Filtering_Example.shtml
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Martin Simons:


if you iterate from 0 to the end, then when you delete one, you effectively skip the next, since you essentially shift all items one spot to the left, so the item that would normally come next occupies the index you have just finished with.

Going backwards this does not happen, as everything that gets shifted, has already been inspected.


An even better approach is to use an Iterator and its remove method.
 
Martin Simons
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
An even better approach is to use an Iterator and its remove method.[/QB]


So true. I am still getting used to this all this Collections and Iterator stuff. What they are is clear, but I still have the mindset of going through them manually, rather than using the Iterator. I have got to get used to that.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm, given that we're talking about an ArrayList rather than LinkedList, I'd prefer to avoid either of these (if there's any chance the list may be significantly large). Removing from the beginning or interior of an ArrayList() is generally an O(N) operation, where N is more properly the number of elements after the item being removed. Every time you remove (or insert), you shift the position of all subsequent elements in the array. Making it O(N^2) to filter the entire ArrayList. Acceptable for a short list maybe, but in general, ugh. This is true for both the removale techniques discussed, although removing from the rear of an ArrayList will be faster. Depending on how many later list elements are retained along the way, there may or may not be a significant problem with O(N^2) by the time you get to the front of the list.

Anyway, for an ArrayList I'd rather just create a new list, and add the elements to be retained to the new list. Even if in the end you need to have those elements retained in the original, not just in a copy - it will be faster to use a separate intermediate list to avaoid the crappy performance of ArrayList's remove() methods:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!