- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to filter non-null value in Java?
Let’s say the following is our List with string elements:
List<String> leagues = Arrays.asList("BBL", "IPL", "MLB", "FPL","NBA", "NFL");
Now, create a stream and filter elements that end with a specific letter:
Stream<String> stream = leagues.stream().filter(leagueName -> leagueName.endsWith("L"));
Now, use Objects::nonnull for non-null values:
List<String> list = stream.filter(Objects::nonNull).collect(Collectors.toList());
The following is an example to filter non-null value in Java:
Example
import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; import java.util.stream.Stream; public class Demo { public static void main(String[] args) { List<String> leagues = Arrays.asList("BBL", "IPL", "MLB", "FPL","NBA", "NFL"); Stream<String> stream = leagues.stream().filter(leagueName -> leagueName.endsWith("L")); List<String> list = stream.filter(Objects::nonNull).collect(Collectors.toList()); System.out.println("League names ending with L = "+list); } }
Output
League names ending with L = [BBL, IPL, FPL, NFL]
- Related Articles
- How to filter String list by starting value in Java?
- How to use null value as key in Java HashMap
- Fetch maximum value from multiple columns with null and non-null values?
- Filter null from an array in JavaScript?
- Looping in JavaScript to count non-null and non-empty values
- How to filter an array in Java
- How does COALESCE order results with NULL and NON-NULL values?
- How to get the first non-null/undefined argument in JavaScript?
- How to set default value to NULL in MySQL?
- JavaScript: How to filter out Non-Unique Values from an Array?
- Filter away object in array with null values JavaScript
- remove null value from a String array in Java
- Replace null values with default value in Java Map
- Java application to insert null value into a MySQL database?
- Which MySQL function is used to find first non-NULL value from a list of values?

Advertisements