Java 8 – filter the null values from Stream Example

In this article we are going to see about Java 8 filter the null values from Stream Example

FilterNullValue.java

package com.ehowtonow.java8;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class FilterNullValue {

 public static void main(String[] args) {
     Stream topFootballPlayers = Stream.of("LIONEL MESSI", "CRISTIANO RONALDO", "LUIS SUAREZ", null, "NEYMAR",
       null, "SERGIO AGUERO", "MANUEL NEUER", "GARETH BALE", null, "ARJEN ROBBEN", "ANDRES INIESTA", "ZLATAN IBRAHIMOVIC");

         List result = topFootballPlayers.collect(Collectors.toList());

         System.out.println("Result with null values ");
         result.forEach(System.out::println);

         topFootballPlayers = Stream.of("LIONEL MESSI", "CRISTIANO RONALDO", "LUIS SUAREZ", null, "NEYMAR",
        null, "SERGIO AGUERO", "MANUEL NEUER", "GARETH BALE", null, "ARJEN ROBBEN", "ANDRES INIESTA", "ZLATAN IBRAHIMOVIC");


         System.out.println("Result after null values filtered ");
         List resultAfterNullFilter = topFootballPlayers.filter(x -> x!=null).collect(Collectors.toList());

         resultAfterNullFilter.forEach(System.out::println);
         
         topFootballPlayers = Stream.of("LIONEL MESSI", "CRISTIANO RONALDO", "LUIS SUAREZ", null, "NEYMAR",
        null, "SERGIO AGUERO", "MANUEL NEUER", "GARETH BALE", null, "ARJEN ROBBEN", "ANDRES INIESTA", "ZLATAN IBRAHIMOVIC");

         System.out.println("Result after null values filtered using nonNull");
         List nonNullResult = topFootballPlayers.filter(Objects::nonNull).collect(Collectors.toList());

         nonNullResult.forEach(System.out::println);
 }

}

Output
Result with null values
LIONEL MESSI
CRISTIANO RONALDO
LUIS SUAREZ
null
NEYMAR
null
SERGIO AGUERO
MANUEL NEUER
GARETH BALE
null
ARJEN ROBBEN
ANDRES INIESTA
ZLATAN IBRAHIMOVIC
Result after null values filtered
LIONEL MESSI
CRISTIANO RONALDO
LUIS SUAREZ
NEYMAR
SERGIO AGUERO
MANUEL NEUER
GARETH BALE
ARJEN ROBBEN
ANDRES INIESTA
ZLATAN IBRAHIMOVIC
Result after null values filtered using nonNull
LIONEL MESSI
CRISTIANO RONALDO
LUIS SUAREZ
NEYMAR
SERGIO AGUERO
MANUEL NEUER
GARETH BALE
ARJEN ROBBEN
ANDRES INIESTA
ZLATAN IBRAHIMOVIC


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. Java – LongUnaryOperator Interface
  2. Java – IntStream.Builder Interface
  3. java.util.stream Package
  4. Java – LongPredicate Interface
  5. Java 8 Stream Collectors groupingBy with sort Example
  6. Java – LongStream Interface
  7. Java – ToDoubleFunction Interface
  8. Java – Supplier Interface
  9. java.util.function Package
  10. Java 8 Stream Filter with map Example
  11. Java 8 Convert Array to Stream
  12. Java – LongConsumer Interface
  13. Java – DoubleBinaryOperator Interface
  14. Java – Function Interface
  15. Java – IntSupplier Interface

Be the first to comment

Leave a Reply