This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin Cookbook and have Ken Kousen on-line!
See this thread for details.
Win a copy of Kotlin Cookbook this week in the Kotlin 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
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Question about Stream.map method

 
Greenhorn
Posts: 11
  • 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: 11
  • 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: 67036
255
  • 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: 6568
176
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.
 
You may have just won ten million dollars! Or, maybe a tiny ad.
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!