javaconcatenationjava-streamprintlnintstream

Concat and print multiple Java String and int streams


I'm trying to print a concat Stream that includes String and Integer elements. I've tried a few ways, but none of them work. I can't find a proper way to do it on the Internet. It's just testing code as you can see:

import java.util.stream.*;
class testconcat{
    public static void main(String[] args){
        Stream<String> part1=Stream.of("Testing the ");
        Stream<String> part2=Stream.of("streams concat");
        Stream<String> part3=Stream.of(" on Java, dividing ");
        Stream<String> part4=Stream.of("this phrase in ");
        IntStream part5=IntStream.of(6);
        Stream<String> part6=Stream.of(" parts.");

        String phrase=Stream.concat(Stream.concat(Stream.concat(Stream.concat(Stream.concat(part1, part2), part3), part4), part5), part6);
        System.out.println(phrase);
    }
}

I know part5 is an Integer so I can't concat it the regular way, so I also tried:

        IntStream part5=IntStream.of(6);
        Stream<String> part6=Stream.of(" parts.");

        String phrase=Stream.concat(IntStream.concat(Stream.concat(Stream.concat(Stream.concat(part1, part2), part3), part4), part5), part6);
        System.out.println(phrase);
    }
}

and also:

        Integer part5=6;
        String part6=(" parts.");

        String phrase=Stream.concat(Stream.concat(Stream.concat(Stream.concat(Stream.concat(part1, part2), part3), part4);
        System.out.println(phrase + Integer.toString(part5) + part6);
    }
}

None of which works. Is there a way to do that?


Solution

  • I'm trying to print a concat Stream that includes String and Integer elements

    Wrong, IntStream is a specific stream of primitive int value(s), and it is not the same as Stream, and therefore, IntStream::boxed or IntStream::mapToObj must be invoked to produce Stream<?> which can be used in Stream::concat:

    Stream<?> many = Stream
        .concat(part1, Stream
            .concat(part2, Stream
                .concat(part3, Stream
                    .concat(part4, Stream
                        .concat(part5.boxed(), part6)
                    )
                )
            )
        );
    

    To get the result of the string concatenation as String, a terminal operation Stream.collect(Collectors.joining()) should be applied after mapping all the objects in the streams into String using String::valueOf (or Objects::toString):

    String result = many.map(String::valueOf).collect(Collectors.joining());
    System.out.println(result);
    // -> Testing the streams concat on Java, dividing this phrase in 6 parts.
    

    However, such verbose Stream concatenation is redundant, and the Streams can be joined using Stream.of + Stream::flatMap. In the example below, IntStream mapped to String immediately, so no extra Stream::map is needed:

    Stream<String> many = Stream
        .of(part1, part2, part3, part4, part5.mapToObj(String::valueOf), part6)
        .flatMap(s -> s);
    
    String res = many.collect(Collectors.joining());
    System.out.println(res);
    // -> Testing the streams concat on Java, dividing this phrase in 6 parts.