remove() method in Iterator iterator [java]

remove() method in Iterator iterator

remove() method is defined inside Iterator

  • The remove() method in Iterator iterator is different from the remove() method in Collection interface
    • The remove() method in the Collection interface is parameterized (the parameter is Object obj)
      • The parameter object matches the elements in our collection, and if it matches, the matched elements in our collection are removed from our collection
    • remove() method in Iterator iterator is parametric
      • This method is to move the elements in the set that our iterator object points to out of the set
Our parameterless remove() method creates an instance object through the implementation subclass (Itr class) of the Iterator interface, and then calls the remove() method in our Iterator interface through this instance object
package iterator;

import java.util.Collection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Objects;

public class IteratorDemo7 {
    public static void main(String[] args) {
        /*
        At this point we created an object of the ArrayList class that we used to call the iterator() method to create an object of the Itr class
         */
        Collection c1 = new ArrayList();
        c1.add(1234);
        c1.add("abc");
        c1.add(false);
        c1.add(7890);
        c1.add(false);
        Iterator iterator = c1.iterator();
        /*
        Here we call the hasNext() method of the iterator object to determine if there are any elements in our collection that have not been traversed, and return a true if there are any
         */
        while(iterator.hasNext()){
            /*
            Once we enter the while loop here, we use the iterator (our iterator object) to call the next() method to get our current iterator object
            The element pointing to the next position of the element (the return value of the next() method we call here is an element in a collection)
             */
            Object obj = iterator.next();
            /*
            Here we remove the false element from our collection, and when the element currently traversed is false, we enter the execution of the if statement
            This removes the collection elements pointed to by the current iterator object, which allows us to unify the removal of elements from the current collection
            Remove all false elements from our c1 collection
             */
            if(obj.equals(false)) {
                iterator.remove();
            }
        }
        /*
        Here we create a new Iterator object (actually an instantiated object of class Itr, which implements our Iterator interface)
        Note: The newly created Iterator object points to the previous position of the first element in the collection
         */
        Iterator iterator2 = c1.iterator();
        while(iterator2.hasNext()){
            /*
            At this point we iterate through the collection using the new Iterator object iterator2, at which point the false element in our c1 collection has been deleted
            Note: here it is determined that the matching false is when the equals() method in the Boolean (wrapper class) is called to match
             */
            System.out.println(iterator2.next());
        }

    }
}

  • As we can see from the example above, we can use the remove() method in an iterator to filter through the elements in our collection
    • You can use the remove() method in the Iterator class to filter the collection elements while iterating through the undesired elements in our collection

Be careful:

If we create a new Iterator implementation class object, we have not called the next() method at this time. If we use the remove() method in the Iterator interface directly, or if we use the remove() method in the Iterator interface twice in a row after calling the next() method, an exception will be thrown at the location where we use the remove() method the next time. (IllegalStateException) (Illegal State)

  1. If an object of an Iterator implementation class is newly created, then if we haven't called the next() method yet, the object created by our Iterator should point to the previous location of the first element in our collection, apparently nothing, then we call remove() in the Iterator interface. Method, the function of this method is to delete from the set the element that the iterator object that calls our remove() method currently points to, but at this point our iterator object points to an empty location, and there must be no element to delete it
  2. If we use the remove() method twice in a row after a next() method, then our first remove() method has deleted the object that our iterator calling this remove() method points to. At this time, our iterator object still points to this object, and then we use the remove() method again. This is when we delete an element that has already been deleted. When we remove an element from a collection by using the remove() method on the Iterator interface, the corresponding state of the element changes, indicating that the element has been removed from the collection. Then we will find that the element has been removed by using the remove() method again. Then an IllegalStateException exception is thrown back here (the state is not legal)

Tags: Java Back-end

Posted on Thu, 02 Dec 2021 13:09:51 -0500 by Negligence