0

I am new for Java . I was just looking for the Iterator in Collection Framework. but now I want to remove all null from ArrayList by using Iterator's remove() method .so tried following code but I am not getting expected output as I want a list without any null values . please explain someone Thank you.

here is my code

package setinterface;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class InternalWorkingOfArrayList {

    public static void main(String[] args) {

        List<String> setOfString = new ArrayList<String>();
        setOfString.add("A");
        setOfString.add("B");
        setOfString.add("C");
        setOfString.add("B");
        setOfString.add(null);
        setOfString.add("A");
        setOfString.add(null);


        Iterator<String> iterator = setOfString.iterator();
        while (iterator.hasNext()) {
            String string = (String) iterator.next();
            if (null==string) {
                iterator.remove();
                break;
            }   
        }
        for (String string1 : setOfString) {
            System.out.println(string1);
        }
    }
}
2

3 Answers 3

5

Why are you breaking the loop?? Remove break statement from your code.

while (iterator.hasNext()) {
            String string = (String) iterator.next();
            if (null==string) {
                iterator.remove();
                //break;
            }   
        }
Sign up to request clarification or add additional context in comments.

1 Comment

No need to cast, either
2

Using Java-8 features, you can achieve it with a single line of code in following way...

setOfString.removeAll(Collections.singleton(null));

And then print the output:

System.out.println(setOfString); //output would be: [A, B, C, B, A]

1 Comment

Or better setOfString.removeIf(s -> s == null);, or with a method reference: setOfString.removeIf(Objects::isNull);
2

What you are doing wrong here is that you are directly trying to cast your element into string. This would give you an exception for a null element.

This is what you could do precisely.

package setinterface;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class InternalWorkingOfArrayList {

    public static void main(String[] args) {

        List<String> setOfString = new ArrayList<String>();
        setOfString.add("A");
        setOfString.add("B");
        setOfString.add("C");
        setOfString.add("B");
        setOfString.add(null);
        setOfString.add("A");
        setOfString.add(null);


        Iterator<String> iterator = setOfString.iterator();
        while (iterator.hasNext()) {
            if (null==iterator.next()) {
                iterator.remove();
            }   
        }
        for (String string1 : setOfString) {
            System.out.println(string1);
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.