Joining Stream of strings – Collectors.joining()

The Collectors.joining() function helps you join elements of a Stream, containing strings. It has three optional parameters, namely—delimiter, prefix, and postfix.

Consider the following example of the program:

  fun main(args: Array<String>) { 
      val resultantString = Stream.builder<String>() 
              .add("Item 1") 
              .add("Item 2") 
              .add("Item 3") 
              .add("Item 4") 
              .add("Item 5") 
              .add("Item 6") 
              .build() 
              .collect(Collectors.joining(" - ","Starts Here=>","<=Ends   Here")) 
 
      println("resultantString $resultantString") 
  } 

The output is as follows:

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

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