• 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
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

How to get the average of an array with a Stream

 
Marshal
Posts: 64690
225
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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: 64690
225
  • Mark post as helpful
  • send pies
  • 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:-
 
Saloon Keeper
Posts: 3304
146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would use 'orElseThrow' if there is no average. I wouldn't return 0.0.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!