-3

I'm trying to print a concat Stream that includes String and Integer elements. I've try a few ways, but none of them works. I can't find a proper way to do it on internet. It's just a 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 on a 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? Thanks in advance.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Shinington
  • 17
  • 1
  • 6
  • Do you want to combine streams or strings/Integers? – omer blechman Feb 24 '22 at 12:06
  • I already tried with strings and integers and could do it, but now I must do it with streams, as it's for my studies. – Shinington Feb 24 '22 at 12:11
  • The easiest way is to replace your part5 declaration with `Stream part5=IntStream.of(6).mapToObj(String::valueOf);`. But that still won’t change the fact that Stream.concat returns another Stream. It will never return a String. – VGR Feb 24 '22 at 14:42

2 Answers2

0

If I understood you correctly and you want to create a combined stream string, you can use something like:

Stream.concat(...):

Stream<Integer> integerStream = IntStream.range(72, 129).boxed(); // to convert to Stream<Integer>
Stream<String> stringStream =Stream.of(" String Stream");
String str = Stream.concat(integerStream.map(String::valueOf), stringStream).collect(Collectors.joining(","));
System.out.println(str);

Or

Stream.of(...)

Stream<Integer> integerStream = IntStream.range(72, 129).boxed(); // to convert to Stream<Integer>
Stream<String> stringStream =Stream.of(" String Stream");
String str = Stream.of(integerStream.map(String::valueOf), stringStream).flatMap(t -> t).collect(Collectors.joining(","));
        System.out.println(str);
omer blechman
  • 277
  • 2
  • 16
  • What does the 72, 129 thing mean? – Shinington Feb 24 '22 at 12:32
  • It's just an example for creating IntStream. It's like for loop from 72 to 129 – omer blechman Feb 24 '22 at 12:34
  • But how could I use it without a .range? I just need it to print the value I gave to the integer (6). I didn't say, but the result I want to receive is a printed line that shows the whole phrase (Testing the stream concat on Java, dividing this phrase in 6 parts). I know I can put that 6 as a String but the point is learning how to concat different data types. – Shinington Feb 24 '22 at 12:41
  • IntStream.of(6).boxed() – omer blechman Feb 24 '22 at 12:42
0

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`):
```java
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.
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42