• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

stream question

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this simple code

I got error from the "map" part, the "filter" part works fine.
java.lang.IllegalStateException: stream has already been operated upon or closed
at java.util.stream.AbstractPipeline.<init>(Unknown Source)
at java.util.stream.ReferencePipeline.<init>(Unknown Source)
at java.util.stream.ReferencePipeline$StatelessOp.<init>(Unknown Source)
at java.util.stream.ReferencePipeline$2.<init>(Unknown Source)
at java.util.stream.ReferencePipeline.filter(Unknown Source)
at streams.Example2.main(Example2.java:27)

Can somebody help explain in little depth ?
 
Bartender
Posts: 6115
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • On line 12 you assign to stream1 a new Stream.
  • On lines 13 & 14 you consume that stream.
  • On lines 17-19 you attempt to consume same stream again, but the stream is empty at this point.
  •  
    Saloon Keeper
    Posts: 3443
    149
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi Linwood,

    a Stream can only operated on once. In your code you are trying to do two operations on stream1. As you see, you get an error here.

    An alternative is to group the strings in your memberNames on the startletter. For instance:


     
    This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!