Site Search:

Develop code that uses Stream data methods and calculation methods

Back>



summaryStatistics
summaryStatistics


Stream and primitive Streams provided terminal methods for calculating, particularly count, max, min for Stream<T>, count, sum, average, max, min, summaryStatistics for primitive Streams. The method summaryStatistics is special, it allows you to get many calculating results without consuming the stream multiple times.

Stream<T>

long count()
Optional<T> max(Comparator<? super T> comparator)
Optional<T> min(Comparator<? super T> comparator)

DoubleStream

OptionalDouble average()
long count()
double sum()
OptionalDouble max()
OptionalDouble min()
DoubleStatistics summaryStatistics()

DoubleStatics have the following methods we need to know:
double getAverage()
long getCount()
double getSum()
double getMax()
double getMin()

IntStream

OptionalInt average()
long count()
int sum()
OptionalInt max()
OptionalInt min()
IntStatistics summaryStatistics()

IntStatistics:
double getAverage()
long getCount()
long getSum()
int getMax()
int getMin()

LongStream

OptionalLong average()
long count()
long sum()
OptionalLong max()
OptionalLong min()
LongStatistics summaryStatistics()

LongStatistics:
double getAverage()
long getCount()
long getSum()
long getMax()
long getMin()


OCPJP>cat CalculatingDemo.java 
import java.util.stream.*;
import java.util.Comparator;
import java.util.DoubleSummaryStatistics;
import java.util.IntSummaryStatistics;
import java.util.LongSummaryStatistics;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;

public class CalculatingDemo {
    public static void main(String...args) {
        
        System.out.println("Stream<T> count, min, max");
        Stream<String> stream = Stream.of("A", "a", "bb", "BB", "C", "c");
        System.out.println("Stream<T>.count: " + stream.count());
        Optional<String> min = Stream.of("A", "a", "bb", "BB", "C", "c").min(Comparator.naturalOrder());
        System.out.println("Stream<T>.min: " + min.get());
        Optional<String> max = Stream.of("A", "a", "bb", "BB", "C", "c").max(Comparator.reverseOrder());
        System.out.println("Stream<T>.max: ");
        max.ifPresent(System.out::println);
        
        System.out.println("DoubleStream average, count, sum, min, max, summaryStatistics");
        DoubleStream doubleStream = DoubleStream.of(0.1, 0.3, 0.2, 0.5, 0.4);
        OptionalDouble average = doubleStream.average();
        System.out.println("doubleStream.average: " + average.getAsDouble());
        long count = DoubleStream.of(0.1, 0.3, 0.2, 0.5, 0.4).count();
        System.out.println("doubleStream.count: " + count);
        double sum = DoubleStream.of(0.1, 0.3, 0.2, 0.5, 0.4).sum();
        System.out.println("doubleStream.sum: " + sum);
        OptionalDouble minDouble = DoubleStream.of(0.1, 0.3, 0.2, 0.5, 0.4).min();
        System.out.println("doubleStream.min: " + minDouble.getAsDouble());
        OptionalDouble maxDouble = DoubleStream.of(0.1, 0.3, 0.2, 0.5, 0.4).max();
        System.out.println("doubleStream.max: " + maxDouble.getAsDouble());
        DoubleSummaryStatistics doubleStatistics = DoubleStream.of(0.1, 0.3, 0.2, 0.5, 0.4).summaryStatistics();
        System.out.println("DoubleSummaryStatistics.getAverage: " + doubleStatistics.getAverage());
        System.out.println("DoubleSummaryStatistics.getCount: " + doubleStatistics.getCount());
        System.out.println("DoubleSummaryStatistics.getMax: " + doubleStatistics.getMax());
        System.out.println("DoubleSummaryStatistics.getMin: " + doubleStatistics.getMin());
        System.out.println("DoubleSummaryStatistics.getSum: " + doubleStatistics.getSum());
        
        System.out.println("IntStream average, count, min, max, sum, summaryStatistics");
        IntStream intStream = IntStream.of(1, 3, 2, 5, 4);
        average = intStream.average();
        System.out.println("IntStream.average: " + average.getAsDouble());
        count = IntStream.of(1, 3, 2, 5, 4).count();
        System.out.println("IntStream.count: " + count);
        OptionalInt minInt = IntStream.of(1, 3, 2, 5, 4).min();
        System.out.println("IntStream.min: " + minInt.getAsInt());
        OptionalInt maxInt = IntStream.of(1, 3, 2, 5, 4).max();
        maxInt.ifPresent(i -> System.out.println("IntStream.max: " + i));
        int sumInt = IntStream.of(1, 3, 2, 5, 4).sum();
        System.out.println("IntStream.sum: " + sumInt);
        IntSummaryStatistics intSummaryStatistics = IntStream.of(1, 3, 2, 5, 4).summaryStatistics();
        System.out.println("IntSummaryStatistics.getAverage: " + intSummaryStatistics.getAverage());
        System.out.println("IntSummaryStatistics.getCount: " + intSummaryStatistics.getCount());
        System.out.println("IntSummaryStatistics.getMax: " + intSummaryStatistics.getMax());
        System.out.println("IntSummaryStatistics.getMin: " + intSummaryStatistics.getMin());
        System.out.println("IntSummaryStatistics.getSum: " + intSummaryStatistics.getSum());
        
        System.out.println("LongStream average, count, min, max, sum, summaryStatistics");
        LongStream longStream = LongStream.of(1, 3, 2L, 5, 4);
        average = longStream.average();
        System.out.println("LongStream.average: " + average);
        count = LongStream.of(1, 3, 2L, 5, 4).count();
        System.out.println("LongStream.count: " + count);
        OptionalLong minLong = LongStream.of(1, 3, 2L, 5, 4).min();
        System.out.println("LongStream.min: " + minLong.getAsLong());
        OptionalLong maxLong = LongStream.of(1, 3, 2L, 5, 4).max();
        System.out.println("LongStream.max: " + maxLong.getAsLong());
        long sumLong = LongStream.of(1, 3, 2L, 5, 4).sum();
        System.out.println("LongStream.sum: " + sumLong);
        LongSummaryStatistics longSummaryStatistics = LongStream.of(1, 3, 2L, 5, 4).summaryStatistics();
        System.out.println("LongSummaryStatistics.getAverage: " + longSummaryStatistics.getAverage());
        System.out.println("LongSummaryStatistics.getCount: " + longSummaryStatistics.getCount());
        System.out.println("LongSummaryStatistics.getMin: " + longSummaryStatistics.getMin());
        System.out.println("LongSummaryStatistics.getMax: " + longSummaryStatistics.getMax());
        System.out.println("LongSummaryStatistics.getSum: " + longSummaryStatistics.getSum());
    }
}
OCPJP>
OCPJP>
OCPJP>
OCPJP>javac CalculatingDemo.java 
OCPJP>java CalculatingDemo
Stream<T> count, min, max
Stream<T>.count: 6
Stream<T>.min: A
Stream<T>.max: 
A
DoubleStream average, count, sum, min, max, summaryStatistics
doubleStream.average: 0.3
doubleStream.count: 5
doubleStream.sum: 1.5
doubleStream.min: 0.1
doubleStream.max: 0.5
DoubleSummaryStatistics.getAverage: 0.3
DoubleSummaryStatistics.getCount: 5
DoubleSummaryStatistics.getMax: 0.5
DoubleSummaryStatistics.getMin: 0.1
DoubleSummaryStatistics.getSum: 1.5
IntStream average, count, min, max, sum, summaryStatistics
IntStream.average: 3.0
IntStream.count: 5
IntStream.min: 1
IntStream.max: 5
IntStream.sum: 15
IntSummaryStatistics.getAverage: 3.0
IntSummaryStatistics.getCount: 5
IntSummaryStatistics.getMax: 5
IntSummaryStatistics.getMin: 1
IntSummaryStatistics.getSum: 15
LongStream average, count, min, max, sum, summaryStatistics
LongStream.average: OptionalDouble[3.0]
LongStream.count: 5
LongStream.min: 1
LongStream.max: 5
LongStream.sum: 15
LongSummaryStatistics.getAverage: 3.0
LongSummaryStatistics.getCount: 5
LongSummaryStatistics.getMin: 1
LongSummaryStatistics.getMax: 5
LongSummaryStatistics.getSum: 15
OCPJP>


It is long, but mostly repetitive.