There we are using following approach to Null and empty check in employees in java 8.
- Create Employee class with id, name and salary object.
- Make a list of employee type object.
- Now get the Stream data from List using arrayList.stream() method.
- Inside filter, validate if e.getName() is not equals to null and empty, then its return those employees data.
- From map, we fetch the employees salary at depend on filter condition.
- From collect() method, we collect all the salary.
- Print the salary of all employees.
public class Employee {
private Integer id;
private String name;
private Integer salary;
public Employee(Integer id, String name, Integer salary) {
super();
this.id = id;
this.name = name;
this.salary = salary;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getSalary() {
return salary;
}
public void setSalary(Integer salary) {
this.salary = salary;
}
}
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class EmployeeCheck {
public static void main(String[] args) {
List<Employee> emp = Arrays.asList(new Employee(1, "anup", 120), new Employee(2, null, 1300),new Employee(3, "vijay", 7230), new Employee(4, "", 678), new Employee(5, "aman", 8230));
List<Integer> name = emp.stream().filter(e -> null != e.getName() && !e.getName().isEmpty()).map(e -> e.getSalary()).collect(Collectors.toList());
System.out.println("fetch employee salary with null and empty validation :- " + name);
}
}
Output :-
fetch employee salary with null and empty validation :- [120, 7230, 8230]