-3

I have a List<String> that can contains blanks ("").

Is there a simple way to remove all blanks from list? I used this way to do so.

ListIterator<String> it = values.listIterator();
while (it.hasNext()) {
  if (it.next().equals("")) {
    it.remove();
  }
}

Thanks for help!

Vikrant Kashyap
  • 5,900
  • 3
  • 29
  • 50
Sheldon
  • 266
  • 3
  • 15

3 Answers3

2

Java 8 added an elegant removeIf method:

values.removeIf(String::isEmpty);
Mureinik
  • 277,661
  • 50
  • 283
  • 320
0

I found a solution on how to remove blank items from ArrayList.Without removing index wise

removeAll(Collection<?> c)

That works fine!

Community
  • 1
  • 1
Sheldon
  • 266
  • 3
  • 15
0

There is a method in List<C> Interface named as removeAll(Collections <?> c) which able to remove all elements.

it will remove all your blank String from the list

 values.removeAll(Arrays.asList("")); //remove all blank String
Vikrant Kashyap
  • 5,900
  • 3
  • 29
  • 50