Java Stream How to - Convert String Stream to join them








Question

We would like to know how to convert String Stream to join them.

Answer

//  w  ww  .j  a va2  s .co m
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {
  public static void main(String[] args) {
    // collect as typed array
    Stream<String> words = Stream.of("All", "men", "are", "created", "equal");
    final String joinedWords = words.collect(Collectors.joining(" "));
    System.out.println(joinedWords);

  }
}

The code above generates the following result.