Remove from a list during iteration


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  1. // REMOVE: Any items that are not in the temporary list should be removed
  2. List<TravelProfilePopupDisplay> itemsToRemove = new ArrayList<TravelProfilePopupDisplay>();
  3. for (TravelProfilePopupDisplay originalProfileDisplay : originalList) {
  4.  
  5. boolean foundMatch = false;
  6. for (TravelProfilePopupDisplay tempProfileDisplay : tempTravelProfileDisplays ) {
  7. if (tempProfileDisplay.getID() == originalProfileDisplay.getID()) {
  8. foundMatch=true;
  9. }
  10. }
  11. // If the item only exists in the original list, then remove it
  12. if (foundMatch==false) {
  13. // Note: Cannot remove from originalList during iteration
  14. itemsToRemove.add( originalProfileDisplay );
  15. }
  16. }
  17. originalList.removeAll( itemsToRemove );

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.