Java List Remove Null Value removeNullElement(List source)

Here you can find the source of removeNullElement(List source)

Description

Removes all of the 'null' elements in a list, recursively.

License

Apache License

Parameter

Parameter Description
source a parameter

Return

source - java.util.List

Declaration

public static List removeNullElement(List source) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.List;

public class Main {
    /**/*  w  ww.  ja  v a2 s .c  o  m*/
     * Removes all of the 'null' elements in a list, recursively.
     * 
     * @param source
     * @return source - java.util.List
     */
    public static List removeNullElement(List source) {
        if (!containsNullElement(source))
            return source;
        while (source.contains(null)) {
            source.remove(null);
        }
        return source;

    }

    /**
     * Checks whether 'list' has any null element in it. 1- if list itself is
     * null, return false 2- if list is empty, return false 3- if list contains
     * any null element, return true Note: Return true on first instance of
     * finding null element
     * 
     * @param source
     * @return boolean
     */
    public static boolean containsNullElement(List source) {
        if (source == null)
            return false;
        if (source.isEmpty())
            return false;
        if (source.contains(null))
            return true;
        return false;
    }
}

Related

  1. removeNull(Collection list)
  2. removeNull(final List list)
  3. removeNull(List list)
  4. removeNullableItems(List exceptIds)
  5. removeNullAndDuplicates(List list)
  6. removeNullElements(List list)
  7. removeNullFromList(List list)
  8. removeNulls(Collection list)
  9. removeNulls(final List list)

  10. HOME | Copyright © www.java2s.com 2016