Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

211
Vistas
Combining two similar methods in Java with predicate

I have two similar methods in terms of the body, but with a different number of parameters and an extra condition inside. I know there is a way of merging them into a single method using a predicate, but I am not entirely sure how to implement it. Which is the best way to approach this?

public boolean checkIfAllCodesAreUnique(List<String> bsnCodes)
{
    List<Businesscode> codes = ConverterUtil.iterableToList(businessCodeService.findAll());
    if(codes != null && !codes.isEmpty() && bsnCodes != null && !bsnCodes.isEmpty())
        for (String code : bsnCodes)
            if (codes.stream().anyMatch(obj -> code.equals(obj.getCode())))
                return false;
    return true;
}

public boolean checkIfAllCodesAreUnique(List<String> bsnCodes, int idRole)
{
    List<Businesscode> codes = ConverterUtil.iterableToList(businessCodeService.findAll());
    if(codes != null && !codes.isEmpty() && bsnCodes != null && !bsnCodes.isEmpty())
        for (String code : bsnCodes)
            if (codes.stream().anyMatch(obj -> code.equals(obj.getCode()) && obj.getId() != idRole))
                return false;
    return true;
}
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

public boolean checkIfAllCodesAreUnique(List<String> bsnCodes) {
    return isAllCodesAreUnique(bsnCodes, businessCode -> true);
}

public boolean checkIfAllCodesAreUnique(List<String> bsnCodes, int idRole) {
    return isAllCodesAreUnique(bsnCodes, businessCode -> businessCode.getId() != idRole);
}

private boolean isAllCodesAreUnique(List<String> bsnCodes, Predicate<Businesscode> checkRole) {
    List<Businesscode> businessCodes = Optional.ofNullable(ConverterUtil
            .iterableToList(businessCodeService.findAll())).orElse(List.of());

    for (String bsnCode : Optional.ofNullable(bsnCodes).orElse(List.of())) {
        if (businessCodes.stream()
                         .filter(businessCode -> bsnCode.equals(businessCode.getCode()))
                         .anyMatch(checkRole))
            return false;
    }

    return true;
}
over 4 years ago · Santiago Trujillo Denunciar

0

Basically predicate would not allow you anything specific in the sense of auto-determinable interface or whatever. Probably the best combination of the two would be:

public boolean checkIfAllCodesAreUnique(List<String> bsnCodes, Integer idRole)
{
    List<Businesscode> codes = ConverterUtil.iterableToList(businessCodeService.findAll());
    if(codes != null && !codes.isEmpty() && bsnCodes != null && !bsnCodes.isEmpty())
        for (String code : bsnCodes)
            if (codes.stream().anyMatch(obj -> code.equals(obj.getCode()) || (idRole != null && obj.getId() != idRole))
                return false;
    return true;
}

And then pass the second parameter as null whenever not available.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda