'How to use stream on method that return boolean value with multiple condition
I am using this method:
private boolean getUsageDisabled(controlUnitVersion cUv) {
Partnumber partn=cUv.getcontrolUnit().getpartnumber();
for (controlUnit cu: partn.getcontrolUnit()) {
for (ControlUnitVersion cUv2: cu.getcontrolUnitVersion()) {
for (release r: cUv2.getRelease()) {
if (r.getWorkflowState().getKey().equals("locked") ||
r.getWorkflowState().getKey().equals("released")) {
return true;
}
}
}
return false;
}
}
and I want to be able to change it using Stream , I've tried the following, but I am missing something:
partn.getcontrolUnit()
.stream()
.forEach(cu -> cu.getControlUnitVersion()
.stream()
.filter(cUv2 -> cUv2.getrelease()
.stream()
.forEach(release -> {
if (release.getWorkflowState().getKey().equals("locked") ||
release.getWorkflowState().getKey().equals("released")) {
return true;
}
})
)
);
but I am getting an error.
Solution 1:[1]
You have a list of a list, and you want to perform some computation in the nested list with the stream. The flatMap() method could resolve your problem.
And you want to return a boolean, if there are any elements in stream that match a given condition, anyMatch() might be a good choice.
partn.getcontrolUnit().stream()
.flatMap(cu -> cu.getcontrolUnitVersion().stream())
.anyMatch(r -> r.getWorkflowState().getKey().equals("locked") || r.getWorkflowState().getKey().equals("released"));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |