Java8 : How to filter data from a collection in Java 8 ? #Stream #Filters

Stream has been added in the new API ‘s Java 8 to help you work with collections. Here’s a simple example of how to filter data in a list .

List<Animals> birds = allAnimals.stream().filter(a -> a.isBird()).collect(toList());
public class Animals {
    { ... }
    private boolean bird;

    public boolean isBird() {
        return bird;
    }

    public void setBird(boolean bird) {
        this.bird = bird;
    }

    {...}
}

Leave a comment