DoubleStream summaryStatistics() method in Java

Java 8Object Oriented ProgrammingProgramming

The summaryStatistics() method of the DoubleStream class returns a DoubleSummaryStatistics describing various summary data about the elements of this stream. This is a special case of a reduction.

The syntax is as follows

DoubleSummaryStatistics summaryStatistics()

Here, DoubleSummaryStatistics is a state object for collecting statistics such as count, min, max, average, etc. It works with streams. To use the DoubleStream class in Java, import the following package

import java.util.stream.DoubleStream;

First, create a DoubleStream and add some elements

DoubleStream doubleStream = DoubleStream.of(20.5, 35.8, 45.9, 50.8, 80.7);

Now, get the statistics

DoubleSummaryStatistics details = doubleStream.summaryStatistics();

The following is an example to implement DoubleStream summaryStatistics() method

Example

 Live Demo

import java.util.stream.DoubleStream;
import java.util.DoubleSummaryStatistics;
public class Demo {
   public static void main(String[] args) {
      DoubleStream doubleStream = DoubleStream.of(20.5, 35.8, 45.9, 50.8, 80.7);
      DoubleSummaryStatistics details = doubleStream.summaryStatistics();
      System.out.println("Details = "+details);
   }
}

Output

Details = DoubleSummaryStatistics{count=5, sum=233.700000, min=20.500000, average=46.740000, max=80.700000}
raja
Updated on 30-Jul-2019 22:30:25

Advertisements