Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip mapping an element if condition not satisfied

Assuming I have the following

public class UserEntity{
    String id;
    List<String> relatedEntity;
}

public class EmployeeEntity{
    String id;
    String name;
    Boolean isActive;
    List<String> relatedEntityDetails;
}

Now having a list of UserEntity I have to map to a list of EmployeeEntity:

private List<EmployeeEntity> getEmployees(List<UserEntity> users)
     return users.stream()
         .filter(x-> !x.getRelatedEntity().isEmpty())
         .map(this::mapToEmployee)
         .collect(Collectors.toList());
}

private EmployeeEntity mapToEmployee(UserEntity userEntity){
    // retrieve EmployeeEntity from DB and perform a validations like
    // employeeEntity.isActive = true
    return employeeEntity;
}

Now, everything works fine, however I need to handle the scenario when EmployeeEntity is not present in db, or isActive = false, in these scenarios, the map() should be skipped, so if there is a list of 3 elements UserEntity and for one of those users, one employee is not active, then the returned List should have only 2 elements.

Any suggestions on how to add that behaviour?

like image 535
VIOO66 Avatar asked Sep 12 '25 01:09

VIOO66


2 Answers

Make mapToEmployee return an Optional<EmployeeEntity>, and filter one those that are not empty:

private List<EmployeeEntity> getEmployees(List<UserEntity> users)
     return users.stream()
         .filter(x-> !x.getRelatedEntity().isEmpty())
         .map(this::mapToEmployee)
         .filter(Optional::isPresent)
         .map(Optional::get)
         .collect(Collectors.toList());
}

private Optional<EmployeeEntity> mapToEmployee(UserEntity userEntity){
    ... optional depending on whether it is present in DB and/or is active
}
like image 168
M A Avatar answered Sep 13 '25 16:09

M A


In any case you NEED to query the databse for every employee so i propose:

  • mapping all empoyees (do not filter by isActive in the mapper, see comment in code)
  • filtering inactive employees

private List<EmployeeEntity> getEmployees(List<UserEntity> users)
     return users.stream()
         .filter(x-> !x.getRelatedEntity().isEmpty())
         .map(this::mapToEmployee)
         .filter(Optional::isPresent)
         .map(Optional::get)
         .filter(EmployeeEntity::isActive)
         .collect(Collectors.toList());
}

private Optional<EmployeeEntity> mapToEmployee(UserEntity userEntity){
    // return the db employee whatsoever, i believe the mapper should not be responssible for logic validation
    if (employeeDoesNotExistsInDb) {
        return Optional.empty();
    }
    return employeeEntity;
}

I would advise you to use the mapper as a mapper, not as a mapper + specific validator. That validation should happen later on on the process. It would make the code more clear and more reusable. Also you'll maybe want to rename getEmployees as getActiveEmployees

EDIT: Thanks to @M A to remind me of the Optional

like image 36
Anthony Raymond Avatar answered Sep 13 '25 14:09

Anthony Raymond