• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stream can i abuse that?

 
Felipe Romani
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lately I've used quite "Stream" instead of "for", especially when the "for" has some conditional inside it, for example if I need to filter content from a list, then sort and pick up the first like this:



I'm doing something wrong using the "Stream" in these situations?

How do you use Stream and functional programming?
 
Stephan van Hulst
Bartender
Posts: 6584
84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch Felipe!

This looks just fine. What does Lance.getValue() return?
 
Felipe Romani
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stephan.

the lance.getValue() method return a double, this code is for study only.

but it is good to know that this is a good practice.

i really like functional programming of java 8, less code lines. =)
 
Stephan van Hulst
Bartender
Posts: 6584
84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case, don't use Comparator.comparing(). Use Comparator.comparingDouble() instead.
 
Campbell Ritchie
Marshal
Posts: 52599
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch again.

If you are after the largest double, consider changing to a DoubleStream (line 4):-I have not tested that code, nor examined it properly, so it may contain errors.
 
Campbell Ritchie
Marshal
Posts: 52599
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you have user as a field of Lance, rather than Lance/Weapon being a field of user?
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch again.

If you are after the largest double, consider changing to a DoubleStream (line 4):-I have not tested that code, nor examined it properly, so it may contain errors.

The DoubleStream cannot return anything that's a Lance anymore, so you lose that. Your max() method returns an OptionalDouble, not an Optional<Lance>.
 
Felipe Romani
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all answers, really to work with Double I can use DoubleStream, it makes more sense.

Abaout "Lance", this is not a weapon, I am Brazilian and I forgot to translate this word, "Lance" is an offer in an auction, that is an auction system I'm using to study.

once again thank you all for the answers.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!