I have a stream of object Person. I want to find people with height>6 and if there exists no one that matches that criteria, then I wanna find people with weight>100. I'm achieving it the following way but wondering if I can get it done in a better way.
Optional<Person> personWithHeightGreaterThan6 = persons.stream()
.filter(person -> person.getHeight()>6)
.findFirst();
if (personWithHeightGreaterThan6.isPresent()) {
result = personWithHeightGreaterThan6.get();
} else {
Optional<Person> personWithWeightGreaterThan100 = persons.stream()
.filter(person -> person.getWeight()>100)
.findFirst();
if (personWithWeightGreaterThan100.isPresent()) {
result = personWithWeightGreaterThan100.get();
}
}