remove Null from Iterator - Java java.util

Java examples for java.util:Iterator

Description

remove Null from Iterator

Demo Code


//package com.book2s;
import java.util.ArrayList;

import java.util.Iterator;
import java.util.List;

public class Main {
    public static void main(String[] argv) {
        Iterator iterator = java.util.Arrays.asList("asdf", "book2s.com")
                .iterator();//from   w ww .  j  a v  a2 s. co  m
        removeNull(iterator);
    }

    public static void removeNull(final Iterator<?> iterator) {
        while (iterator.hasNext()) {
            if (iterator.next() == null) {
                iterator.remove();
            }
        }
    }

    public static <E> List<E> asList(
            @SuppressWarnings("unchecked") final E... array) {
        List<E> list = new ArrayList<>(array.length);
        for (E element : array) {
            list.add(element);
        }
        return list;
    }

    public static <E> List<E> asList(final Iterator<? extends E> iterator) {
        List<E> list = new ArrayList<>();
        while (iterator.hasNext()) {
            list.add(iterator.next());
        }
        return list;
    }
}

Related Tutorials