How to convert string Stream to join them in Java?

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 02-May-2019 11:51:06
Advertisements