Stream filter and slice

In this example we will use a base object Post that could represent a trim down version of a blog post. We will show ways to filter with a predicate, filter unique elements, filter and truncate or limit and skip the first and ignore the first few elements of a stream.


class Post {

    String description;
    String tags;
    int wordlength;

    public Post(String description, String tags, int wordlength) {
        this.description = description;
        this.tags = tags;
        this.wordlength = wordlength;

    public String toString() {

                .add("description", description).add("tags", tags)
                .add("tags", wordlength).toString();

    public String getDescription() {
        return description;

    public String getTags() {
        return tags;

    public int getWordlength() {
        return wordlength;

List<Post> posts;

public void setUp() {

    posts = new ArrayList<>();

    posts.add(new Post("Java 8 is jammed packed with some cool",
            "java 8, eclipse", 459));
    posts.add(new Post("The Date-Time package introduced in the Java SE 8",
            "java 8, date time api", 750));
    posts.add(new Post("Prefer empty collection to null is a basic",
            "clean code, java", 245));
    posts.add(new Post("Screencast #1: Intro to filtering collections",
            "java, java tutorial, guava", 1234));
    posts.add(new Post("Screencast #2: Intro to transforming objects",
            "java, java tutorial", 9483));

Filter with predicate

The Stream interface supports a filter method that takes a predicate as an argument. Below we will filter any Post that has a word length of less than 500 then converting the stream to a list.

public void filter_with_predicate() {

    List<Post> postWithLessThan500 =
            .filter(p -> p.wordlength < 500).collect(Collectors.toList());

    assertEquals(2, postWithLessThan500.size());

Filter unique elements

Streams interface also supports a distinct method. Similar to a select distinct column from table it returns a stream with unique elements according to the implmementation of the equals method. This example we will find unique list of tags. Since tags is a string seperated by a comma, we first need to split, trim and convert all elements to lower case. Finally we will call distinct and convert the stream to a list.

public void filter_unique_elements() {

    List<String> tags = posts
                    tag ->",")).map(String::trim)

            contains("java 8", "eclipse", "date time api", "clean code",
                    "java", "java tutorial", "guava"));


Filter and truncate

Another method that stream supports is limit which returns a stream consisting of the elements of this stream, truncated to be no longer than the parameter maxSize in length. In our example we may only want to display the first two posts to a user possibly on a main landing page or as a side nav element.

public void filter_and_truncate() {

    List<Post> firstTwoPosts =

    assertEquals(2, firstTwoPosts.size());


Post{description=Java 8 is jammed packed with some cool, tags=java
8, eclipse, tags=459},
Post{description=The Date-Time package
introduced in the Java SE 8, tags=java 8, date time api, tags=750}

Skip elements

Limit and skip complement each other. Limit truncates a stream, skip will return a stream consisting of the remaining elements after the first n elements provided as a parameter. In the instance where the stream has fewer elements than provide n, an empty stream will be returned. Using posts we will skip the first 4 elements and return the 5th element in the stream.

public void skipping_elements() {

    List<Post> firstTwoPosts =

    assertEquals(1, firstTwoPosts.size());


[Post{description=Screencast #2: Intro to transforming objects, tags=java, java tutorial, tags=9483}]