• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about Java 8 Stream

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public static void main(String[] args) {
Stream<Integer> nums = Stream.of(1, 2, 3, 4, 5);
nums.filter(n -> n % 2 == 1);
nums.forEach(p -> System.out.print(p));
}


//===================
In this main method, The Stream<Integer> contains 1,2,3,4,5.
The stream filter first.
Then, the same stream forEach.
the above main method will throw a IllegaStatementException.  I thought the first filter statement will not affect the seecond forEach statement.
Please tell me why throw Exception.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Becaue the Stream<Integer> nums is being used twice, while only one use is allowed.
The filter delivers a new Stream, so this is allowed:
 
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The message accompanying the exception is: stream has already been operated upon or closed

Streams can only be operated on once - you are trying to use it twice.  If you want to continue by operating on the stream returned by the filter, you could either include forEach in your pipeline:

or create a new stream object from the output of the filter:
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you are supposed to do is use the result from filter(...) as a new Stream. After all, Stream#filter(Predicate) is an intermediate operation, which returns a new Stream, so you should use that Stream. As PS says, once you have called a method on a Stream, the Stream is regarded as used, even if the method appears to do nothing.
Why are you using a Stream<Integer> rather than an IntStream? This is what I would have used:-I shall let you work out why the last line would read better as .forEach(System.out::print);

I shall also move this discussion to our Streams forum.
reply
    Bookmark Topic Watch Topic
  • New Topic