2

What will be the equivalent lambda expression for below code?

List<String> List = new ArrayList<String>();
for (String var: alreadyList) {
     try {
         if (getNumber(var) == num) {
             filteredList.add(var);
         }
     } catch(NullPointerException exception) {
           throw exception;
     }
 }
  • getNumber method throws Nullpointerexception and the above code is also in a method which throws the same exception to a caller.

This is usual lambda expression but how to throw the Nullpointerexception in it?

List<String> List = alreadyList.stream()
            .filter(var -> getNumber(var) == num)
            .collect(Collectors.toList());
2
  • 3
    The original code is strange. As you can see, it just re-throws the exception. In other words, it doesn't really do anything. Commented Aug 6, 2018 at 13:44
  • 2
    NullPointerException is an unchecked exception and so you don't need to catch / throw it explicitly at all (and arguably shouldn't.) So the short answer is the stream based code you have is already the equivalent of what's above, the code above just has some needless exception catching in it. Commented Aug 6, 2018 at 13:48

3 Answers 3

3

Catching a NPE is bad practice. NPE is an unchecked exception. From the docs:

If an argument is null, the method might throw a NullPointerException, which is an unchecked exception.

Instead why not add another filter to the stream:

List<String> List = alreadyList.stream()
        .filter(e -> e!= null)
        .filter(var -> getNumber(var) == num)
        .collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

1 Comment

Agreed that Catching an NPE is bad practice.
2

It is bad practice to catch NullPointerException. You can use Optional class to avoid it and do necessary action only if object is present. You can throw other exceptions from your getNumber() method without any problems

Comments

1

Never catch the unchecked exceptions including the NullPointerException. The way you do is just rethrowing it. Either filter out the null values out.

List<String> List = alreadyList.stream()
                               .filter(Objects::nonNull)
                               .filter(var -> getNumber(var) == num)
                               .collect(Collectors.toList());

The code you have provided:

List<String> List = alreadyList.stream()
                               .filter(var -> getNumber(var) == num)
                               .collect(Collectors.toList());

Will already throw the NullPointerException in case getNumber(var) would fail. Your try-catch does not do anything different.

2 Comments

Is there a difference between Objects::nonNull and (c -> c != null) ?
@GBlodgett: Nope in the behavior. Objects::nonNull is just a static wrapper used as a shortcut and a reference which is generally accepted standard of filtering null values out. See its implementation since the line 264. Moreover in c -> c != null you make easier an mistake forgetting the !.

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.