HOW TO Generate endless Stream of data in Java

HOW TO Generate Endless Stream of Data in Java using the Stream package

Starting from Java 8 is available the so called Stream package, that, as its name implies, deals with streams, endless stream and a lot of operation onto them.

In this lesson we will see how to create virtually endless stream of data, numbers, strings and so on…  so, at this point if you know something about programming and computers, you are surely saying something like: “Ok this post is trash clickbait, let’s close this tab to free my RAM… yeah, endless c’mon..” , but NO man, this is real. Just keep reading!

Since it is the very first tutorial on the Streams, I will explain every concept, package or Class I am going to use, let’s begin!

Stream.iterate(.., ..)

In this example we’ll see how to create an endless stream of increasing-by-1 numbers, endless.

/*

BigInteger is a standard java Class capable to deal with big integers as you could imagine,

it has some static members such as ZERO, ONE, TWO and you can represent whatever number you want

of whatever size. Of course, it doesn’t work in the same way of the numerical numbers you know.

Let me explain with a simple example: The number 4 billion, won’t waste 4 GB of RAM as the int type would do.

*/

Stream simpleSequence = Stream.iterate(BigInteger.ZERO, n -> n.add(BigInteger.ONE));

/*

The first argument is the starting point, in our case ZERO

The second one is an UnaryOperator<T> function, which just give back a BigInteger ( in this case ) and does a single operation (increment).

*/

You may think, okay this will eat up all my RAM. The answer is just NO! You have to know that Streams in Java are lazy, each operation into them is executed only when required.

In fact if you want to print this stream (or any stream in general), you have to:

simpleSequence.forEach( (n) -> System.out.println(n) );

And it will just keep printing numbers, until you won’t quit your terminal.

StreamingBot – How to watch your online videos without ads and popups

Following this first example, we’ll see another useful technique: filtering the streams

simpleSequence.forEach( (n) -> System.out.println(n) );

This will print every number of our sequence, but if I would like to print only even ones?

simpleSequence.filter( n -> n.mod(BigInteger.valueOf(2)).equals(BigInteger.ZERO) ).forEach( (n) -> System.out.println(n) );

/*

The filter method create a brand new stream, including all the elements of our simpleSequence which makes the condition true.

In particular, in the filter you have to use a Predicate<T> a function which returns a boolean value.

mod(BigInteger b) is the modulus operator for the BigInteger type

*/

I leave to you the exercise to print only the odd ones!

Stream.generate( .. )

Now let’s talk about this other method, that may be useful too.

A simple example using Random numbers

Stream randoms = Stream.generate( () -> new Random().nextInt() );

/*

generate() takes a Supplier<T> as parameter, it just takes no params and returns a value of type T

*/

Now that you are expert you’ll understand that this will create an endless random numbers stream, on which you can iterate, filter and other things we’ll discover later on.

 

CONCLUSION

As you have seen during this little lesson, the Stream package is a really powerful technique to analyze big streams of data, but to master it, you must know something about the functional paradigm, somewhat different than the OOP.

But don’t worry, we’ll come back to these concepts as soon as possible!

The main goal of this article, is to make you discover new ways of programming, much more powerful than the standard ones, that in an era of Big Data and the problem of the Data retrival, master these methods is almost necessarily.

Leave a Reply

Your email address will not be published.