1

I am creating a Supplier Stream of String and working on the same.

public void test(Stream<String> s) {
    Supplier<Stream<String>> streamSupplier = () -> s;

    System.out.println(streamSupplier.get().count());

    streamSupplier.get().parallel()
        .collect(Collectors.groupingBy(it -> counter.getAndIncrement() / 2))
        .values()
        .stream()
        .forEach(input -> {
            System.out.println("input " + input);
        });

I am getting an error "stream has already been operated upon or closed" when I try to work with the stream. How can I resolve this. Thank you.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
Tanu
  • 1,286
  • 4
  • 16
  • 35
  • 5
    A stream instance can only be used once. This supplier returns the same stream instance each time `get` is called. Typically, suppliers should return a new instance on each invocation. – Tim Moore Dec 12 '22 at 08:58
  • 1
    What do you mean by "stream of stream"? And at which line in your code do you get that error message? Please edit your question and add the complete stack trace of the exception. – tgdavies Dec 12 '22 at 08:58
  • You can check [this answer](https://stackoverflow.com/a/67040099/10819573) to learn more about how to use a `Supplier` of `Stream`. – Arvind Kumar Avinash Dec 12 '22 at 10:14
  • Since a stream instance can only be used once, we can modified the code as below: `List slist = s.collect(Collectors.toList()); System.out.println(slist.size()); Stream s1 = slist.stream(); Supplier> streamSupplier = () -> s1; final AtomicInteger counter = new AtomicInteger(); streamSupplier.get().parallel()` ... – Neo Jou Dec 12 '22 at 12:26
  • @NeoJou this is giving me an error as well. – Tanu Dec 13 '22 at 02:01
  • @ArvindKumarAvinash I am using the supplier stream in the example above, the issue I am facing is that I am working with an already created stream. Thanks – Tanu Dec 13 '22 at 06:12
  • @Tanu Since Stream s is the parameter of the test, is it possible that the Steam s is already consumed before entering to run in this test function? I did a simple test problem at https://github.com/neojou/java_on_macOS/blob/main/SupplierExample/src/Main.java and it runs well here. FYI. – Neo Jou Dec 14 '22 at 15:44

1 Answers1

0

Not really sure what you are trying to achieve with this one, but a stream can only be used once. Quoting from the documentation:

A stream should be operated on (invoking an intermediate or terminal stream operation) only once. This rules out, for example, "forked" streams, where the same source feeds two or more pipelines, or multiple traversals of the same stream. A stream implementation may throw IllegalStateException if it detects that the stream is being reused. However, since some stream operations may return their receiver rather than a new stream object, it may not be possible to detect reuse in all cases.

I think that you want to have a re-usable stream, so in this case I would suggest you pass in the original collection instead of the Stream of this collection dataset.

akortex
  • 5,067
  • 2
  • 25
  • 57