• 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

How to get the average of an array with a Stream

 
Marshal
Posts: 79177
377
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you look in this thread, you find that Suane Mane couldn't understand the threadstream code mentioned. So let's have a look at some of it. SM: be sure to look at the links.
What I shall use is an IntStream. Like any other Stream it takes a source of information, of which there are lots of different sorts, in this case I am using an array:-You shouldn't alter the source, nor should you have any side‑effects. So what you do is create a Stream which processes the individual elements of that information source. Needless to say, there are several ways to do that. I shall do the same as Kyle Prouty did: use Arrays.stream, which, according to that link, “Returns a sequential IntStream with the specified array as its source.”
Note that an IntStream is a Stream which processes ints; there are other kinds of Stream to process longs, doubles, and plain simple Stream to process objects.
Also, I am going to get the average as a double. Let's add another line to the previous bit of code:-Now let's add the Stream to that, but the following won't compile. It isn't complete.Now, let's go through IntStream  again, and see what methods it has. Lo and behold: the three kinds of Stream for “primitives” have an average() method
So we try that average() method and the compiler doesn't like it:-I shall let you work out what the compiler error is, but if you have another look at average() it returns OptionalDouble. The reason for having classes called Optional or OptionalXXX is that it is conceivable that the Stream might not have any elements when you try to complete the process, and in this case you would be trying to divide 0 ÷ 0 (‍)
So you can't simply turn an OptionalDouble into a double. If you go through its methods, you will find five places where double appears on the left. Kyle Prouty used the getAsDouble() method, which will work in the present circumstances...but you will get problems if you don't have anything in the Stream:-So I would prefer one of the other four methods; let's try orElse().What I have done is supply it with a “default” value, here 0.0. If it has a “real” result, it will supply that, otherwise you get the default value of 0.0.

[edit]That isn't the first time I have spelt Stream T-h-r-e-a-d
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

half an hour ago, I wrote:. . .. . .

I fed that code to JShell and gotThe result from the following code was different:-This is what happens with orElse(0.0):-.And if I use orElse with a “full” source array:-
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would use 'orElseThrow' if there is no average. I wouldn't return 0.0.
 
Creativity is allowing yourself to make mistakes; art is knowing which ones to keep. Keep this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic