简体   繁体   中英

How to create an arraylist satisfying a condition using lambda expression

I have to write a method body of class Employee containing 3 objects and return an arraylist of employees whose attendance is greater than 70% using lambda expression.

Java

public List<Employee> findAttendance(List<Employee> emp) {
    List<Employee> l = new ArrayList<Employee>();
    l.forEach(emp -> (if(emp.getAttendance() >= 70)) {
            l.add(emp);
     });
}
List<Employee> empsWithGoodAttendance = l.stream()
                                          .filter(e -> e.getAttendance() >= 70)
                                          .collect(Collectors.toList());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM