Calculates the average of all numeric elements in the stream. @return an Optional containing the average, or empty if the stream is empty
()
| 449 | * @return an Optional containing the average, or empty if the stream is empty |
| 450 | */ |
| 451 | public Optional average() { |
| 452 | long count = 0; |
| 453 | Number sum = Long.valueOf(0); |
| 454 | |
| 455 | while (iterator.hasNext()) { |
| 456 | count++; |
| 457 | sum = ELArithmetic.add(sum, iterator.next()); |
| 458 | } |
| 459 | |
| 460 | if (count == 0) { |
| 461 | return Optional.EMPTY; |
| 462 | } else { |
| 463 | return new Optional(ELArithmetic.divide(sum, Long.valueOf(count))); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | |
| 468 | /** |