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;
}
anyMatch. It is a short-circuit terminal operation made exactly for this purpose.getmethod