Three newer ways to traverse the list array

数据: List< Student > studentList;

  1. By enhancing the for loop
		for (Student student : studentList) {
    
    
	    	System.out.println(student);
		}
  1. Traverse through stream
        //@FunctionalInterface 函数式接口
        studentList.stream().forEach(new Consumer<Student>(){
    
    
            //接收对象 对Student对象的处理
            @Override
            public void accept(Student student) {
    
    
                System.out.println(student);
            }
        });

  1. Traverse through a functional interface (only one method of a functional interface is called)
		//  (函数式接口方法声明)->{方法代码}
        System.out.println("\n   ===函数式接口方法遍历输出  ===   ");
        studentList.stream().forEach(student->{
    
    
            System.out.println(student);
        });

Guess you like

Origin blog.csdn.net/qq_40542534/article/details/108804633