-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
  • 6,398
  • 3
  • 32
  • 52
Sheldon
  • 276
  • 3
  • 17

3 Answers3

2

Java 8 added an elegant removeIf method:

values.removeIf(String::isEmpty);
Mureinik
  • 297,002
  • 52
  • 306
  • 350
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
  • 276
  • 3
  • 17
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
  • 6,398
  • 3
  • 32
  • 52