1

Simply, the code below produces a stream of five lines, I need to concat these lines into one line (while streaming) for further use and before collecting them.The code is about to convert a character string into one long binary string

Stream<String> binText = "hello".chars()
    .mapToObj(x-> Integer.toBinaryString(x))
    .map(x-> String.format("%8s", x).replaceAll(" ", "0"));

I'm a bit new for the term of "Stream API", so any suggestion would be appreciated.

Update: Let say I want map the stream from the above code into int [] of 0 and 1, actually, I tried the below code and it works fine but it seems not efficient and need to be normalized

int [] binText = "hello".chars()
                     .mapToObj(x-> Integer.toBinaryString(x))
                     .map(x-> String.format("%8s", x).replaceAll(" ", "0"))
                     .flatMap(s-> Stream.of(s.split("")))
                     .mapToInt(Integer::parseInt)
                     .toArray();   

isn't it ?

FSm
  • 2,017
  • 7
  • 29
  • 55
  • Whats wrong with collecting this into a `String`? – Polygnome Oct 15 '20 at 16:36
  • so you want to have a `Stream<5Lines, 5Lines, 5Lines>` and so on? – Eugene Oct 15 '20 at 16:39
  • @Eugene, just concat the lines without any coma or space in between. – FSm Oct 15 '20 at 16:53
  • 1
    you need to provide an example of what you mean, exactly, this is rather confusing: _...while streaming for further use and before collecting them.._ – Eugene Oct 15 '20 at 16:54
  • Polygnome, actually, for some reasons, I need to merge the lines before collecting. – FSm Oct 15 '20 at 17:07
  • Eugene, I want the code above to produce one long line as a stream. – FSm Oct 15 '20 at 17:08
  • @FSm *Why*? Why not collect the lines into a string using a joining collector and be done with it? I don't see any scenario where you wouldn't simply collect the lines to get the string. – Polygnome Oct 15 '20 at 17:11
  • Polygnome, you are right, but something is looping in my mind, but it is ok , I will update the answer. – FSm Oct 15 '20 at 17:16
  • I have updated the question, please have a look at it. Many thanks. – FSm Oct 15 '20 at 17:21

1 Answers1

1

What you are looking for is a collector to join those strings and you would have

.collect(Collectors.joining())

eventually the complete solution could be:

String binText = "hello".chars()
        .mapToObj(Integer::toBinaryString)
        .map(x-> String.format("%8s", x).replace(" ", "0"))
        .collect(Collectors.joining());
Naman
  • 27,789
  • 26
  • 218
  • 353
  • 1
    I hope you are right, be the OP says : _...before collecting them_. may be he got the terminology wrong – Eugene Oct 15 '20 at 17:06
  • 1
    @Eugene `.collect(Collectors.joining()).stream()...` or `Collectors.collectingAndThen(...)` – Polygnome Oct 15 '20 at 17:12
  • @Eugene well yeah, at least relates when the expectation is accompanied with *convert a character string into one long binary string* :) – Naman Oct 15 '20 at 17:12
  • @Eungene, nice to know `Collectors.collectingAndThen` , many thanks – FSm Oct 15 '20 at 17:28