0

I am iterating over a list of POJO objects called txnList (say, size = 2). If an object falls into an invalid criteria, then I am removing it from this txnList list (now, size = 1). However, I want to process the rest of the elements in txnList but since the size is 1, it doesn't process further and exits from the top most loop altogether.

Below is my code, with bare minimum essential details:

for (OrgTransaction txn : txnList) {
    String txnId = txn.getTxnNumber());
    Order order = util.getOrder(txnId);
    
    boolean isInvalid = false;
    List<CollectionType> collectionTypeList = util.getCollectionType(order);
    for (CollectionType collectionType : collectionTypeList) {
        if (isNotValid(collectionType)) {
            // first transaction in the list is invalid so it enters here
            txnList.remove(txn);
            isInvalid = true;
            // Breaking since we do not want to process anything in this nested list if one of the invalid things is found.
            break;
        }
    }

    if (!isInvalid) {
        // process only valid transactions further      
    }
    // NOTE: now after first iteration of processing invalid case, this should go to top to iterate 2nd transaction but it just returns from the list since the new size is 1 and 1 time the `txnList` loop has iterated.
}

FYI: No exception is thrown in this code as a result of deletion

1
  • 1
    Does your code run without throwing an exception? AFAIK, you are not allowed to delete an object from a list while iterating it. At least not the way you are doing it. Commented Aug 5, 2021 at 22:42

3 Answers 3

2

Remove the item with the iterator, which needs to know about the operation.

for (Iterator<OrgTransaction> iterator = txnList.iterator(); iterator.hasNext(); ) {
   OrgTransaction txn = iterator.next();

   ... 
   iterator.remove();
   ... 
}

This also avoids exceptions due to concurrent modification.

1
  • Thank you @Andy Thomas and all others who replied.
    – Atihska
    Commented Aug 5, 2021 at 23:38
1

There are 2 possible ways:

  1. create a list of elements you want to remove, then use list.removeAll(removeList)
for(...){
 if (isInvalid) {
    removeList.add(txn)
}
...
txnList.removeAll(removeList);
  1. while traversing a list, create a new list which does not contain elements you do not need. Then just reassign.
for(...){
 if (!isInvalid) {
    newList.add(txn)
}
...
txnList = newList;
1
  • The second option here is a common idiom, and a good way to go. +1 Commented Aug 5, 2021 at 22:44
-1

One method you can use is to just use a for loop and decrement the index each time you remove an element:

for (int x = 0; x < txnList.size(); x++;) {
    txn = txn.get(x); //define txn
    String txnId = txn.getTxnNumber());
    Order order = util.getOrder(txnId);
    
    boolean isInvalid = false;
    List<CollectionType> collectionTypeList = util.getCollectionType(order);
    for (CollectionType collectionType : collectionTypeList) {
        if (isNotValid(collectionType)) {
            // first transaction in the list is invalid so it enters here
            txnList.remove(txn);
            x--; //decrement index
            isInvalid = true;
            // Breaking since we do not want to process anything in this nested list if one of the invalid things is found.
            break;
        }
    }

    if (!isInvalid) {
        // doSomething      
    }
    // NOTE: now after first iteration of processing invalid case, this should go to top to iterate 2nd transaction but it just returns from the list since the new size is 1 and 1 time the `txnList` loop has iterated.
}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.