For example, we have a simple class with some fields. Let's assume, we have fields called 'name' and 'id'. And I want to get name of an object with specified id and return some defaultName, if there is no such object:
private List<MyObject> myObjects;
public String findFieldById(int id) {
MyObject matchingObj = myObjects.stream()
.filter(m -> m.getId() == id)
.findFirst().orElse(null);
return (matchingObj == null) ? "defaultName" : matchingObj.getName();
}
My question is how can I easily make this check on null in a stream in one line?