Java 8: Streams API

Spread the love

Streams in Java 8 represents a sequence of elements. It supports sequential as well as parallel operations.

The most important thing to know about streams is that it is not a separate Data Structure. It doesn’t store data. It just converts the input data whether it is a List or anything else into a sequence of streams.

They work with Functional Interfaces and Lambda Expressions which makes them so attractive to use.

So how does a stream look like?

import java.util.*;
 
public class MyClass {
 
    public static void main(String[] args) {
        List<String> names = Arrays.asList("David", "John", "Nathan", "Kevin");
 
        names.stream()
                   .map((n) -> n.toUpperCase())
                   .forEach(System.out::println);
    }
}

So here :>

  • We had a list of names initially which can be considered as an Input.
  • Then we convert it into a stream.
  • Then we map and convert it into UpperCase.
  • Finally using the ForEach we loop over the elements and display the names using Sys Out.

Here we had an intermediate operation which was converted to stream and a terminal operation which was forEach.

We can create an empty stream as well. It is done so that we can return a stream with 0 elements instead of null.
Stream s = Stream.empty()

This is how we create an empty stream.

Also we can get a stream eve without using a collection. Like in the above example which is took to display names in UPPER CASE, i took a collection and converted that into a Stream. But we can directly get a Stream as well without using a collection.

We use the Stream.of method here.

Stream streamArray =Stream.of("X","Y","Z");

Parallel Streams:>

You can create Parallel Stream using .parallel() method on Stream object in Java. Parallel Stream is a feature in Java 8 in which multiple cores of the processor will be used. Benefit is efficient processing and execution of data.

public class MyClass {
 
    public static void main(String[] args) {
        int[] array= {1,2,3,4,5};
 
        System.out.println("Use of Parallel Stream");
        IntStream intParallelStream=Arrays.stream(array).parallel();
        intParallelStream.forEach((s)->
        {
            System.out.println(s+" "+Thread.currentThread().getName());
        }
        );
    }
}

So here those 5 elements are getting ran in 5 cores by 5 threads. So accordingly you can see the output.

Parallel streams are used when performance is critical. Else a single serial stream is sufficient.

Hope the article was clear. Thanks for reading.

7 Replies to “Java 8: Streams API”

  1. I would like to express some thanks to this writer for bailing me out of such a trouble. After researching through the world-wide-web and seeing strategies that were not powerful, I figured my entire life was over. Existing without the approaches to the difficulties you have fixed by means of the post is a critical case, as well as those that could have in a negative way damaged my entire career if I had not discovered your web page. Your personal training and kindness in playing with a lot of stuff was very useful. I’m not sure what I would’ve done if I hadn’t discovered such a thing like this. I can also at this moment look forward to my future. Thanks so much for the reliable and results-oriented help. I won’t hesitate to propose the blog to anyone who wants and needs direction about this problem.

  2. Hello I am so delighted I found your webpage, I really found you by mistake, while I was researching on Google for something else, Anyhow I am here now and would just like to say thanks a lot for a marvelous post and a all round exciting blog (I also love the theme/design), I don抰 have time to read it all at the moment but I have bookmarked it and also included your RSS feeds, so when I have time I will be back to read much more, Please do keep up the awesome work.

  3. I intended to draft you one very small remark just to say thanks a lot once again for these pleasant tips you have shown at this time. This is quite unbelievably open-handed with people like you in giving unhampered exactly what most of us could possibly have offered for sale as an ebook to end up making some dough for their own end, precisely considering that you could possibly have tried it if you ever decided. Those good ideas also worked to be a fantastic way to fully grasp other people online have a similar fervor like mine to figure out more in terms of this condition. I think there are several more pleasurable moments up front for those who find out your website.

  4. Thanks for any other informative web site. The place else may just I am getting that type of information written in such an ideal way? I have a mission that I am simply now operating on, and I have been on the look out for such information.

  5. I’m not sure why but this website is loading extremely slow for me. Is anyone else having this problem or is it a problem on my end? I’ll check back later on and see if the problem still exists.

Comments are closed.