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);
        }
    }
}
jagdish khetre
  • 1,381
  • 2
  • 8
  • 15
  • Why is that break there? – cpp beginner Apr 02 '18 at 10:42
  • Possible duplicate of [How to remove all null elements from a ArrayList or String Array?](https://stackoverflow.com/questions/4819635/how-to-remove-all-null-elements-from-a-arraylist-or-string-array) – Ousmane D. Apr 02 '18 at 12:40

3 Answers3

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;
            }   
        }
Rahul
  • 3,479
  • 3
  • 16
  • 28
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);
        }
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
The Cloud Guy
  • 963
  • 1
  • 8
  • 20
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]
Harsh Mehta
  • 571
  • 3
  • 16
  • Or better `setOfString.removeIf(s -> s == null);`, or with a method reference: `setOfString.removeIf(Objects::isNull);` – fps Apr 03 '18 at 21:18