stream to string java code example

Example 1: java stream collect to string

List<Integer> numbers = Arrays.asList( 4, 8, 15, 16, 23, 42 );
return numbers.stream()
        .map( n -> n.toString() )
        .collect( Collectors.joining( "," ) );

Example 2: inputstream to string java

StringBuilder textBuilder = new StringBuilder();
        try (Reader reader = new BufferedReader(new InputStreamReader
            (stream, Charset.forName(StandardCharsets.UTF_8.name())))) {
            int c = 0;
            while ((c = reader.read()) != -1) {
                textBuilder.append((char) c);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
 textBuilder.toString;

Tags:

Java Example