Filter employees by age set senior if age is greater than 30 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 EmployeAge50ThenEmployeeTypeAsSenior {
//Filter employees by age set senior if age is greater than 30 in Java 8 Lamda steam
public static void main(String[] args) {
List<EmployeeType> list = List.of(
new EmployeeType(1, "Rajesh", 32, "JUNIOR"), new EmployeeType(2, "Bimal", 30, "JUNIOR"),
new EmployeeType(3, "Brijesh", 35, "JUNIOR")
);
List<EmployeeType> seniorEmployees = list.stream().filter(e -> e.getAge() > 30).peek(e -> e.setType("Senior"))
.collect(Collectors.toList());
System.out.print(seniorEmployees);
}
}
class EmployeeType {
int id;
String name;
int age;
String type;
public EmployeeType(int id, String name, int age, String type) {
this.id = id;
this.name = name;
this.age = age;
this.type = type;
}
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;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", type=" + type + ", age=" + age + "]";
}
}
Output
[Employee [id=1, name=Rajesh, type=Senior, age=32], Employee [id=3, name=Brijesh, type=Senior, age=35]]