Dry goods | An article to understand the implementation principle and use of Java Iterator iterator

Iterator (iterator) is sometimes called cursor (cursor) is a software design pattern of programming, an interface that can be visited on container objects (containers, such as linked lists or arrays), designers do not need to care about the implementation details of the memory allocation of container objects , As long as you get this object, you can use the iterator to traverse the inside of the object


Preface

ArrayList uses internal classes to implement the Iterator interface, and provides iterator methods to obtain iterator objects, which can be used to traverse the sequence table
Insert picture description here

One, iterator

java.util.Iterator interface: Iterator (traversing the collection)
has three methods
boolean hasNext() If there are still elements to iterate, it returns true.
Determine if there is another element in the set, return true if there is, false if not

E next() returns the next element of the iteration.
Take out the next element in the collection.
Iterator is an interface, we cannot use it directly, we need to use the implementation class object of the Iterator interface, and the way to obtain the implementation class is quite special.

remove() deletes the elements returned by the
iterator. There is a method in the Collection interface called iterator(). This method returns the iterator implementation class object.
Iterator iterator() returns an iterator that iterates over the elements of this collection.

Steps to use iterator

Steps for using the iterator (emphasis):
1. Use the method iterator() in the collection to obtain the implementation class object of the iterator, and use the Iterator interface to receive (polymorphism)
2. Use the method hasNext in the Iterator interface to determine if there is another Element
3. Use the method next in the Iterator interface to take out the next element in the collection

Three, code example

   //创建一个对象
        Collection<String> coll= new ArrayList<>();
        //往集合添加元素
        coll.add("易建联");
        coll.add("徐杰");
        coll.add("任骏飞");
        coll.add("胡明轩");
        coll.add("赵睿");
        /*
        1.使用集合中的方法iterator()获取迭代器的实现类对象,使用Iterator接口接收(多态)
        注意:
        Iterator<E>接口也是有泛型的,迭代器的泛型跟着集合走,集合是什么泛型,迭代器就是什么泛型
         */
         
        //多态  接口            实现类对象
        Iterator<String> it = coll.iterator();
        //2.使用Iterator接口中的方法hasNext判断还有没有下一个元素
        boolean b = it.hasNext();
        //3.使用Iterator接口中的方法next取出集合中的下一个元素
        String s = it.next();
        System.out.println(s);
        it.remove();
//      删除最近一次已近迭代出出去的那个元素。
//     只有当next执行完后,才能调用remove函数。
//     比如你要删除第一个元素,不能直接调用 remove()   而要先next一下( );
//     在没有先调用next 就调用remove方法是会抛出异常的。
//     这个和MySQL中的ResultSet很类似

Four, iterator principle

When traversing the collection, first obtain the iterator object by calling the iterator() method of the t collection, and then use the hashNext() method to determine whether there is the next element in the collection, if it exists, call the next() method to take the element out, otherwise it will indicate The end of the collection has been reached, stop traversing the elements.

When the Iterator object traverses the collection, it uses pointers internally to track the elements in the collection. In order for beginners to better understand the working principle of the iterator, a legend is used to demonstrate the process of iterator object iteration:

Before calling the next method of Iterator, the index of the iterator is located before the first element and does not point to any element. When the next method of the iterator is called for the first time, the index of the iterator will move one bit backward and point to the first Element and return the element. When the next method is called again, the index of the iterator will point to the second element and return the element, and so on, until the hasNext method returns false, indicating that the end of the collection is reached, and the pair is terminated. Traversal of elements.

Guess you like

Origin blog.csdn.net/weixin_46235428/article/details/109298306
Recommended