Filter employees by age in Java 8 Lamda steam
Categories: Java 9 Java 8(JDK1.8) Java Java Examples
package r4r.co.in.emp;
import java.util.List;
import java.util.stream.Collectors;
public class Employees {
//Filter employees by age in Java 8 Lamda steam
public static void main(String[] args) {
List<Employee> list = List.of(
new Employee(1, "Rajesh", 32), new Employee(2, "Bimal", 30), new Employee(3, "Brijesh", 35)
);
List<Employee> employeesListByAge = list.stream().filter(e -> e.getAge() > 30).collect(Collectors.toList());
System.out.print(employeesListByAge);
}
}
class Employee {
int id;
String name;
int age;
public Employee(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
Output
[Employee [id=1, name=Rajesh, age=32], Employee [id=3, name=Brijesh, age=35]]