Convert stream to String

The opposite of converting a string to a stream, this example will demonstrate converting a java 8 stream into a string. A Collector is a reduction operation which will gather together elements from a stream into a cumulative result of a String. Collectors.joining() will return a Collector that will concatenates the input elements into a String which has similar behavior to joining strings. If you want to join the string by a delimiter just pass in a CharSequence into the joining method. For instance, if you want to join string by a comma it would look like Collectors.joining(",").

Java 8

@Test
public void stream_to_list() {

    String streamToString = Stream.of("a", "b", "c")
            .collect(Collectors.joining());

    assertEquals("abc", streamToString);
}

If you prefer, you could use a static import.

...
import static java.util.stream.Collectors.toSet;
...
@Test
public void stream_to_set() {

    String streamToString = Stream.of("a", "b", "c")
            .collect(joining());

    assertEquals("abc", streamToString);
}