I wonder what's teh best approach to check if list is null. In my Stream I call orElseThrow twice. It works but I don;t know if its correct? It looks a little bit ugly:
Optional.ofNullable(listCanBeNull)
.orElseThrow(() -> new ResourceNotFoundException("the same error message"))
.stream()
.filter(configuration -> configuration.getId().equals(warehouseConfigurationId))
.findAny()
.orElseThrow(() -> new ResourceNotFoundException("the same error message"));
I have to throw error when list is null and when no item was found
if (list == null) { throw blah; }at the start of your loop.Listshouldn't benull, it should be empty instead..orElseThrow(() -> new ResourceNotFoundException("the same error message")).stream()use.stream().flatMap(List::stream)which in case of null list (so empty Optional) will create empty stream. So since no value will be found it will be indicated by lastorElseThrow.