Issue
I have a method that works fine. This is how it looks.
private boolean roomWithMoreThanTenFurnitures(Building building) {
if (building != null && building.hasRooms()) {
for (Room room : building.getRooms()) {
if (room.getFurnitures.size() > 10) {
return true;
}
}
}
return false;
}
I wanna switch this to Lambda. In came uo with the shell, but I am not sure how fill in the if (condition) return true or return false outside.
building.getRooms().forEach(room -> {
//??
});
Solution
You can do it like this. Returns false
based on the initial conditions, then streams the rooms. This presumes the rooms can be streamed (e.g a List). If they are an array you will need to do something similar to Arrays.stream(building.getRooms())
private boolean roomWithMoreThanTenFurnitures(Building building) {
if (building != null && building.hasRooms()) {
return building.getRooms().stream()
.anyMatch(room -> room.getFurnitures.size() > 10);
}
return false;
}
Answered By - WJS
Answer Checked By - David Marino (JavaFixing Volunteer)