2

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

6
  • 11
    The Java stream interface is convenient, but it doesn't have to consume your entire program. Sometimes the most readable thing is a good old-fashioned if (list == null) { throw blah; } at the start of your loop. Commented Aug 18, 2022 at 15:34
  • Just use CollectionUtils.isEmpty and MapUtils.isEmpty() Commented Aug 18, 2022 at 15:37
  • 1
    And for starters, a List shouldn't be null, it should be empty instead. Commented Aug 18, 2022 at 15:40
  • 1
    OK, thanks maybe I tried to use Streams by force :) Commented Aug 18, 2022 at 15:41
  • 1
    BTW you can remove first exception throwing. Instead of .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 last orElseThrow. Commented Aug 18, 2022 at 15:42

3 Answers 3

7

An Optional::ofNullable can be turned into a (possibly empty) Stream, or you can immediately use Stream::ofNullable. And do a flatMap.

Stream.ofNullable(listCanBeNull)
    .flatMap(List::stream)
    .filter(configuration -> configuration.getId().equals(warehouseConfigurationId))
    .findAny()
    .orElseThrow(() -> new ResourceNotFoundException("the same error message"));
Sign up to request clarification or add additional context in comments.

2 Comments

It can be also like this: Stream.ofNullable(listCanBeNull) .flatMap(Collection::stream) .filter...
Streams seems like talking in a foreign language - with mistakes. Correct it.
6

Just check if the list is null directly. Streaming elements from a list only makes sense if the list exists in the first place.

if(null != listCanBeNull) {
    // stream things from here
}

2 Comments

Or, if ( Objects.nonNull( list ) ) { … }
simple; expressing almost¹ exactly what I want done; no additional objects needed/being created; no need to know the function of some additional method (¹ - I personally do not like the Yoda Conditions)
2

tl;dr

Objects.requireNonNull( list ).stream() …  // Throws NullPointerException if null.

Details

If you choose to go with a simple null check before streaming, here is an alternative to the null check shown in correct Answer by Makoto.

Objects.nonNull

I find a call to Objects.nonNull to be more readable than null != list.

if ( Objects.nonNull( list ) ) { list.stream() … }

Objects.requireNonNull

Or automatically throw an exception if the object reference should never be null, with a call to Objects.requireNonNull.

 Objects.requireNonNull( list )  // Throws NullPointerException if null.

This method returns the object you pass (your list), providing for convenient method-chaining.

Objects.requireNonNull( list ).stream() … 

You can optionally pass a message to be used in the exception if thrown.

Objects.requireNonNull( list , "message goes here" ).stream() … 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.