How to convert string Stream to join them in Java?


Let us create string Stream:

Stream<String> stream = Stream.of("Bing Bang Theory", "Vampire Diaries", "Game of Thrones", "Homecoming");

Convert the above string stream and join them with Collectors:

final String str = stream.collect(Collectors.joining(" "));

The following is an example to convert string Stream to join them:

Example

import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Demo {
   public static void main(String[] args) {
      Stream<String> stream = Stream.of("Bing Bang Theory", "Vampire Diaries", "Game of Thrones", "Homecoming");
      final String str = stream.collect(Collectors.joining(" "));
      System.out.println("Join result...
"+str);    } }

Output

Join result...
Bing Bang Theory Vampire Diaries Game of Thrones Homecoming

Updated on: 30-Jul-2019

363 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements