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

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

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 Report

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 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!