Java Streams – Filtering a List


Streams provide a modern and functional approach to manipulating collections.

Source Code

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

public class StreamFilter {
    public static void main(String[] args) {
        List strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
        List filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
        System.out.println(filtered);
    }
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments