| Author |
Enhance for loop and collection API
|
Syed Saifuddin
Ranch Hand
Joined: Sep 01, 2003
Posts: 129
|
|
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
|
Thank You & Best Regards,
Syed Saifuddin,
Senior Software Engineer
SAP Oracle AIX & Java Training
http://www.socialinet.com
|
 |
Martin Simons
Ranch Hand
Joined: Mar 02, 2006
Posts: 196
|
|
|
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
Joined: Sep 01, 2003
Posts: 129
|
|
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
Joined: Mar 02, 2006
Posts: 196
|
|
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
Joined: Aug 25, 2005
Posts: 536
|
|
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
|
MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
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.
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
Martin Simons
Ranch Hand
Joined: Mar 02, 2006
Posts: 196
|
|
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
Joined: Jan 30, 2000
Posts: 18670
|
|
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:
|
"I'm not back." - Bill Harding, Twister
|
 |
 |
|
|
subject: Enhance for loop and collection API
|
|
|