This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Question about Stream.map method

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

we have this code:



Why is it, that when i activate line

that this exception comes:

Because I thought that streams are only not usable anymore after a terminal operation is called... i commented out the terminal method collect at the bottom...
 
Ahmed Sabrioglu
Greenhorn
Posts: 13
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
don't bother... found the answer:


A stream is closed when a terminal operation is called. A stream is operated on when you call *any* stream operation.

You bind two operations to the stream you create on line 3: the mapToInt() on line 4 and the mapToDouble() on line 6. That's not allowed.

 
Marshal
Posts: 69847
278
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:-I think mapToInt() is more appropriate than straight .map() because the getAge() method will return an int. The .map() call would probably return a Stream<Integer>. Hint: use an IDE for your Streams code; hover your mouse over the method calls and it will tell you the type of Stream being returned.
As far as possible, don't create Stream<T> variables. You will occasionally have to in this sort of situation, but this is an exception:-Of course that might not compile because the something variable goes out of scope without ever being used
 
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ahmed Sabrioglu wrote:don't bother... found the answer:


Thanks for posting back what you found.  It may be useful to others.
 
Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic