Java Collection Clear cleanup(AnyCollection collection)

Here you can find the source of cleanup(AnyCollection collection)

Description

Cleanups the collection by removing all null entries

License

Open Source License

Parameter

Parameter Description
AnyCollection the type of collection
collection the collection

Return

the same collection for chaining calls

Declaration

public static <AnyCollection extends Collection<?>> AnyCollection cleanup(AnyCollection collection) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Collection;

import java.util.Iterator;

public class Main {
    /**/*  ww  w.j  a v a2  s  .  co m*/
     * Cleanups the collection by removing all null entries
     *
     * @param <AnyCollection> the type of collection
     * @param collection the collection
     * @return the same collection for chaining calls
     */
    public static <AnyCollection extends Collection<?>> AnyCollection cleanup(AnyCollection collection) {
        Iterator<?> iterator = collection.iterator();

        while (iterator.hasNext()) {
            if (iterator.next() == null) {
                iterator.remove();
            }
        }

        return collection;
    }
}

Related

  1. clear(Collection collection)
  2. clear(Collection collection)
  3. clear(Collection collection)
  4. clear(final Collection collection)