Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if Iterator.next() is == null?

How do I check if the next element in the list is null ?

while(it.hasNext()){

            System.out.println(it.next()+"\n");
        }

this is how I tried, but when the last element is null it prints it as null.

I tried to change it

 while(it.hasNext()){
    if(it.next()==null){
    }else             
        System.out.println(it.next()+"\n");
            }

but this just makes it worst because some of the elements don't even print!

This is my Iteration method/anonymous class

public Iterator<Filmi> iterator3DFilms ()throws FilmiException{
        if(filmList.isEmpty())
            throw new FilmiException("No Films on the list");
        return new Iterator<Filmi>(){
            private int index=0;
            public boolean hasNext(){
                return index <filmList.size();
            }
            public Filmi next(){
                Filmi lb = filmList.get(index++);

                if(lb.is3D()== true)
                    return lb;
                if(hasNext())
                    return next();
                return null;
            }
            public void remove(){}
        };
    }

The null print only happens at the last element Thank you.

like image 651
Lazlow Avatar asked Jun 17 '16 09:06

Lazlow


People also ask

How do I know if my iterator is empty?

l = [_ for _ in iterator] # If the list is not empty then the iterator had elements; else it was empty. if l : pass # Use the elements of the list (i.e. from the iterator) else : pass # Iterator was empty, thus list is empty.

Can Java iterator be null?

Overall: Java API has no mean to say if the iterator value may be null or not, unless by stating it explicitly in the documentation. In this case, nothing is stated, however good sense allows to think that a null iterator value will never be returned from Java API methods.

What does iterator next () do?

The next() method of ListIterator interface is used to return the next element in the given list. This method is used to iterate through the list.

What is the return type of next () in iterator?

Object next(): It returns the next element in the collection until the hasNext()method return true. This method throws 'NoSuchElementException' if there is no next element.


4 Answers

Naturally, code like

if (it.next() == null){
} else {
    System.out.println(it.next()+"\n");
}

will consume every other non-null element, as you are observing. Plus calling it.next() without checking it.hasNext() is a recipe for disaster.

Why not write

Foo/*ToDo - use the correct type here*/ foo = it.next()
if (foo != null){
    /*ToDo*/
}

instead?

like image 200
Bathsheba Avatar answered Oct 01 '22 11:10

Bathsheba


No it cannot work this way because if it.next() is not null you call it.next() twice which will make you skip a value that could not even be available.

Use a variable instead as next:

Object o = it.next();
if (o != null) {
   ...
}
like image 22
Nicolas Filotto Avatar answered Oct 01 '22 12:10

Nicolas Filotto


you should use stream instead of iterator.

filmList.stream().filter(film->film!=null).filter(film->film.is3D())

Edit: or, if you'r not in Java 8 :

Predicate<Film> isNotNullAnd3D = new Predicate<Person>() {
        public boolean apply(Film f) {
            return f != null && f.is3D();
        }
    };

Collection2.filter(filmList, isNotNullAnd3D)
like image 31
sab Avatar answered Oct 01 '22 13:10

sab


You never mentioned why you use iterators explicitly in the first place. Why not use implicit iterator notation like this ? :

for (Film film : filmList) {
  if (film != null ){
     ....
  }
}
like image 26
Ivan Matavulj Avatar answered Oct 01 '22 13:10

Ivan Matavulj