Suhaila
Suhaila

Reputation: 1

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);
     });
}

Upvotes: 0

Views: 479

Answers (1)

Jude Niroshan
Jude Niroshan

Reputation: 4460

List<Employee> empsWithGoodAttendance = l.stream()
                                          .filter(e -> e.getAttendance() >= 70)
                                          .collect(Collectors.toList());

Upvotes: 1

Related Questions