Java streams 12. Create numeric stream using class Random

Class Random has the following methods that create IntStream
ints()
ints(long streamSize)
— ints(int randomNumberOrigin, int randomNumberBound)
— ints(long streamSize, int randomNumberOrigin, int randomNumberBound)
Similar overloaded methods longs() and doubles() of the Random class create LongStream and DoubleStream correspondingly. For the multithreaded environment, use the same methods but on the ThreadLocalRandom class.

Using the method ints() is very straightforward. It creates an infinite stream of integers. For example:

  
  new Random().ints()
              .limit(3)
              .forEach(i -> System.out.print(i + " "));  
                        //prints: 729828546 -1094812726 -882682722
   

We have added the limit() operator to limit the size of the stream (the number of the emitted values). Otherwise, the created IntStream object would continue emitting integers until the program or JVM is stopped.

An overloaded version ints(long streamSize) achieves the same result of limiting the stream size without the limit() operator. For example:

  
  new Random().ints(3)
              .forEach(i -> System.out.print(i + " "));  
                        //prints: -1989654223 -774535445 1381816175
   

The ints(int randomNumberOrigin, int randomNumberBound) method creates IntStream that emits integers in the specified range (including the randomNumberOrigin value, but not including the randomNumberBound value). For example:

  
  new Random().ints(0, 10)
         .limit(3)
         .forEach(i -> System.out.print(i + " "));  //prints: 4 6 1
  

Again, we have added the limit() operator. Otherwise, the created stream would emit all ten values 0—9.

The same result can be achieved using the overload ints(long streamSize, int randomNumberOrigin, int randomNumberBound), as follows:  

  
  new Random().ints(3, 0, 10)
          .forEach(i -> System.out.print(i + " "));  //prints: 3 4 7
  

As we have mentioned already, other methods of the Random class create LongStream and DoubleStream
LongStream longs()
LongStream longs(long streamSize)
— LongStream longs(int randomNumberOrigin, int randomNumberBound)
— LongStream longs(long streamSize, int randomNumberOrigin, int randomNumberBound)
DoubleStream doubles()
DoubleStream doubles(long streamSize)
— DoubleStream doubles(int randomNumberOrigin, int randomNumberBound)
— DoubleStream doubles(long streamSize, int randomNumberOrigin, int randomNumberBound)

In the next post, we will talk about other ways to create a stream:
IntStream BitSet.stream()
— Stream<JarEntry> JarFile.stream()
Stream<String> Pattern.splitAsStream(java.lang.CharSequence)

See other posts on Java 8 streams and posts on other topics.
You can also use navigation pages for Java stream related blogs:
— Java 8 streams blog titles
— Create stream
— Stream operations
— Stream operation collect()
The source code of all the code examples is here in GitHub

, ,

Send your comments using the link Contact or in response to my newsletter.
If you do not receive the newsletter, subscribe via link Subscribe under Contact.

Powered by WordPress. Designed by Woo Themes