Java streams 5. Create using Stream.of()

The static Stream.of(T… values) method accepts values that are going to be emitted by the newly created Stream object at the request of the terminal operator downstream:

   
  //Creates empty Stream that emits nothing
  Stream.of().forEach(System.out::print);         //prints:

  Stream.of(1).forEach(System.out::print);        //prints: 1

  Stream.of(1,2).forEach(System.out::print);      //prints: 12

  Stream.of("1"," ","2").forEach(System.out::print);  
                                                  //prints: 1 2
   

The Stream.of() method accepts varargs. Which means, we can specify either comma-separated values (as shown in the preceding example) or an array: 

   
  String[] strings = {"1"," ","2"};

  Stream.of(strings).forEach(System.out::print);   //prints: 1 2
    

When there is no type specified for the Stream object, the compiler does not complain if the array contains a mix of types:

  
  Stream.of("1"," ",2).forEach(System.out::print);  //prints: 1 2                                             
   

Adding generics that declare the expected element type causes an exception when at least one of the listed elements has a different type:

  
  Stream<String> stringStream = Stream.of("1 ", "  ", 2); 
                                                  //compilation error
  

The of(T… values) method can be also used for the concatenation of multiple streams. Let’s assume, for example, that we have the following four streams that we would like to concatenate into one:

  
  Stream<Integer> stream1 = Stream.of(1, 2);

  Stream<Integer> stream2 = Stream.of(2, 3);  
  
  Stream<Integer> stream3 = Stream.of(3, 4);

  Stream<Integer> stream4 = Stream.of(4, 5);
    

We would like to concatenate them into one stream of values 1,2,2,3,3,4,4,5. First, we try the following code:

  
  Stream.of(stream1, stream2, stream3, stream4)       
        .forEach(System.out::print);    
           //prints: java.util.stream.ReferencePipeline$Head@58ceff1j
    

It does not do what we hoped for. It treats each stream as an object of the internal class java.util.stream.ReferencePipeline that is used in the Stream interface implementation. So, we need to add the flatMap() operator that converts each stream element into a stream (we will talk about this operator in one of the following posts while discussing intermediate operators):

  
  Stream.of(stream1, stream2, stream3, stream4)       
        .flatMap(e -> e)       
        .forEach(System.out::print);    //prints: 12233445 
   

The function we passed into flatMap() as a parameter (e -> e) looks like it’s doing nothing, but that is because each element of the stream is a stream already, so there is no need to transform it. By returning an element as the result of the flatMap() operation, we tell the pipeline to treat the return value as a Stream object.

The static of() method for numeric stream types works similarly:

  
  int sum = IntStream.of(1,2,3).sum(); 
  System.out.print(sum);             //prints: 6 

  long sumLong = LongStream.of(1,2,3).sum(); 
  System.out.print(sumLong);         //prints: 6 

  double avg = DoubleStream.of(1,2,3)
                           .average()
                           .getAsDouble(); 
  System.out.print(avg);             //prints: 2.0  
  

In the next post, we will talk about creating a stream using method Stream.builder().

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