Xiaobai learns Java: so is the iterator

Xiaobai learns Java: so is the iterator

In the previous article, we talked about the old iterator enumeration and the new iterator that replaces it. What are the advantages of this new species compared with the past?

The word iterator, before looking up a lot of data, I only know a general idea. I know it can be used to traverse a collection, but I haven't delved into its mysteries. This article makes a small summary about iterator iterator to consolidate learning. If there is any misunderstanding or improper description, please comment on the pointer in the comment area.

Iterator overview

The official document explains iterator as follows:

No, no, this description is too brief. I continue to look for information:

Iterator design pattern

I was full of curiosity about the above, so I drew and drew in the complex inheritance relationship, and finally gradually clarified the embodiment of the so-called iterator mode in the collection. For the time being, take ArrayList as an example:

Iterator defined method

By looking at the source code, I found that in the ITR implementation class, two pointers are defined: cursor and lastret. (another attribute is expectedmodcount, which is initialized to the version number modcount of ArrayList, which is related to the fail fast mechanism and will be mentioned later). Cursor is initialized to 0, which is specifically used to compare the number and size of collection elements. Lastret is initialized to - 1. If the next operation is successfully performed, it will add 1 to become 0, that is, the "next element" mentioned above It is conceivable that lastret can be regarded as the pointer before initialization to the first element, which is the same as the index of the value to be returned, which will be easier to remember.

In addition to the above two methods, jdk1 8 adds two methods, which also reflects its new advantage different from the old iterator: it supports deletion.

    List<Integer> list = new ArrayList<>();
    list.add(1);
    list.add(2);
    list.add(3);
    //创建一个Iterator对象
    Iterator<Integer> it = list.iterator();
    //返回第一个值
    System.out.print(it.next()+" ");

The test result is obvious. Only the first element is output: 1. We continue our new method based on the original code:

    it.forEachRemaining(new Consumer<Integer>() {
        @Override
        public void accept(Integer integer) {
            System.out.print(integer+" ");
        }
    });

The test results are: 1, 2, 3. On the original basis, the remaining elements are printed. The new method is actually the same as the one we are familiar with:

    while(it.hasNext()){
        System.out.print(it.next()+" ");
    }

Iterators: unified approach

Through the study of the methods defined in iterator, we probably know that the purpose of iterator is to traverse elements one by one from front to back, regardless of its internal structure. Hey, I know everything about traversal, but where does the structure reflect? Don't worry. Let's take a look at an example. Let's ignore the structure of two different sets:

First, we define a method that can receive an iterator object:

    public static void display(Iterator<?> T){
        while(T.hasNext()){
            System.out.print(T.next());
        }
    }

Then we create two different collections, one is ArrayList and the other is HashSet, which is disordered. We should learn the corresponding source code next.

        //ArrayList 有序
        List<String> list = new LinkedList<>();
        list.add("天");
        list.add("乔");
        list.add("巴");
        list.add("夏");
        //HashSet 无序
        Set<Integer> set = new HashSet<>();
        set.add(11);
        set.add(22);
        set.add(33);
        set.add(44);
        display(list.iterator());//天 乔 巴 夏
        System.out.println();
        display(set.iterator());//33 22 11 44

It can be seen that after the iterators of two different collections pass in the display method, they can access the elements in the collection in the same way. Through the above analysis, we can determine that iterators unify the way to access containers.

Summary of iterator

Finally, there is a part about iterators, which will be summarized in the future. Reference: Dahua design pattern, Java programming thought

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>