Java 8 code KATAs VI – Streams Example

In this series of posts, I will be posting simple java 8 code katas.

Part I – Interface with default methods
Part II – Interface with static methods
Part III – Functional Interfaces & Lambda Expressions
Part IV – Method References
Part V – java.util.function package
Part VI – Streams

This post would be about streams.

See the example below:

StreamsExample.java


package com.idodevjobs;

import java.util.Arrays;
import java.util.List;

public class StreamsExample {
    public static void main(String[] args) {
        List<Task> taskList = Arrays.asList(
                                        new Task("Task A", Task.Priority.NEW),
                                        new Task("Task B", Task.Priority.COMPLETE),
                                        new Task("Task C", Task.Priority.PROGRESS));
        // example 1
        System.out.println(taskList.stream().filter((Task t) -> (t.getPriority().equals(Task.Priority.COMPLETE))).count());

        // example 2
        taskList.stream().map((t) -> (t.getName())).forEach((s) -> {System.out.println(s);});

        // example 3
        System.out.println(Arrays.deepToString(taskList.stream().map(t -> t.getName() + " - " + t.getPriority()).toArray(l -> new String[l])));
    }
}

class Task {
    private String name;
    private Priority priority;

    public Task() {}

    public Task(String name, Priority priority) {
        this.name = name;
        this.priority = priority;
    }

    public String getName() {
        return name;
    }

    public Priority getPriority() {
        return priority;
    }

    enum Priority {
        NEW, PROGRESS, COMPLETE
    }
}


Java 8 streams provide a better way to manipulate collections. It provides methods to perform filtering, mapping, sorting and a lot more.

Most of the stream operations return another stream and hence the operations can be chained.

In example 1,

taskList.stream().filter((Task t) -&gt; (t.getPriority().equals(Task.Priority.COMPLETE))).count()

the list is converted to a stream, filtered based on a criteria which return another stream containing elements that satisfy the criteria and finally finding the count.

In example 2,

taskList.stream().map((t) -&gt; (t.getName())).forEach((s) -&gt; {System.out.println(s);})

the list is converted to a stream, converted to another stream containing only the name and finally doing a terminal operation using foreach to print the contents of the new stream.

In example 3,

taskList.stream().map(t -&gt; t.getName() + &quot; - &quot; + t.getPriority()).toArray(l -&gt; new String[l])

the list is converted to a stream, converted to another stream containing the name and priority and finally doing a terminal operation using toArray to convert the new stream to an array.

Advertisements
This entry was posted in java and tagged . Bookmark the permalink.

5 Responses to Java 8 code KATAs VI – Streams Example

  1. Pingback: Java 8 code KATAs V – java.util.function Example | iDoDevJobs

  2. Pingback: Java 8 code KATAs IV – Method References Example | iDoDevJobs

  3. Pingback: Java 8 code KATAs III – FunctionalInterface & Lambda Expressions Example | iDoDevJobs

  4. Pingback: Java 8 code KATAs II – Interface with static methods Example | iDoDevJobs

  5. Pingback: Java 8 code KATAs I – Interface with default methods Example | iDoDevJobs

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