In Java, I have an ArrayList of Strings like:
[,Hi, ,How,are,you]
I want to remove the null and empty elements, how to change it so it is l
If you are using Java 8 then try this using lambda expression and org.apache.commons.lang.StringUtils, that will also clear null
and blank
values from array
input
public static String[] cleanArray(String[] array) {
return Arrays.stream(array).filter(x -> !StringUtils.isBlank(x)).toArray(String[]::new);
}
ref - https://stackoverflow.com/a/41935895/9696526
Regarding the comment of Andrew Mairose - Although a fine solution, I would just like to add that this solution will not work on fixed size lists.
You could attempt doing like so:
Arrays.asList(new String[]{"a", "b", null, "c", " "})
.removeIf(item -> item == null || "".equals(item));
But you'll encounter an UnsupportedOperationException at java.util.AbstractList.remove
(since asList
returns a non-resizable List).
A different solution might be this:
List<String> collect =
Stream.of(new String[]{"a", "b", "c", null, ""})
.filter(item -> item != null && !"".equals(item))
.collect(Collectors.toList());
Which will produce a nice list of strings :-)
Going to drop this lil nugget in here:
Stream.of("", "Hi", null, "How", "are", "you")
.filter(t -> !Strings.isNullOrEmpty(t))
.collect(ImmutableList.toImmutableList());
I wish with all of my heart that Java had a filterNot
.
There are a few approaches that you could use:
Iterate over the list, calling Iterator.remove()
for the list elements you want to remove. This is the simplest.
Repeatedly call List.remove(Object)
. This is simple too, but performs worst of all ... because you repeatedly scan the entire list. (However, this might be an option for a mutable list whose iterator didn't support remove
... for some reason.)
Create a new list, iterate over the old list, adding elements that you want to retain to a new list.
If you can't return the new list, as 3. above and then clear the old list and use addAll
to add the elements of the new list back to it.
Which of these is fastest depends on the class of the original list, its size, and the number of elements that need to be removed. Here are some of the factors:
For an ArrayList
, each individual remove
operation is O(N)
, where N
is the list size. It is expensive to remove multiple elements from a large ArrayList using the Iterator.remove()
method (or the ArrayList.remove(element)
method).
By contrast, the Iterator.remove
method for a LinkedList
is O(1)
.
For an ArrayList
, creating and copying a list is O(N)
and relatively cheap, especially if you can ensure that the destination list's capacity is large enough (but not too large).
By contrast, creating and copying to a LinkedList
is also O(N)
, but considerably more expensive.
All of this adds up to a fairly complicated decision tree. If the lists are small (say 10 or less elements) you can probably get away with any of the approaches above. If the lists could be large, you need to weigh up all of the issues in the list of the expected list size and expected number of removals. (Otherwise you might end up with quadratic performance.)
Another way to do this now that we have Java 8 lambda expressions.
arrayList.removeIf(item -> item == null || "".equals(item));