Stream Builder

The Stream Builder interface makes it really easy to create an instance of Stream with ease. Have a look at the following example:

  fun main(args: Array<String>) { 
      val stream = Stream.builder<String>() 
              .add("Item 1") 
              .add("Item 2") 
              .add("Item 3") 
              .add("Item 4") 
              .add("Item 5") 
              .add("Item 6") 
              .add("Item 7") 
              .add("Item 8") 
              .add("Item 9") 
              .add("Item 10") 
              .build() 
      println("The Stream is ${stream.collect(Collectors.toList())}") 
  } 

The output is as follows:

The Stream.builder() method returns an instance of Streams.Builder. Then, we used the Builder.add function; the add function accepts an item for the stream value to be built, and returns the same instance of Stream.Builder. The build function then created the stream instance with the items provided to the builder.

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

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