Here we are Increase salary of employees by 10%, only for those whose salary is less than 20,000 in Java 8.
- Create Employee class with name, age and salary parameter.
- Make list of Employee object using Arrays.asList() method.
- Now get the Stream data from List using arrayList.stream() method.
- Using filter to check whose employee’s salary is less than 20000.
- After finding the employees, inside map we increase the salary by 10 % using m -> m.getSalary() * 10 method.
- Print the Employee salary.
class Employee {
private String name;
private Integer age;
private Double salary;
public Employee(String name, Integer age, Double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public String toString() {
return "Employee Name: " + this.name + " Age: " + this.age + " Salary: " + this.salary;
}
}
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class EmployeeSalaryTest {
public static void main(String[] args) {
List<Employee> employeeList = Arrays.asList(new Employee("Negombo", 35, 7000.00),
new Employee("Brent", 56, 100000.00), new Employee("Matara", 75, 8000.00),
new Employee("Gerry", 43, 120000.00), new Employee("Galle", 28, 9000.00));
List<Double> list = employeeList.stream().filter(r -> r.getSalary() < 20000).map(m -> m.getSalary() * 10)
.collect(Collectors.toList());
System.out.println("Increase Employee Salary :-" + list);
}
}
Output :-
Increase Employee Salary :-[70000.0, 80000.0, 90000.0]