In Java, remove empty elements from a list of Strings

前端 未结 11 2393
余生分开走
余生分开走 2020-12-01 06:05

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

11条回答
  •  失恋的感觉
    2020-12-01 06:40

    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

提交回复
热议问题