For Removing empty item in list, it can be be done directly. When using method removeIf()
, it will throw UnSupportOperationException
. It caused by collection items cannot be removed.
There has some tricks to remove list item. It can be done by using Java Stream to filter unwanted item out. In the demo it will use List<String>
as example to remove null
and blank item in list.
private List<String> removeEmptyItem(List<String> stringList) { return stringList.stream().filter(o->Objects.nonNull(o) && !o.trim().isBlank()).collect(Collectors.toList()); }
Leave a Reply