Creating a Stream by passing elements – Stream.of()

We can also get an instance of Stream by providing its elements to the of function. The of function works in a similar way to the Observable.just method from RxJava/RxKotlin.

Have a look at the following example:

 fun main(args: Array<String>) { 
      val stream = Stream.of("Item 1",2,"Item 3",4,5.0,"Item 6") 
      println("Items in Stream =            ${stream.collect(Collectors.toList())}") 
  } 

The output is as follows:

Straightforward and easy, isn't it?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.156.235