Home  >  Article  >  Java  >  remove trap in java collection iterator Iterator

remove trap in java collection iterator Iterator

高洛峰
高洛峰Original
2016-11-22 17:08:241276browse

package TestList;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;

public class TestIterator {

/**
     * @param args
    */
Public static void main(String[] args) {
// TODO Auto-generated method stub
List list = new ArrayList();
list.add("aaa");
list.add("bbb");
       list.add("ccc");
       Iterator t = list.iterator();
                                                                                    can "bbb")){ //When using iterator to traverse the list collection, if you want to delete an element in any collection, you must delete it when the second to last element is traversed
                                                                                                                                                                                                                                             }
                                                                                                                                 TreeSet< add("fff");
Iterator t1 = set.iterator();
                                                                                                                                                                                                                                  can The elements in a set must be deleted when the first element from the last is traversed
              set.remove("eee"); The difference, therefore, causes List and Set to behave differently when deleting collection elements while iterating.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn