Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

214
Views
Combinando dos métodos similares en Java con predicado

Tengo dos métodos similares en cuanto al cuerpo, pero con un número diferente de parámetros y una condición adicional en el interior. Sé que hay una forma de fusionarlos en un solo método usando un predicado, pero no estoy del todo seguro de cómo implementarlo. ¿Cuál es la mejor manera de abordar esto?

 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 answers
Answer question

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 Report

0

Básicamente, el predicado no le permitiría nada específico en el sentido de una interfaz autodeterminable o lo que sea. Probablemente la mejor combinación de los dos sería:

 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; }

Y luego pase el segundo parámetro como nulo siempre que no esté disponible.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!