forEachRemaining()方法的用法


forEachRemaining()是java1.8新增的Iterator接口中的默認方法
對於這個方法,官方文檔是這么描述的:
Performs the given action for each remaining element until all elements have been processed or the action throws an exception. Actions are performed in the order of iteration, if that order is specified. Exceptions thrown by the action are relayed to the caller.
簡單來說,就是對集合中剩余的元素進行操作,直到元素完畢或者拋出異常。這里重要的是剩余元素,怎么理解呢,下面就來用代碼解釋一下:

import java.util.*;
public class Test{
    public static void main(String[] args){
        //創建一個元素類型為Integer的集合
        Collection<Integer> collection =  new HashSet<>();
        for (int i=0;i<10 ;i++ ){
            //向集合中添加元素
            collection.add(i);
        }
        //獲取該集合的迭代器
        Iterator<Integer> iterator= collection.iterator();
        //調用forEachRemaining()方法遍歷集合元素
        iterator.forEachRemaining(ele -> System.out.println(ele));
    }
}
輸出為:
0
1
2
3
4
5
6
7
8
9

這是預料之中的結果。

那繼續看下面代碼:

import java.util.*;
public class Test
{
    public static void main(String[] args)
    {
        //創建一個元素類型為Integer的集合
        Collection<Integer> collection =  new HashSet<>();
        for (int i=0;i<10 ;i++ )
        {
            //向集合中添加元素
            collection.add(i);
        }
        //獲取該集合的迭代器
        Iterator<Integer> iterator= collection.iterator();
        //調用迭代器的經過集合實現的抽象方法遍歷集合元素
        while(iterator.hasNext())
        {
            System.out.println(iterator.next());
        }
        System.out.println("--------------");
        //調用forEachRemaining()方法遍歷集合元素
        iterator.forEachRemaining(ele -> System.out.println(ele));
        
    }
}

這時輸出為:
0
1
2
3
4
5
6
7
8
9
--------------

明明調用了迭代器兩個遍歷方法,怎么會只遍歷一次呢?
問題就出在剩余里,當第一次調用迭代器的經過集合實現的抽象方法遍歷集合元素時,迭代器就已經將元素遍歷完畢,也就是說迭代器中已經沒有剩余元素了,因此這時調用forEachRemaining()方法,就什么也不輸出了,為了驗證,再來看下面代碼:

        //獲取該集合的迭代器
        Iterator<Integer> iterator= collection.iterator();
        //調用forEachRemaining()方法遍歷集合元素
        int i=0;
        while(iterator.hasNext())
        {
            System.out.println(iterator.next());
            i++;
            if (i==5)
            {
                break;
            }
        }
        System.out.println("--------------");
        //調用forEachRemaining()方法遍歷集合元素
        iterator.forEachRemaining(ele -> System.out.println(ele));
        
    }
}
這時輸出:
0
1
2
3
4
--------------
5
6
7
8
9

可以看到,當我們第一次用迭代器遍歷時,只讓它遍歷五次就跳出循環,那么就還剩下五個元素,再調用forEachRemaining()方法,就可以看到輸出后五個元素了

測試代碼:

        List<String> list = new ArrayList<>();
        list.add("aaa");
        list.add("bbb");
        Iterator<String> iterator = list.iterator();
        System.out.println("第一次遍歷");
        iterator.forEachRemaining(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });
        System.out.println("第二次遍歷");
        while(iterator.hasNext()){
            String item = iterator.next();
            System.out.println(item);
        }
        System.out.println("第三次遍歷");
        iterator.forEachRemaining(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });
        System.out.println("第四次遍歷");
        Iterator<String> iterator2 = list.iterator();
        while(iterator2.hasNext()){
            String item = iterator2.next();
            System.out.println(item);
        }

輸出結果:

第一次遍歷
aaa
bbb
第二次遍歷
第三次遍歷
第四次遍歷
aaa
bbb

參考文章:https://blog.csdn.net/qq_43717113/java/article/details/105062570


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM