如何在jdk 8中使用流循环?

时间:2014-06-28 06:55:41

标签: java java-8

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

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

(如何转换上面的for循环)

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

1 个答案:

答案 0 :(得分:0)

试试这个

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

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

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