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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s