Creating empty Streams – Stream.empty()

Creating empty Streams is really easy with the Streams.empty() factory method. Consider the following example:

  fun main(args: Array<String>) { 
      val emptyStream = Stream.empty<String>() 
      val item = emptyStream.findAny() 
      println("Item is $item") 
 } 

In the preceding example, we created an emptyStream value with Stream.empty(), we then used the findAny() function to get hold of any element randomly selected from that Stream. The findAny() method returns an Optional value with a randomly selected item from the Stream, or an empty Optional, if the Stream is empty.

The following is the output of the preceding program:

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

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