[Java Collection] Use Iterator to traverse Collection and the execution principle of Iterator Iterator

Use the Iterator interface to traverse collection elements

Iterator objects are called iterators (a type of design pattern), and are mainly used to traverse the elements in the Collection collection.

GOF defines the iterator pattern as: providing a way to access elements in a container object without exposing the internal details of the object. The iterator pattern is born for containers . Similar to "conductor on the bus", "stewardess on the train", "stewardess".

The Collection interface inherits the java.lang.Iterable interface, which has an iterator() method, so the

A collection class that implements the Collection interface has an iterator() method that returns an object that implements the Iterator interface.

Iterator is only used to traverse collections , and Iterator itself does not provide the ability to hold objects. If you need to create an Iterator object, you must have an iterated collection.

Every time the collection object calls the iterator() method, a new iterator object is obtained , and the default cursor is before the first element of the collection.

 Methods of the Iterator interface

 

 

 Before calling the it.next() method, it.hasNext() must be called for detection. If not called, and the next record is invalid, calling it.next() directly will throw NoSuchElementException.

 

The traversal operation of the collection elements uses the Iterator interface of the iterator

method one:

    @Test
    public void test1(){
        Collection coll = new ArrayList();
        coll.add(456);
        coll.add(123);
        coll.add(new Person("Jerry", 20));
        coll.add(new String("Tom"));
        coll.add(false);

        Iterator iterator = coll.iterator();
        System.out.println(iterator.next());
        System.out.println(iterator.next());
        System.out.println(iterator.next());
        System.out.println(iterator.next());
        System.out.println(iterator.next());
    }

The result of the operation is as follows:

If we add another sentence: System.out.println(iterator.next());

 The result of the operation is as follows:

 We can see that the exception is reported: NoSuchElementException

Method 2:

    @Test
    public void test1(){
        Collection coll = new ArrayList();
        coll.add(456);
        coll.add(123);
        coll.add(new Person("Jerry", 20));
        coll.add(new String("Tom"));
        coll.add(false);

        Iterator iterator = coll.iterator();
        //方式一:
//        System.out.println(iterator.next());
//        System.out.println(iterator.next());
//        System.out.println(iterator.next());
//        System.out.println(iterator.next());
//        System.out.println(iterator.next());
//        //报异常:NoSuchElementException
//        System.out.println(iterator.next());

        //方法二:不推荐
        for (int i = 0; i < coll.size(); i++) {
            System.out.println(iterator.next());
        }
    }

operation result

Method three:

    public void test1(){
        Collection coll = new ArrayList();
        coll.add(456);
        coll.add(123);
        coll.add(new Person("Jerry", 20));
        coll.add(new String("Tom"));
        coll.add(false);

        Iterator iterator = coll.iterator();
        //方式一:
//        System.out.println(iterator.next());
//        System.out.println(iterator.next());
//        System.out.println(iterator.next());
//        System.out.println(iterator.next());
//        System.out.println(iterator.next());
//        //报异常:NoSuchElementException
//        System.out.println(iterator.next());

        //方法二:不推荐
//        for (int i = 0; i < coll.size(); i++) {
//            System.out.println(iterator.next());
//        }

        //方式三:推荐
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

 The result of the operation is as follows:

The traversal operation of the collection elements uses the Iterator interface of the iterator

Internal methods: hasNext() and next()

The execution principle of the iterator Iterator

 

 

 thanks for watching! ! !

Guess you like

Origin blog.csdn.net/qq_64976935/article/details/129739964