Find employee whose salary is more than 10000 using java 8 stream
package ajitation;
import java.util.*;
public class Test {
String empName;
int salary;
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public Test(String empName, int salary) {
super();
this.empName = empName;
this.salary = salary;
}
public static void main(String[] args) {
List<Test> list = new ArrayList<>();
list.add(new Test(“Ajit”, 9000));
list.add(new Test(“Akash”, 15000));
list.add(new Test(“Abhay”, 32000));
list.add(new Test(“Kunal”, 35000));
list.stream().filter(emp -> emp.getSalary() > 10000)
.forEach(emp -> System.out.println(emp.getEmpName() + ” “ + emp.getSalary()));
}
}
Output :
Akash 15000
Abhay 32000
Kunal 35000
Java program to find employee whose salary is more than 10000 using java 8 stream
This is a Java program that defines a class called “Test” within the “ajitation” package. The “Test” class has two instance variables: “empName” of type String and “salary” of type int. It also has getter and setter methods for both variables.
The class has a constructor that takes two parameters, “empName” and “salary”, and initializes the instance variables with the values passed as arguments.
In the main method of the class, a list of “Test” objects is created using the ArrayList class. Four “Test” objects are added to the list, each representing an employee with a name and a salary.
The list is then streamed, and a filter is applied to select only those employees whose salary is greater than 10000. For each selected employee, the name and salary are printed to the console using the forEach method.
In summary, this program demonstrates the use of Java streams and lambda expressions to filter and print employee names and salaries based on a their salary amount.