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