Friday 11, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Thursday, 29 June 2017

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

01.package com.javatutorialcorner.java8;
02. 
03.import java.util.List;
04.import java.util.Objects;
05.import java.util.stream.Collectors;
06.import java.util.stream.Stream;
07. 
08.public class FilterNullValue {
09. 
10. public static void main(String[] args) {
11.     Stream<string> topFootballPlayers = Stream.of("LIONEL MESSI", "CRISTIANO RONALDO", "LUIS SUAREZ", null, "NEYMAR",
12.       null, "SERGIO AGUERO", "MANUEL NEUER", "GARETH BALE", null, "ARJEN ROBBEN", "ANDRES INIESTA", "ZLATAN IBRAHIMOVIC");
13. 
14.         List<string> result = topFootballPlayers.collect(Collectors.toList());
15. 
16.         System.out.println("Result with null values ");
17.         result.forEach(System.out::println);
18. 
19.         topFootballPlayers = Stream.of("LIONEL MESSI", "CRISTIANO RONALDO", "LUIS SUAREZ", null, "NEYMAR",
20.        null, "SERGIO AGUERO", "MANUEL NEUER", "GARETH BALE", null, "ARJEN ROBBEN", "ANDRES INIESTA", "ZLATAN IBRAHIMOVIC");
21. 
22. 
23.         System.out.println("Result after null values filtered ");
24.         List<string> resultAfterNullFilter = topFootballPlayers.filter(x -> x!=null).collect(Collectors.toList());
25. 
26.         resultAfterNullFilter.forEach(System.out::println);
27.          
28.         topFootballPlayers = Stream.of("LIONEL MESSI", "CRISTIANO RONALDO", "LUIS SUAREZ", null, "NEYMAR",
29.        null, "SERGIO AGUERO", "MANUEL NEUER", "GARETH BALE", null, "ARJEN ROBBEN", "ANDRES INIESTA", "ZLATAN IBRAHIMOVIC");
30. 
31.         System.out.println("Result after null values filtered using nonNull");
32.         List<string> nonNullResult = topFootballPlayers.filter(Objects::nonNull).collect(Collectors.toList());
33. 
34.         nonNullResult.forEach(System.out::println);
35. }
36. 
37.}
38. 
39.</string></string></string></string>

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


Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Java 8 forEach with List example In this article we are going to see about Java 8 forEach with List example program ForEachList.java package com.javatutorialcorner.java8; import java.util.ArrayList; import java.util.List; public class ForEachList { pub… Read More
  • Java 8 Stream Filter with findAny orElse Example In this article we are going to see about Java 8 Stream filter() , findAny() and orElse() example program. Employee.java package com.javatutorialcorner.java8; public class Employee { private Str… Read More
  • How to configure Maven to use Java 8 In pom.xml, defined this maven.compiler.source properties to configure Maven to use Java 8 to compile the project. Sometimes when you may need to compile a certain project to a different version than what you are currently … Read More
  • Java 8 Stream Filter and Collect Example In this article we are going to see about Java 8 Stream filter() and collect()  example program. StreamFilter.java package com.javatutorialcorner.java8; import java.util.ArrayList; import java.util.List; import java.… Read More
  • Java 8 Stream Filter with multiple conditions Example In this article we are going to see about Java 8 Stream filter() , findAny() and orElse() with multiple conditions  example program. Employee.java package com.javatutorialcorner.java8; public… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Java 8 - filter the null values from Stream Example Rating: 5 Reviewed By: eHowToNow