Learn how to check null and empty value in Employee List in Java 8 Stream API. Filter employee records with valid names and retrieve salaries using stream(), filter(), map(), and collect() methods.
Code Explanation point wise
- Create an Employee class with id, name, and salary attributes.
- Create an Employee constructor to initialize employee objects and getter/setter methods to access and modify object values.
- Arrays.asList() creates a list of employee objects.
- emp.stream() converts the employee list into a Stream for processing.
- .filter(e -> e.getName() != null && !e.getName().isEmpty()) keeps only those employees whose name is neither null nor an empty string (“”).
- .map(Employee::getSalary()) extracts the salary from each filtered employee object.
- . collect(Collectors.toList()) collects all salary values into a new List<Integer>.
- Print the list containing only the salaries of employees whose names are neither null nor empty.
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 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 salaries = emp.stream()
.filter(e -> e.getName() != null && !e.getName().isEmpty())
.map(Employee::getSalary)
.collect(Collectors.toList());
System.out.println(
"Fetch employee salary with null and empty validation: "
+ salaries);
}
}
Output :
Fetch employee salary with null and empty validation: [120, 7230, 8230]
Null and Empty Check in Java 8
List salaries = emp.stream()
.filter(e -> java.util.Objects.nonNull(e.getName()))
.filter(e -> !e.getName().trim().isEmpty())
.map(Employee::getSalary)
.collect(Collectors.toList());
Here are some related Java 8 programs that will help you understand Stream API concepts better:
FAQ
How do you perform a null check in Java 8 Stream API?
Use the filter() method with a null condition:
.filter(e -> e.getName() != null)
How do you check for empty strings in Java 8?
Use the isEmpty() method:
.filter(e -> !e.getName().isEmpty())
Why use filter() for null and empty validation in Java 8?
The filter() method removes invalid records before further stream processing.
What is the advantage of using Stream API for validation?
It provides concise, readable, and functional-style code.
Can filter() and map() be used together in Java 8?
Yes. First filter records, then use map() to transform the data.