LongStream summaryStatistics() method in Java

Java 8Object Oriented ProgrammingProgramming

The summaryStatistics() method in the LongStream class in Java returns a LongSummaryStatistics describing various summary data about the elements of this stream.

The syntax is as follows −

LongSummaryStatistics summaryStatistics()

Here, LongSummaryStatistics is a state object for collecting statistics such as count, min, max, etc.

To use the LongStream class in Java, import the following package

import java.util.stream.LongStream;

Create LongStream and add some elements −

LongStream longStream = LongStream.of(30000L, 15000L, 20000l, 25000l, 30000l);

Now, get the statistics −

LongSummaryStatistics info = longStream.summaryStatistics();

The following is an example to implement LongStream summaryStatistics() method in Java −

Example

 Live Demo

import java.util.stream.LongStream;
import java.util.LongSummaryStatistics;

public class Demo {
   public static void main(String[] args) {
      LongStream longStream = LongStream.of(30000L, 15000L, 20000l, 25000l, 30000l);
      LongSummaryStatistics info = longStream.summaryStatistics();
      System.out.println("Summary = "+info);
   }
}

Output

Summary = LongSummaryStatistics{count=5, sum=120000, min=15000, average=24000.000000, max=30000}
raja
Updated on 30-Jul-2019 22:30:25

Advertisements