I have a list of Employee
, and I want to retrieve only one Employee
information with the specific name:
public static Employee getAllEmployeeDetails(String employeeName) {
List<Employee> empList = getAllEmployeeDetails();
Employee employee = empList.stream().filter(x -> x.equals(employeeName));
return employee;
}
Please let me know how to filter the data and return a single element.
This will locate the first Employee
matching the given name, or null
if not found:
Employee employee = empList.stream()
.filter(x -> x.getName().equals(employeeName))
.findFirst()
.orElse(null);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With