240

I have a list of objects say car. I want to filter this list based on some parameter using Java 8. But if the parameter is null, it throws NullPointerException. How to filter out null values?

Current code is as follows

requiredCars = cars.stream().filter(c -> c.getName().startsWith("M"));

This throws NullPointerException if getName() returns null.

3
  • Do you wanna “filter values only if not null” or “filter out null values”? That sounds contradicting to me.
    – Holger
    Oct 1, 2015 at 10:18
  • 3
    Could I suggest that you accept Tunaki's answer as it appears to be the only one which actually answers your question.
    – Mark Booth
    May 4, 2017 at 10:14
  • What about kotlin? )) requiredCars = cars.filter {c -> c?.name?.startsWith("M"))}; Dec 17, 2021 at 21:31

6 Answers 6

488
+200

In this particular example, I think @Tagir is 100% correct get it into one filter and do the two checks. I wouldn't use Optional.ofNullable the Optional stuff is really for return types not to be doing logic... but really neither here nor there.

I wanted to point out that java.util.Objects has a nice method for this in a broad case, so you can do this:

    cars.stream()
        .filter(Objects::nonNull)

Which will clear out your null objects. For anyone not familiar, that's the short-hand for the following:

    cars.stream()
        .filter(car -> Objects.nonNull(car))

To partially answer the question at hand to return the list of car names that starts with "M":

    cars.stream()
        .filter(car -> Objects.nonNull(car))
        .map(car -> car.getName())
        .filter(carName -> Objects.nonNull(carName))
        .filter(carName -> carName.startsWith("M"))
        .collect(Collectors.toList());

Once you get used to the shorthand lambdas you could also do this:

    cars.stream()
        .filter(Objects::nonNull)
        .map(Car::getName)        // Assume the class name for car is Car
        .filter(Objects::nonNull)
        .filter(carName -> carName.startsWith("M"))
        .collect(Collectors.toList());

Unfortunately once you .map(Car::getName) you'll only be returning the list of names, not the cars. So less beautiful but fully answers the question:

    cars.stream()
        .filter(car -> Objects.nonNull(car))
        .filter(car -> Objects.nonNull(car.getName()))
        .filter(car -> car.getName().startsWith("M"))
        .collect(Collectors.toList());
7
  • 1
    note that the null car isn't the issue. In this case, it's name property causing problems. So Objects::nonNull can't be used here, and in last advice it should be cars.stream() .filter(car -> Objects.nonNull(car.getName())) I believe
    – kiedysktos
    Jan 9, 2017 at 11:38
  • 1
    BTW, I think cars.stream() .filter(car -> Objects.nonNull(car.getName()) && car.getName().startsWith("M")) would be the summary of your advice in this question context
    – kiedysktos
    Jan 9, 2017 at 11:39
  • 4
    @kiedysktos That's a good point that calling .startWith could also cause a null pointer. The point I was trying to make is that Java supplies a method specifically for filtering out null objects from your streams.
    – xbakesx
    Jan 10, 2017 at 3:43
  • @Mark Booth yes, obviously Objects.nonNull is equivalent to != null, your option is shorter
    – kiedysktos
    May 4, 2017 at 10:30
  • 1
    Aren't you creating a list of car names (String) instead cars (Car)? Oct 4, 2018 at 10:12
85

You just need to filter the cars that have a null name:

requiredCars = cars.stream()
                   .filter(c -> c.getName() != null)
                   .filter(c -> c.getName().startsWith("M"));
4
  • 5
    It's a real shame that this answer isn't more highly voted as it appears to be the only answer that actually answers the question.
    – Mark Booth
    May 4, 2017 at 10:13
  • 2
    @MarkBooth The question "How to filter out null values?" seems to be answered well by xbakesx. Jun 1, 2017 at 15:42
  • @MarkBooth Looking at the dates you are correct. My mistake. Jun 1, 2017 at 16:28
  • Performance wise is it good to filter the stream twice Or better use the predicate for filtering? Just want to know. Mar 27, 2019 at 5:43
67

The proposed answers are great. Just would like to suggest an improvement to handle the case of null list using Optional.ofNullable, new feature in Java 8:

List<String> carsFiltered = Optional.ofNullable(cars)
                    .orElseGet(Collections::emptyList)
                    .stream()
                    .filter(Objects::nonNull)
                    .collect(Collectors.toList());

So, the full answer will be:

List<String> carsFiltered = Optional.ofNullable(cars)
                    .orElseGet(Collections::emptyList)
                    .stream()
                    .filter(Objects::nonNull) //filtering car object that are null
                    .map(Car::getName) //now it's a stream of Strings
                    .filter(Objects::nonNull) //filtering null in Strings
                    .filter(name -> name.startsWith("M"))
                    .collect(Collectors.toList()); //back to List of Strings
4
  • 7
    Bad use of Optional. null should never be used as a synonym for an empty Collection in the first place.
    – VGR
    Dec 12, 2016 at 22:20
  • 7
    @VGR Of course, but that is not what happens in practice. Sometimes (most of the time) you need to work with code that a lot of people worked on. Sometime you receive your data from external interfaces. For all those cases, Optional is a great use.
    – Johnny
    Dec 13, 2016 at 8:44
  • 3
    note that the null car isn't the issue. In this case, it's name property causing problems. So Objects::nonNull doesn't solve the problem since non-null car can have name==null
    – kiedysktos
    Jan 9, 2017 at 11:41
  • 1
    Of course @kiedysktos, but that's not what I wanted to show in the answer. But, I'm accepting what you saying and editing the answer :)
    – Johnny
    Sep 7, 2017 at 10:36
31

You can do this in single filter step:

requiredCars = cars.stream().filter(c -> c.getName() != null && c.getName().startsWith("M"));

If you don't want to call getName() several times (for example, it's expensive call), you can do this:

requiredCars = cars.stream().filter(c -> {
    String name = c.getName();
    return name != null && name.startsWith("M");
});

Or in more sophisticated way:

requiredCars = cars.stream().filter(c -> 
    Optional.ofNullable(c.getName()).filter(name -> name.startsWith("M")).isPresent());
1
  • The inline expansion in the second example was valuable for my use case
    – Paul
    Mar 5, 2020 at 16:56
4

Leveraging the power of java.util.Optional#map():

List<Car> requiredCars = cars.stream()
  .filter (car -> 
    Optional.ofNullable(car)
      .map(Car::getName)
      .map(name -> name.startsWith("M"))
      .orElse(false) // what to do if either car or getName() yields null? false will filter out the element
    )
  .collect(Collectors.toList())
;
3

you can use this

List<Car> requiredCars = cars.stream()
    .filter (t->  t!= null && StringUtils.startsWith(t.getName(),"M"))
    .collect(Collectors.toList());

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.