0

How can I write a loop with an until condition using Java 8 streams?

void readValue(char[] buf, int len, int curIndex) {
        for(; curIndex < len; curIndex++) {
            if(buf[curIndex] == '|') {
                break;
            } else {
                System.out.print(buf[curIndex]);
            }
        }
}

When I use filter, it filters all the '|' characters. I want to break out when I encounter the first one.

    Arrays.stream(buf, curIndex, len)
          .filter(n -> n != '|')
          .forEach(k -> System.out.print(k));
Tunaki
  • 132,869
  • 46
  • 340
  • 423
mauryat
  • 1,610
  • 5
  • 29
  • 53
  • 1
    In short, from the duplicate: not easily; it's probably easier just to do it the old-fashioned way. Also, recall that there is no `CharStream`; the chars will get promoted to ints, which will not be `System.out.print`ed the same way. – Louis Wasserman Nov 12 '15 at 17:16
  • If you just need to print them, something like `System.out.println(new String(buf, curIndex, IntStream.range(curIndex, len).filter(i -> buf[i] == '|').findFirst().orElse(len)));` might work. – Tagir Valeev Nov 13 '15 at 01:18

0 Answers0