I have a reactive program and i am adding to an Arraylist employees, as long as there isn't an employee with the same id following the code bellow.The thing is that it returns null even though the Arraylist has no employees in, So there can't be one with the same.
public Uni<Employee> addEmployeee(Employee employee){
return Multi.createFrom().iterable(employees)
.onItem().transform(empl -> {
if(empl.getId().equals(employee.getId())){
return null;
}else {
employees.add(employee) ;
}
return employee ;
}).toUni() ;
}
I have achieved that with the following code but i think the previous code is better.
public Uni<Employee> addEmployee(Employee employeeToAdd){
return Uni.createFrom().item(employees)
.onItem().transform(employees ->{
Optional<Employee> exists = employees.stream().filter(empl -> empl.getId().equals(employeeToAdd.getId())).findFirst();
if(exists.isEmpty()){
employees.add(employeeToAdd);
return employeeToAdd;
}
return null;
});
}
Can someone tell me what i'm doing wrong here ?