Enhanced for loop when collection is empty

  When the list is null, a null pointer exception will be reported; if the list is an empty collection with a length of 0, it will not

    @Test
    public void test(){
    
    
        List<String> list2 = new ArrayList<>();
        for (String s : list2) {
    
    
            System.out.println(s);
        }
        List<String> list1 = null;
        for (String s : list1) {
    
    
            System.out.println(s);
        }
    }

Guess you like

Origin blog.csdn.net/bai_mi_student/article/details/123891343