How to filer a List<> to remove empty strings in Java

1 Answer

0 votes
package javaapplication1;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class JavaApplication1 {

    public static void main(String[] args) {

        List<String> s = Arrays.asList("abc", "", "def", "ghi", "", "uvw", "", "xyz");
        List<String> s1 = s.stream().filter(str -> !str.isEmpty()).collect(Collectors.toList()); 

        System.out.println(s1);
    }
}

/*
run:

[abc, def, ghi, uvw, xyz]

*/

 



answered Sep 5, 2016 by avibootz

Related questions

2 answers 318 views
1 answer 116 views
1 answer 187 views
1 answer 177 views
4 answers 377 views
1 answer 191 views
...