CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > How to Remove Null Values from Java List

Java Tutorial Menu

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor

How to Remove Null Values from Java List

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Published: January 29, 2024 · 3 min read · 0 Comments
Share: X in 🔗

When working with Java lists, it is not uncommon to encounter null values. Null values can cause issues when performing operations on the list, and it is often necessary to remove them to ensure the integrity of the data. In this blog post, we will explore different approaches to remove null values from Java list.

Table of Contents

Using an Iterator
Using Java 8 Stream
Using Apache Commons Collections
Using Java 8 removeIf
Conclusion

Using an Iterator

One way to remove null values from a Java list is by using an iterator. Iterators provide a way to traverse through the elements of a list and perform operations on them.

Following is an example:

List<String> list = new ArrayList<>();
list.add("Apple");
list.add(null);
list.add("Banana");
list.add(null);

Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
    if (iterator.next() == null) {
        iterator.remove();
    }
}
System.out.println(list);

In the above code snippet, we create a list with some null values. We then create an iterator and iterate through the list. If we encounter a null value, we remove it using the iterator’s remove() method.

Output:

[Apple, Banana]

Using Java 8 Stream

Java 8 introduced the Stream API, which provides a powerful way to perform operations on collections. We can leverage the Stream API to remove null values from a Java list.

Following is an example:

List<String> list = new ArrayList<>();
list.add("Apple");
list.add(null);
list.add("Banana");
list.add(null);

list = list.stream()
           .filter(Objects::nonNull)
           .collect(Collectors.toList());
System.out.println(list);

In the above code snippet, we create a list with some null values. We then convert the list to a stream using the stream() method. We apply a filter using the Objects::nonNull method reference to remove the null values. Finally, we collect the filtered stream back into a list using the collect() method.

Output:

[Apple, Banana]

Using Apache Commons Collections

If you are using the Apache Commons Collections library, you can make use of the CollectionUtils class to remove null values from a Java list.

Following is an example:

List<String> list = new ArrayList<>();
list.add("Apple");
list.add(null);
list.add("Banana");
list.add(null);

CollectionUtils.filter(list, Objects::nonNull);
System.out.println(list);

In the above code snippet, we create a list with some null values. We then use the filter() method from the CollectionUtils class to remove the null values from the list.

Output:

[Apple, Banana]

Using Java 8 removeIf

Another approach to remove null values from a Java list is by using the removeIf() method introduced in Java 8.

Following is an example:

List<String> list = new ArrayList<>();
list.add("Apple");
list.add(null);
list.add("Banana");
list.add(null);

list.removeIf(Objects::isNull);
System.out.println(list);

In the above code snippet, we create a list with some null values. We then use the removeIf() method along with the Objects::isNull method reference to remove the null values from the list.

Output:

[Apple, Banana]

Conclusion

Null values in a Java list can cause issues when working with the data. It is important to remove null values to ensure the integrity of the list. In this blog post, we explored different approaches to remove null values from a Java list, including using an iterator, Java 8 Stream API, Apache Commons Collections, and the Java 8 removeIf() method. Choose the approach that best suits your requirements and coding style.

Related Posts:

  • List in Java
  • Arrays in Java
  • What Is Java Swing? A Complete Guide to Java’s GUI Toolkit
  • Top 10 Common Java Errors
  • Vector Class in Java
  • Control Statements in Java
Tags:Apache Commons CollectionsiteratorJava 8 RemoveIfJava 8 StreamJava listsnull values
Was this article helpful?
← Previous ArticleHow to Solve “systemctl command not found” Error: A Step-by-Step Guide
Next Article →MongoDB bulkWrite in Java: A Comprehensive Guide

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
  • Complete Guide to JaCoCo: How to Measure Java Code Coverage Accurately
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap