增強for、lambda for、stream 遍歷List 結束方法 or 跳過循環本次循環


增強for、lambda for、stream 遍歷List 結束方法 or 跳過循環本次循環

        List<Integer> list = Arrays.asList(1, 2, 3, 4);
        System.out.println("forEach");
        list.forEach(e -> {
            if (e == 2) {
                return; // 結束本次循環
            }
            System.out.println(e);
        });

        System.out.println("stream().anyMatch");
        list.stream().anyMatch(e -> {
            if (e == 2) {
                return false; // 結束本次循環
//                return true; // 結束方法
            }
            System.out.println(e);
            return false;
        });

        System.out.println("for");
        for (Integer i : list) {
            if (i == 2) {
                continue; // 結束本次循環
//                return; // 結束方法
            }
            System.out.println(i);
        }

  

 


免責聲明!

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



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