Iterator deleting collection elements

Keywords: Java

Iterator deleting collection elements

Labels (space separated): j2se

The iterator interface is also a member of the Java Collection framework. It is mainly used to traverse (i.e. iterate) the elements in the Collection collection. Iterator objects are also called iterators

  • boolean hasNext(): returns true if the set elements in the iteration are not traversed

  • Object next(): returns the next element in the collection

  • void remove(): delete the element returned by the next method last time in the collection

Iterator pattern is the standard access method for traversing collection classes. It can abstract the access logic from different types of collection classes, so as to avoid exposing the internal structure of the collection to the client.

public static void forRemove(List<String> list,String target){

        for(int i = 0;i<list.size();i++){
            if(list.get(i).equals(target)){
                list.remove(i);
            }
        }
    }

    public static void IteratorRemove(List<String> list,String target){
        Iterator<String> iterator = list.iterator();

        while(iterator.hasNext()){
            String s  =  iterator.next();
            if(s.equals(target)){
                iterator.remove();
            }
        }
    }

    public static void IteratorRemoveStu(List<Student> list,String str){

        Iterator<Student> iterator = list.iterator();
        while(iterator.hasNext()){
            Student stu  = iterator.next();
            if(stu.getName().equals(str)){
                iterator.remove();
            }
        }
    }

main test

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        list.add("e");
        list.add("c");

        /*IteratorRemove(list,"c");*/
        /*forRemove(list,"c");*/

        List<Student> listStu = new ArrayList<>();
        listStu.add(new Student("Zhang San", 25));
        listStu.add(new Student("Li Si", 36));
        listStu.add(new Student("Wang Wu", 12));
        listStu.add(new Student("Li Si", 36));
        listStu.add(new Student("Zhang San", 25));

        /*for(String str:list){
            System.out.println(str);
        }*/
        IteratorRemoveStu(listStu,"Zhang San");

        for(Student stu :listStu){
            System.out.println(stu);
        }
    }

For remove, when the data volume is large, the efficiency is particularly low

Iterator must be attached to Collection object, iterator The iterator adopts the fail fast mechanism. Once the Collection has been modified (usually by other threads in the program) during the iteration, the program immediately raises the ConcurrentModificationException exception instead of displaying the modified result, which can avoid potential problems caused by shared resources

Be careful:

It is more convenient for foreach loop to visit element set iteratively, but the iterative variable in foreach loop is not the set itself. The system only assigns the elements of the set in turn to the iterative variable, which cannot be used to delete the set.

Posted by burnside on Tue, 31 Mar 2020 19:54:37 -0700