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

5

6

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.

Ardys answered 17/6, 2016 at 9:32 Comment(5)
Why there are null values there in the first place? I think the main problem is in the code that populate the iterator. Can't you control that ?Supervene
Does the iterator give you the first film in the list?Barbados
in the Filmi next() method it test if the film is 3d and hasNext() if it dosent have any of those it returns a null .thats when the null comes from, but i have to return something .Ardys
@keiwan yes it doesArdys
Every time you call it.next() you get the next value. If you want to use this value more than once you have to use a variable and look at this variable.Tremayne
D
12

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?

Disforest answered 17/6, 2016 at 9:40 Comment(0)
P
8

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) {
   ...
}
Panayiotis answered 17/6, 2016 at 9:40 Comment(0)
L
6

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)
Lemmueu answered 17/6, 2016 at 9:40 Comment(7)
That's only a solution if OP is using Java 8, isn't it?Deaton
Not sure that's necessary a bad thing; Java 8 solutions ought to be considered acceptable on the Java tag. Have an upvote!Disforest
I am not arguing that. Just found it worth mentioning because especially beginners may not be aware of that this functionality is Java 8+ only.Deaton
yes, but if you don't use Java 8, you can use similar stuff with the lib guava and the class Collections2, method filterLemmueu
But where do you draw the line? Generics are Java 5 onwards, for example.Disforest
@Bathsheba: I understand that if you still are on Java 7, you can forgot to write-it on your post. But if you'r java 4, he will have precise it on his post, I hope.Lemmueu
Might be worth mentioning Objects::nonNull could be used instead of film->film!=nullRecapture
F
1

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 ){
     ....
  }
}
Fagot answered 17/6, 2016 at 10:14 Comment(0)
A
1

Additionally to what others said: in case you are doing a for-each loop with a primitive type like int

for (int node : input) {
    doSomething(node);
}

you might want to consider using the Wrapper class instead:

for (Integer node : input) {
    if (node == null) throw new IllegalArgumentException();
    doSomething(node);
}
Athalee answered 8/1, 2021 at 18:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.