clean Nulls from Collection - Java java.util

Java examples for java.util:Collection Operation

Description

clean Nulls from Collection

Demo Code

// This program is free software: you can redistribute it and/or modify
//package com.java2s;

import java.util.Collection;
import java.util.Iterator;

public class Main {
    public static void main(String[] argv) {
        Collection c = java.util.Arrays.asList("asdf", "java2s.com");
        cleanNulls(c);/*from  www . jav  a 2 s  . co  m*/
    }

    public static <T> void cleanNulls(Collection<T> c) {
        Iterator<T> it = c.iterator();
        try {
            while (it.hasNext())
                if (it.next() == null)
                    c.remove(null);
        } catch (NullPointerException e) {
            // Does not permit nulls, it should be ok
        }
    }
}

Related Tutorials