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...\n"+str);    } }

Output

Join result... Bing Bang Theory Vampire Diaries Game of Thrones Homecoming
Updated on: 2019-07-30T22:30:26+05:30

599 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements