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

182
Vistas
Issue with optional usage (java 8)

I am trying to use stream with my code below. My problem is my return value. This piece of code return an Optional not a boolean:

ogps.stream().filter(elt -> (elt.getLineNumbers() != null && !vg.getLineNumbers().isEmpty())).findFirst();

Can you please guide me? Thks

//The original code
public static boolean isOrderOk(List<OrderGroup> ogps) {
        if (ogps == null || ogps.isEmpty()) {
            return true;
        }
        for (OrderGroup elt : ogps) {
            if (elt.getLineNumbers() != null && !vg.getLineNumbers().isEmpty()) {
                return false;
            }
        }
        return true;
}

//The new code with stream usage
public static boolean isOrderOk(List<OrderGroup> ogps) {
        if (ogps == null || ogps.isEmpty()) {
            return true;
        }
        return ogps.stream()
                .filter(elt -> (elt.getLineNumbers() != null && !vg.getLineNumbers().isEmpty()))
                .findFirst();
        return true;
}
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You are probably looking for something like:

public static boolean isOrderOk(List<OrderGroup> ogps) {
    if (ogps == null || ogps.isEmpty()) {
        return true;
    }
    return ! ogps.stream()
        .anyMatch(elt -> (elt.getLineNumbers() != null && !vg.getLineNumbers().isEmpty()));
}
over 4 years ago · Santiago Trujillo Denunciar

0

The reason is that you are using findFirst method of stream class and it returns Optional type as you can see here.

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