Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the range of a numeric stream

Given a DoubleStream s, I can do s.min() or s.max() but not both, as any one of them will consume the stream.

Now suppose I have

class Range /* can add code here */ {
    private final double min;
    private final double max;
    Range(double min, double max){
        this.min = min;
        this.max = max;
    }
    // can add code here
}

How can I get the range of the stream? (Other than by s.collect(Collectors.toList()); new Range(s.stream().min(),s.stream().max());)

like image 921
Museful Avatar asked Jan 07 '23 06:01

Museful


1 Answers

You can call DoubleStream's summaryStatistics method. It returns a DoubleSummaryStatistics object that contains a min and a max, plus a few other statistics: average, count, and sum.

IntStream and LongStream have similar summaryStatistics methods.

like image 113
rgettman Avatar answered Jan 17 '23 17:01

rgettman