I have a spring project where i have a boolean method which uses optional to filter through a set of requests. Im getting an error when i try return request.isEmpty()
, I looked it up and i might be using an older version of java but is there any alternative way to say request.isEmpty()
without having to update my version of java as Im worried if i update it, it will affect the rest of my project
This is my code for the method
private boolean hasNoDaysOffOnShiftDate(List<Request> requests, ShiftParams params) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // for string
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // for localdate
// Shift start date in LocalDate
String shiftDate = (params.start).format(formatter);
LocalDate formatDateTime = LocalDate.parse(shiftDate, formatter);
System.out.println("Shift date in date format " + formatDateTime);
Optional<Request> request = requests.stream().filter(req -> req.getStatus() == Status.Accepted)
.filter(req -> isDayOffOnShiftDate(req, formatDateTime)).findAny();
return request.isEmpty();
}
The error I am getting is
The method
isEmpty()
is undefined for the typeOptional<Request>
I am using this version of java
java.version=1.8.0_73
Optional#isEmpty()
is a Java 11 method, which is a shortcut for Java 8's !Optional#isPresent()
.
return !request.isPresent();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With