Getting a range of number stream

Given DoubleStream s, I can do s.min()or s.max(), but not both, as either 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 flow range? (Except s.collect(Collectors.toList()); new Range(s.stream().min(),s.stream().max());)

+4
source share
1 answer

You can call the method DoubleStream summaryStatistics. It returns an object DoubleSummaryStatisticsthat contains min and max, plus several other statistics: average, quantity, and amount.

IntStreamand LongStreamhave similar methods summaryStatistics.

+9
source

Source: https://habr.com/ru/post/1613750/


All Articles