remove Null Element from Collection - Java java.util

Java examples for java.util:Collection Null Element

Description

remove Null Element from Collection

Demo Code


//package com.java2s;
import java.util.Collection;

import java.util.Iterator;

public class Main {
    public static void main(String[] argv) {
        Collection collection = java.util.Arrays.asList("asdf",
                "java2s.com");
        removeNullElement(collection);/*from w  ww. j a  v a  2  s .  c  om*/
    }

    public static <E> void removeNullElement(Collection<E> collection) {
        Iterator<E> it = collection.iterator();
        if (it.hasNext()) {
            E item = it.next();
            if (item == null) {
                it.remove();
            }
        }
    }
}

Related Tutorials