java 如何在jdk8中使用循环流?

qyyhg6bp  于 2023-01-01  发布在  Java
关注(0)|答案(1)|浏览(164)

如何在jdk8中像这样使用for循环。

for (ClassAttendanceParent classAttendanceParent : list) {
                TransportAttendanceFormBean tb = new TransportAttendanceFormBean();
                tb.setDate(classAttendanceParent.getAttendancedate().toString());
            }

in JDK8(如何转换上述for循环)

list.forEach((ClassAttendanceParent element) -> System.out.println(element.getAttendancedate()));
cwtwac6a

cwtwac6a1#

试试这个

list.forEach(element->{
        TransportAttendanceFormBean tb = new TransportAttendanceFormBean();
        tb.setDate(element.getAttendancedate().toString());         
    });

或(无需在每次迭代中创建新对象)

TransportAttendanceFormBean tb = new TransportAttendanceFormBean();
  list.forEach(element->{   
        tb.setDate(element.getAttendancedate().toString());         
    });

相关问题