简体   繁体   中英

How to remove an entire list after from master list in java using Iterator

I have one List<List<Address>> object and another List<AddressList> Object. I have to iterate through this list and keep deleting objects from list after my first loop executes. How can I do this?

List<List<Address>> addressLists;
List<Address> addressList;
for (List<Address> listOfAddress : this.addressLists) {
    for (Address address : listOfAddress) {
        //some code
    }
}

How can I delete or remove object from addressLists after processing?

PS I am using Java 7.

Use an explicit iterator:

Iterator<List<Address>> iter = this.addressLists.iterator();
while (iter.hasNext()) {
    List<Address> listOfAddress = iter.next();
    for (Address address : listOfAddress) {
        //some code
    }
    iter.remove();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM